From 1968db62ad9bdc41f52afc26a8a929f5aeca4a19 Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@klebos.com>
Date: Wed, 21 Apr 2021 22:09:20 +0100
Subject: [PATCH] archeology on the logbook entry editing forms

---
 core/forms.py                                 | 32 +++++++++---------
 core/views/other.py                           | 33 ++++++++++++++++---
 logbooksdump.py                               | 16 +++++++--
 .../{fileupload.html => _fileupload.html}     |  0
 templates/core/todos.html                     |  4 +--
 urls.py                                       |  4 +--
 6 files changed, 60 insertions(+), 29 deletions(-)
 rename templates/{fileupload.html => _fileupload.html} (100%)

diff --git a/core/forms.py b/core/forms.py
index 0ecca73..977e664 100644
--- a/core/forms.py
+++ b/core/forms.py
@@ -93,14 +93,14 @@ class EntranceForm(ModelForm):
         return self.cleaned_data
 
 
-# This next is called from the templates/edit_cave2.html template.
+# This next line is called from the templates/edit_cave2.html template.
 # This is sufficeint to create an entire entry for for the cave fields automatically
 # http://localhost:8000/cave/new/
 # using django built-in Deep magic. https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/
 CaveAndEntranceFormSet = modelformset_factory(CaveAndEntrance, exclude=('cave',))
 
 class EntranceLetterForm(ModelForm):
-    '''Can't see what this does at all
+    '''Can't see what this does at all. called twice from views.caves
     '''
     class Meta:
         model = CaveAndEntrance
@@ -148,37 +148,35 @@ def get_name(pe):
 class UploadFileForm(forms.Form):
     """Only called by views.others.newFile() which seems to be only about logbook files.
     """
+    title = forms.CharField(max_length=50)
+    file  = forms.FileField()
+    #html = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
+    html = forms.CharField(widget=forms.Textarea(attrs={"cols":80, "rows":20}))
+    lon_utm = forms.FloatField(required=False)
+    lat_utm = forms.FloatField(required=False)
+    slug = forms.CharField(max_length=50)
+    date = forms.DateField(required=False)
+    survey_point = forms.CharField()
+
     # Because this has EXECUTABLE statements in its signature (the fields) they get
     # executed when this module is LOADED. Which barfs horribly.
-    # so all replaced by an __init__ method instead.
+    # so all put in an __init__ method instead pending deletion or rewriting
     def __init__(self):
-        title = forms.CharField(max_length=50)
-        file  = forms.FileField()
-        #html = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
-        html = forms.CharField(widget=forms.Textarea(attrs={"cols":80, "rows":20}))
-        lon_utm = forms.FloatField(required=False)
-        lat_utm = forms.FloatField(required=False)
-        slug = forms.CharField(max_length=50)
-        date = forms.DateField(required=False)
-
         #This next line is the one that causes django.setup() to BARF LOUDLY
         caves = [cave.slug for cave in Cave.objects.all()]
         #caves.sort() # sort needs rewriting for python3
         caves = ["-----"] + caves
         cave = forms.ChoiceField([(c, c) for c in caves], required=False)
-
+        
         entrance = forms.ChoiceField([("-----", "Please select a cave"), ], required=False) 
-        qm = forms.ChoiceField([("-----", "Please select a cave"), ], required=False) 
 
         expeditions = [e.year for e in Expedition.objects.all()]
         expeditions.sort()
         expeditions = ["-----"] + expeditions
         expedition = forms.ChoiceField([(e, e) for e in expeditions], required=False)  
 
+        qm = forms.ChoiceField([("-----", "Please select a cave"), ], required=False) 
         logbookentry = forms.ChoiceField([("-----", "Please select an expedition"), ], required=False) 
-
         person = forms.ChoiceField([("-----", "Please select an expedition"), ], required=False) 
 
-        survey_point = forms.CharField()
-
 
diff --git a/core/views/other.py b/core/views/other.py
index cadb609..4f27d98 100644
--- a/core/views/other.py
+++ b/core/views/other.py
@@ -23,15 +23,24 @@ download all QMs (not working)
 
 todo = '''Delete the newfile & TripForm code once we have a proper file-upload system working.
 meanwhile keep it as an example to consider.
+
+Check that the logbookdownloader works by testing with a round trip.
+
+Use it to convert all older logbooks into the 2005-variant of HTML then we can 
+get rid of the parsers for older formats.  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!
+When we have done all the old logbooks, delete this function and the two templates.
+
+But how does this interact with troggle/logbooksdump.py ?S
 '''
 
 def todos(request, module): 
     '''produces todo text from module
+    We should automate this to find all those strings automatically
     '''
     from troggle.core.TESTS.tests import todo as tests
     from troggle.core.views.logbooks import todo as viewlogbooks
     from troggle.core.forms import todo as forms
-    print(f'TODO - {tests}')
     tododict = {'views/other': todo,
         'tests': tests, 
         'views/logbooks': viewlogbooks, 
@@ -85,6 +94,12 @@ def controlpanel(request):
 
 
 def downloadlogbook(request,year=None,extension=None,queryset=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, tobe followed by
+    extensive hand-editing.
+    '''
     
     if year:
         current_expedition=Expedition.objects.get(year=year)
@@ -108,11 +123,14 @@ def downloadlogbook(request,year=None,extension=None,queryset=None):
     elif extension == 'html':
         response = HttpResponse(content_type='text/html')
         style='2005'
+    else:
+        response = HttpResponse(content_type='text/html')
+        style='2005'
         
     template='logbook'+style+'style.'+extension
     response['Content-Disposition'] = 'attachment; filename='+filename+'.'+extension 
     t=loader.get_template(template)
-    c=Context({'logbook_entries':logbook_entries})
+    c={'logbook_entries':logbook_entries}
     response.write(t.render(c))
     return response
 
@@ -153,7 +171,12 @@ def ajax_QM_number(request):
 
 @login_required_if_public
 def newfile(request, pslug = None):
-    ''' not known quite what this was for or where it fits in - original 2006 troggle idea never finished?
+    '''     
+    If not POST, it goes straight to UploadFileForm using the editfile.html template which is about re-submitting 
+    a LBE aka TripReport
+    
+    If it is POST, then it is a new LBE so an HTML formatted version of it is produced, using template.render()
+    and a format 'dataformat/logbookentry.html' which is then put in expoweb/years<year>/autologbook/<LBEnamedfile>
     '''
     if pslug:
         previousfile = LogbookEntry.objects.get(slug = pslug, date = previousdate, expedition = expedition)
@@ -174,10 +197,10 @@ def newfile(request, pslug = None):
                 delLogbookEntry(previouslbe)
             f = open(filename, "w")
             template = loader.get_template('dataformat/logbookentry.html')
-            context = Context({'trip': tripForm.cleaned_data, 
+            context = {'trip': tripForm.cleaned_data, 
                               'persons': personTripFormSet.cleaned_data,
                               'date': dateStr,
-                              'expeditionyear': expeditionyear})
+                              'expeditionyear': expeditionyear}
             f.write(template.render(context))
             f.close()
             print(logbookparsers.parseAutoLogBookEntry(filename))
diff --git a/logbooksdump.py b/logbooksdump.py
index a2a4d8e..4df4c93 100644
--- a/logbooksdump.py
+++ b/logbooksdump.py
@@ -22,6 +22,11 @@ from troggle.core.models.caves import Cave, Entrance
 
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 def import_auto_logbooks():
+    '''For logbook entries which have been 'typed up' by entering the data in a form,
+    which makes a copy of the LBE in years/<year>/autologbook/<tripname>.html
+    this will then re-import all thoise individual LBE files.
+    Gosh. How complicated. Thank goodness we don't do anything like this anymore.
+    '''
     import os
     import troggle.parsers.logbooks 
 
@@ -42,8 +47,13 @@ def import_auto_logbooks():
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 #Temporary function until definitive source of data transfered.
 from django.template.defaultfilters import slugify
-from django.template import Context, loader
+from django.template import loader
 def dumplogbooks():
+    '''This appears to take all the LBEs in the database and to write them all out as infividual html files
+    so that they can be re-imported.
+    This is the sort of silly thing you have to do when you started out thinking that the database was
+    going to be the Source Of All Truth and then retrofitting to make inthe input files be the master.
+    '''
     def get_name(pe):
             if pe.nickname:
                 return pe.nickname
@@ -68,10 +78,10 @@ def dumplogbooks():
             persons = [{"name": get_name(pt.personexpedition), "TU": pt.time_underground, "author": pt.is_logbook_entry_author} for pt in pts]
             f = open(filename, "wb")
             template = loader.get_template('dataformat/logbookentry.html')
-            context = Context({'trip': trip, 
+            context = {'trip': trip, 
                                'persons': persons,
                                'date': dateStr,
-                               'expeditionyear': lbe.expedition.year})
+                               'expeditionyear': lbe.expedition.year}
             output = template.render(context)
             f.write(str(output).encode( "utf-8" ))
             f.close()
diff --git a/templates/fileupload.html b/templates/_fileupload.html
similarity index 100%
rename from templates/fileupload.html
rename to templates/_fileupload.html
diff --git a/templates/core/todos.html b/templates/core/todos.html
index d6d2d4e..de1fd1d 100644
--- a/templates/core/todos.html
+++ b/templates/core/todos.html
@@ -1,3 +1,3 @@
 {% extends "baseapi.html" %}
-{% block content %}{% for k, v in tododict.items %}<b>{{k}}</b>: {{v}}<br /><br />
-{% endfor %}{% endblock %}
\ No newline at end of file
+{% block content %}<pre>{% for k, v in tododict.items %}<b><big>{{k}}</big></b>: {{v}}<br />
+{% endfor %}</pre>{% endblock %}
\ No newline at end of file
diff --git a/urls.py b/urls.py
index d3855b8..23e21e6 100644
--- a/urls.py
+++ b/urls.py
@@ -91,8 +91,8 @@ trogglepatterns = [
 #   Logbook entries
     re_path(r'^logbookentry/(?P<date>.*)/(?P<slug>.*)/?$', logbookentry,name="logbookentry"),
     re_path(r'^newfile', newfile, name="newFile"), # oddly broken, needs investigating more
-    re_path(r'^logbooksearch/(.*)/?$', logbookSearch),
-    re_path(r'^logbook(?P<year>\d\d\d\d)\.(?P<extension>.*)/?$', downloadlogbook),
+    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/?$', downloadlogbook, name="downloadlogbook"),
     
 #   Internal. editfile.html template uses these internally