summaryrefslogtreecommitdiffstats
path: root/core/views/other.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@klebos.com>2021-04-28 02:43:09 +0100
committerPhilip Sargent <philip.sargent@klebos.com>2021-04-28 02:43:09 +0100
commite5cf1b5289d908133bcfbc7054b0eda1e658dbfd (patch)
treea8b535d79b33c2d9764312ec611def791e91add4 /core/views/other.py
parent62799d196b9f2558a769ddef4311fd1f06a8c99e (diff)
downloadtroggle-e5cf1b5289d908133bcfbc7054b0eda1e658dbfd.tar.gz
troggle-e5cf1b5289d908133bcfbc7054b0eda1e658dbfd.tar.bz2
troggle-e5cf1b5289d908133bcfbc7054b0eda1e658dbfd.zip
download logbook in standard HTML works
Diffstat (limited to 'core/views/other.py')
-rw-r--r--core/views/other.py77
1 files changed, 46 insertions, 31 deletions
diff --git a/core/views/other.py b/core/views/other.py
index 552bf6f..89fac23 100644
--- a/core/views/other.py
+++ b/core/views/other.py
@@ -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.
- '''
- 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")
+ NEED TO ADD IN THE MATERIAL WHIHC IS NOT IN ANY LBE ! e.g. front matter.
- if 'year' in request.GET:
- year=request.GET['year']
- if 'extension' in request.GET:
- extension=request.GET['extension']
-
- if extension =='txt':
- response = HttpResponse(content_type='text/plain')
- style='2008'
- elif extension == 'html':
- response = HttpResponse(content_type='text/html')
- style='2005'
+ 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:
- 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):