From d3c17361192fbdea57eb3ef339375f671cc69ebf Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 10 Aug 2023 23:17:03 +0300 Subject: [PATCH] extending logbook entry edit --- core/views/uploads.py | 106 ++++++++++++++++++++++++++++-------- templates/logbookentry.html | 1 + templates/logbookform.html | 6 +- urls.py | 2 +- 4 files changed, 89 insertions(+), 26 deletions(-) diff --git a/core/views/uploads.py b/core/views/uploads.py index 9bcce94..0be3d35 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -1,4 +1,5 @@ import subprocess +import hashlib from pathlib import Path from django import forms @@ -6,6 +7,7 @@ from django.core.files.storage import FileSystemStorage from django.shortcuts import render, redirect import settings +from troggle.core.models.logbooks import LogbookEntry, PersonLogEntry from troggle.core.models.survex import DrawingFile # from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time* @@ -38,6 +40,8 @@ todo = """ - Make file rename utility less ugly. """ +sha = hashlib.new('sha256') + class FilesForm(forms.Form): # not a model-form, just a form-form uploadfiles = forms.FileField() @@ -56,10 +60,33 @@ class LogbookEditForm(forms.Form): # not a model-form, just a form-form author = forms.CharField(strip=True, required=False) @login_required_if_public -def logbookedit(request, year=None): - """Type in a logbook entry. - No editing yet, name is implying a future enhancement +def logbookedit(request, year=None, slug=None): + """Edit a logbook entry + This is daft: we have the parsed identity of the person and we render it to text as 'fullname', to be re-parsed on re-importing. + And there is no guarantee that this will be the same thing, esp. as aliases are used in the initial data input. + So we are losing all the cute aliases that have been used over the years by this export/re-import process. Bother. + But they have already been lost in the Great Format Conversion of 2022-23 when everything was chnaged to use the same HTML parser. + Which is a shame. """ + def clean_tu(tu): + if tu =="": + return 0 + try: + tu = float(tu)/1 # check numeric + except: + return 0 + return tu + + def unique_id(text, n): + """This gives each logbook entry a unique id based on the date+content, so the order of entries on a particular day + does not matter. This is a change (August 2023) from previous process. + Otherwise we could get 2023-07-20a and 2023-07-20b swapped on exporting and re-importing logbooks + because the database does not record precendence. + 2 hex digits would seem adequate for each expo day, but we might get a collision.. + """ + sha.update(text.encode('utf-8')) + return sha.hexdigest()[0:n] + if not year: year = 2023 @@ -78,23 +105,18 @@ def logbookedit(request, year=None): title = request.POST["title"].strip() entry = request.POST["text"].strip() entry = entry.replace('\r','') # remove HTML-standard CR inserted - entry = entry.replace('\n\n','\n

\n') # replace 2 \n with

- entry = entry.replace(' tag, even if it has attributes, with

+ entry = entry.replace('\n\n','\n
\n
\n') # replace 2 \n with

+ entry = entry.replace('\n tag, even if it has attributes, with

entry = entry.replace('
','
') # clean up previous hack tu = request.POST["tu"].strip() - if tu =="": - tu = 0 - try: - tu = float(tu)/1 # check numeric - except: - tu = 0 - seq = 99 # should match the number of entries on this date +1 in the db already - + tu = clean_tu(tu) + uniq = unique_id(entry,2) + print(uniq) # OK this could be done by rendering a template, but for such a small bit of HTML, it is easier to have # it all in one place: here output = f'''
-
{date}
+
{date}
{author}, {others}
{place} - {title}
{entry} @@ -119,14 +141,54 @@ def logbookedit(request, year=None): else: form = LogbookEditForm() - return render( - request, - "logbookform.html", - { - "form": form, - "year": year, - }, - ) + if slug: + lbes = LogbookEntry.objects.filter(slug=slug) + if lbes: + if len(lbes) > 1: + return render(request, "object_list.html", {"object_list": lbe}) # ie a bug + else: + lbe = lbes[0] + print(f"{lbe}") + tu = clean_tu(lbe.time_underground) + + people = [] + for p in lbe.personlogentry_set.filter(logbook_entry=lbe): + if p.is_logbook_entry_author: + author = p.personexpedition.person.fullname + else: + people.append(p.personexpedition.person.fullname) + others =', '.join(people) + lenothers = min(70,max(20, len(others))) + print(f"{lenothers}") + text = lbe.text + rows = max(5,len(text)/50) + return render( + request, + "logbookform.html", + { + "form": form, + "year": year, + "date": lbe.date.isoformat(), + "author": author, + "others": others, + "lenothers": lenothers, + "place": lbe.place, + "title": lbe.title.replace(f"{lbe.place} - ",""), + "tu": tu, + "entry": text, + "textrows": rows, + #"output": output, + }, + ) + else: + return render( + request, + "logbookform.html", + { + "form": form, + "year": year, + }, + ) diff --git a/templates/logbookentry.html b/templates/logbookentry.html index 2fdfb41..4185512 100644 --- a/templates/logbookentry.html +++ b/templates/logbookentry.html @@ -67,6 +67,7 @@ {% for personlogentry in logbookentry.personlogentry_set.all %}{% if personlogentry.is_logbook_entry_author %}
{{personlogentry.personexpedition.person}}{% endif %}{% endfor %}

{{logbookentry.text|safe}}

+

Edit this entry. diff --git a/templates/logbookform.html b/templates/logbookform.html index bd21e5d..f9b12ea 100644 --- a/templates/logbookform.html +++ b/templates/logbookform.html @@ -40,10 +40,10 @@

+ />



diff --git a/urls.py b/urls.py index b398690..7edc26d 100644 --- a/urls.py +++ b/urls.py @@ -110,7 +110,7 @@ trogglepatterns = [ path('dwguploadnogit/', dwgupload, {'gitdisable': 'yes'}, name='dwguploadnogit'), # used in testing path('dwguploadnogit/', dwgupload, {'gitdisable': 'yes'}, name='dwguploadnogit'), # used in testing path('logbookedit/', logbookedit, name='logbookedit'), - path('logbookedit/', logbookedit, name='logbookedit'), # year=2023 + path('logbookedit/', logbookedit, name='logbookedit'), # Renaming an uploaded file path('expofilerename/', expofilerename, name='expofilerename'),