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 @@