download logbook in standard HTML works

This commit is contained in:
Philip Sargent 2021-04-28 02:43:09 +01:00
parent 62799d196b
commit e5cf1b5289
5 changed files with 55 additions and 40 deletions

View File

@ -4,7 +4,7 @@ from django.forms import ModelForm
from django.http import HttpResponse from django.http import HttpResponse
from django.core import serializers from django.core import serializers
from troggle.core.views.other import downloadlogbook from troggle.core.views.other import exportlogbook
from troggle.core.models.troggle import Person, PersonExpedition, Expedition, DataIssue from troggle.core.models.troggle import Person, PersonExpedition, Expedition, DataIssue
from troggle.core.models.caves import Cave, Area, Entrance, CaveAndEntrance, LogbookEntry, PersonTrip, QM from troggle.core.models.caves import Cave, Area, Entrance, CaveAndEntrance, LogbookEntry, PersonTrip, QM
from troggle.core.models.survex import SurvexBlock, SurvexPersonRole, SurvexStation, Wallet, SingleScan from troggle.core.models.survex import SurvexBlock, SurvexPersonRole, SurvexStation, Wallet, SingleScan

View File

@ -1,4 +1,5 @@
import re, os import re, os
from pathlib import Path
from django import forms from django import forms
from django.conf import settings from django.conf import settings
@ -140,46 +141,60 @@ def controlpanel(request):
def downloadlogbook(request,year=None,extension=None,queryset=None): def exportlogbook(request,year=None,extension=None):
'''Constructs, from the database, a complete HTML (or TXT) formatted logbook - but TEXT ONLY '''Constructs, from the database, a complete HTML (or TXT) formatted logbook - but TEXT ONLY
for the current year. Formats available are HTML2005 or 2008text for the current year. Formats available are HTML2005 or 2008text
There are no images stored in the database, so this is only a tool for a first pass, to be followed by There are no images stored in the database, so this is only a tool for a first pass, to be followed by
extensive hand-editing. extensive hand-editing.
NEED TO ADD IN THE MATERIAL WHIHC IS NOT IN ANY LBE ! e.g. front matter.
This is the recipient of the POST action os the export form in the control panel
''' '''
def lbeKey(lbe):
"""This function goes into a lexicogrpahic sort function
"""
return str(lbe.date)
if not request.method=='POST':
return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':""})
else:
print(f'Logbook export {request.POST}')
if request.POST.get("year", '2016'):
year = request.POST['year']
if request.POST.get("extension", 'html'):
extension = request.POST['extension'] # e.g. html
if year:
current_expedition=Expedition.objects.get(year=year) current_expedition=Expedition.objects.get(year=year)
logbook_entries=LogbookEntry.objects.filter(expedition=current_expedition) logbook_entries=LogbookEntry.objects.filter(expedition=current_expedition).order_by('date') # need to be sorted by date!
filename='logbook'+year
elif queryset:
logbook_entries=queryset
filename='logbook'
else:
response = HttpResponse(content_type='text/plain')
return response(r"Error: Logbook downloader doesn't know what year you want")
if 'year' in request.GET: #print(f'Logbook has {len(logbook_entries)} entries in it.')
year=request.GET['year']
if 'extension' in request.GET:
extension=request.GET['extension']
if extension =='txt': if extension =='txt':
response = HttpResponse(content_type='text/plain') response = HttpResponse(content_type='text/plain')
style='2008' style='2008'
elif extension == 'html': else :
response = HttpResponse(content_type='text/html') extension == 'html'
style='2005' response = HttpResponse(content_type='text/html')
else: style='2005'
response = HttpResponse(content_type='text/html')
style='2005'
template='logbook'+style+'style.'+extension filename='newlogbook.' + extension
response['Content-Disposition'] = 'attachment; filename='+filename+'.'+extension template='logbook'+style+'style.'+extension
t=loader.get_template(template) response['Content-Disposition'] = 'attachment; filename='+filename
c={'logbook_entries':logbook_entries} t=loader.get_template(template)
response.write(t.render(c)) logbookfile = (t.render({'logbook_entries':logbook_entries}))
return response
dir = Path(settings.EXPOWEB) / "years" / year
filepath = Path(dir, filename)
with(open(filepath, 'w')) as lb:
lb.writelines(logbookfile)
#print(f'Logbook exported to {filepath}')
completed = f'Logbook exported to <a href="/years/{filename}">{filename}</a>'
return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':[completed]})
def ajax_test(request): def ajax_test(request):

View File

@ -19,7 +19,7 @@ django.db.transaction.TransactionManagementError:
An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block. An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
''' '''
todo='''- Update does not work when a cave id is in the pending list but a proper cave description file exists todo='''- db Update does not work when a cave id is in the pending list but a proper cave description file exists
and is being imported. It should work. But currently Django aborts and he file is not read in. and is being imported. It should work. But currently Django aborts and he file is not read in.
- Cannot use Edit This Page for pendingcaves.txt_edit as Edit This Page is expecting an html file. - Cannot use Edit This Page for pendingcaves.txt_edit as Edit This Page is expecting an html file.
@ -139,13 +139,13 @@ def readcaves():
''' '''
# For those caves which do not have cave_data/1623-xxx.html XML files even though they exist and have surveys # For those caves which do not have cave_data/1623-xxx.html XML files even though they exist and have surveys
# should put this in a simple list which can be edited using 'Edit this file' # should put this in a simple list which can be edited using 'Edit this file'
pending = [] pending = set()
fpending = Path(settings.CAVEDESCRIPTIONS, "pendingcaves.txt") fpending = Path(settings.CAVEDESCRIPTIONS, "pendingcaves.txt")
if fpending.is_file(): if fpending.is_file():
with open(fpending, "r") as fo: with open(fpending, "r") as fo:
cids = fo.readlines() cids = fo.readlines()
for cid in cids: for cid in cids:
pending.append(cid.rstrip('\n')) pending.add(cid.rstrip('\n'))
with transaction.atomic(): with transaction.atomic():
print(" - Deleting Caves and Entrances") print(" - Deleting Caves and Entrances")

View File

@ -95,7 +95,7 @@
</tr> </tr>
<tr> <tr>
<td> <td>
<form name="export" method="get" action={% url "downloadlogbook" %}> <form name="export" method="POST" action={% url "exportlogbook" %}>
{% csrf_token %} {% csrf_token %}
<p>Download a logbook file which is dynamically generated by Troggle.</p> <p>Download a logbook file which is dynamically generated by Troggle.</p>

View File

@ -11,7 +11,7 @@ from django.urls import reverse, resolve
from troggle.core.views import caves, statistics, survex from troggle.core.views import caves, statistics, survex
from troggle.core.views.surveys import scansingle, singlewallet, allwallets, dwgdata, dwgfilesingle, dwgfileupload from troggle.core.views.surveys import scansingle, singlewallet, allwallets, dwgdata, dwgfilesingle, dwgfileupload
from troggle.core.views.other import troggle404, frontpage, todos, controlpanel, frontpage, scanupload from troggle.core.views.other import troggle404, frontpage, todos, controlpanel, frontpage, scanupload
from troggle.core.views.other import downloadlogbook from troggle.core.views.other import exportlogbook
from troggle.core.views.caves import ent, cavepage from troggle.core.views.caves import ent, cavepage
from troggle.core.views.logbooks import get_logbook_entries, logbookentry, logbookSearch from troggle.core.views.logbooks import get_logbook_entries, logbookentry, logbookSearch
from troggle.core.views.logbooks import personindex, person, get_people from troggle.core.views.logbooks import personindex, person, get_people
@ -98,8 +98,8 @@ trogglepatterns = [
# Logbook entries # Logbook entries
re_path(r'^logbookentry/(?P<date>.*)/(?P<slug>.*)/?$', logbookentry,name="logbookentry"), re_path(r'^logbookentry/(?P<date>.*)/(?P<slug>.*)/?$', logbookentry,name="logbookentry"),
re_path(r'^logbooksearch/(.*)/?$', logbookSearch), # name 'search' not defined in views/logbooks.py re_path(r'^logbooksearch/(.*)/?$', logbookSearch), # name 'search' not defined in views/logbooks.py
re_path(r'^logbook(?P<year>\d\d\d\d)\.(?P<extension>.*)/?$', downloadlogbook), # e.g. /logbook2019.html # working but old CSS in template re_path(r'^logbook(?P<year>\d\d\d\d)\.(?P<extension>.*)/?$', exportlogbook, name='exportlogbook'), # e.g. /logbook2019.html # working but old CSS in
re_path(r'^logbook/?$', downloadlogbook, name="downloadlogbook"), re_path(r'^logbook$', exportlogbook, name='exportlogbook'),
# Internal. editfile.html template uses these internally # Internal. editfile.html template uses these internally
re_path(r'^getPeople/(?P<expeditionslug>.*)', get_people, name = "get_people"), re_path(r'^getPeople/(?P<expeditionslug>.*)', get_people, name = "get_people"),