From e5cf1b5289d908133bcfbc7054b0eda1e658dbfd Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Wed, 28 Apr 2021 02:43:09 +0100 Subject: [PATCH] download logbook in standard HTML works --- core/admin.py | 2 +- core/views/other.py | 77 ++++++++++++++++++++++--------------- parsers/caves.py | 6 +-- templates/controlPanel.html | 2 +- urls.py | 8 ++-- 5 files changed, 55 insertions(+), 40 deletions(-) diff --git a/core/admin.py b/core/admin.py index cc231da..dbf34b8 100644 --- a/core/admin.py +++ b/core/admin.py @@ -4,7 +4,7 @@ from django.forms import ModelForm from django.http import HttpResponse 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.caves import Cave, Area, Entrance, CaveAndEntrance, LogbookEntry, PersonTrip, QM from troggle.core.models.survex import SurvexBlock, SurvexPersonRole, SurvexStation, Wallet, SingleScan 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. + + 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 {filename}' + + return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':[completed]}) def ajax_test(request): diff --git a/parsers/caves.py b/parsers/caves.py index 50086ea..3b96d69 100644 --- a/parsers/caves.py +++ b/parsers/caves.py @@ -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. ''' -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. - 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 # should put this in a simple list which can be edited using 'Edit this file' - pending = [] + pending = set() fpending = Path(settings.CAVEDESCRIPTIONS, "pendingcaves.txt") if fpending.is_file(): with open(fpending, "r") as fo: cids = fo.readlines() for cid in cids: - pending.append(cid.rstrip('\n')) + pending.add(cid.rstrip('\n')) with transaction.atomic(): print(" - Deleting Caves and Entrances") diff --git a/templates/controlPanel.html b/templates/controlPanel.html index bab7bf9..8d90315 100644 --- a/templates/controlPanel.html +++ b/templates/controlPanel.html @@ -95,7 +95,7 @@ -
+ {% csrf_token %}

Download a logbook file which is dynamically generated by Troggle.

diff --git a/urls.py b/urls.py index 556f981..351c4ae 100644 --- a/urls.py +++ b/urls.py @@ -11,7 +11,7 @@ from django.urls import reverse, resolve from troggle.core.views import caves, statistics, survex 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 downloadlogbook +from troggle.core.views.other import exportlogbook 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 personindex, person, get_people @@ -98,9 +98,9 @@ trogglepatterns = [ # Logbook entries re_path(r'^logbookentry/(?P.*)/(?P.*)/?$', logbookentry,name="logbookentry"), re_path(r'^logbooksearch/(.*)/?$', logbookSearch), # name 'search' not defined in views/logbooks.py - re_path(r'^logbook(?P\d\d\d\d)\.(?P.*)/?$', downloadlogbook), # e.g. /logbook2019.html # working but old CSS in template - re_path(r'^logbook/?$', downloadlogbook, name="downloadlogbook"), - + re_path(r'^logbook(?P\d\d\d\d)\.(?P.*)/?$', exportlogbook, name='exportlogbook'), # e.g. /logbook2019.html # working but old CSS in + re_path(r'^logbook$', exportlogbook, name='exportlogbook'), + # Internal. editfile.html template uses these internally re_path(r'^getPeople/(?P.*)', get_people, name = "get_people"), re_path(r'^getLogBookEntries/(?P.*)', get_logbook_entries, name = "get_logbook_entries"),