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

@@ -1,4 +1,5 @@
import re, os
from pathlib import Path
from django import forms
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
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
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
'''
if year:
current_expedition=Expedition.objects.get(year=year)
logbook_entries=LogbookEntry.objects.filter(expedition=current_expedition)
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:
year=request.GET['year']
if 'extension' in request.GET:
extension=request.GET['extension']
def lbeKey(lbe):
"""This function goes into a lexicogrpahic sort function
"""
return str(lbe.date)
if extension =='txt':
response = HttpResponse(content_type='text/plain')
style='2008'
elif extension == 'html':
response = HttpResponse(content_type='text/html')
style='2005'
if not request.method=='POST':
return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':""})
else:
response = HttpResponse(content_type='text/html')
style='2005'
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
current_expedition=Expedition.objects.get(year=year)
logbook_entries=LogbookEntry.objects.filter(expedition=current_expedition).order_by('date') # need to be sorted by date!
template='logbook'+style+'style.'+extension
response['Content-Disposition'] = 'attachment; filename='+filename+'.'+extension
t=loader.get_template(template)
c={'logbook_entries':logbook_entries}
response.write(t.render(c))
return response
#print(f'Logbook has {len(logbook_entries)} entries in it.')
if extension =='txt':
response = HttpResponse(content_type='text/plain')
style='2008'
else :
extension == 'html'
response = HttpResponse(content_type='text/html')
style='2005'
filename='newlogbook.' + extension
template='logbook'+style+'style.'+extension
response['Content-Disposition'] = 'attachment; filename='+filename
t=loader.get_template(template)
logbookfile = (t.render({'logbook_entries':logbook_entries}))
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):