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<br /><br />\n') # replace 2 \n with <br><br> - entry = entry.replace('<p','<br /><br') # replace <p> tag, even if it has attributes, with <br><br> + entry = entry.replace('\n\n','\n<br />\n<br />\n') # replace 2 \n with <br><br> + entry = entry.replace('<p','<br />\n<br') # replace <p> tag, even if it has attributes, with <br><br> entry = entry.replace('<br>','<br />') # 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''' <hr /> - <div class="tripdate" id="{date}-{seq}">{date}</div> + <div class="tripdate" id="{date}-{uniq}">{date}</div> <div class="trippeople"><u>{author}</u>, {others}</div> <div class="triptitle">{place} - {title}</div> {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 %}<br />{{personlogentry.personexpedition.person}}{% endif %}{% endfor %} <p>{{logbookentry.text|safe}}</p> </div> + <p><a href="/logbookedit/{{logbookentry.slug|safe}}">Edit this entry</a>. </div> </div> 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 @@ <br /><br /> <label for="others">Other names (comma separated) <a href="/aliases/{{year}}">[valid aliases]</a></label> <input {% if not user.username %} disabled{% endif %} - label = "others" name = "others" size="20" + label = "others" name = "others" size="{% if lenothers %}{{lenothers}}{% else %}20{% endif %}" title="Everyone else involved" {% if others %}value="{{others}}"{% else %}placeholder="Phil T, Chas, Planc" {% endif %} - required /> + /> <br /><br /> <label for="place">Place: cave name, or 'plateau', 'topcamp' etc.</label> <input {% if not user.username %} disabled{% endif %} @@ -60,7 +60,7 @@ required /> <br /><br /> <textarea {% if not user.username %} disabled{% endif %} - rows="5" cols="60" + rows="{% if textrows%}{{textrows}}{% else %}5{% endif %}" cols="70" label = "" name = "text" required />{% if entry %}{{entry}}{% else %}We had a lot of fun...{% endif %} </textarea> 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/<path:folder>', dwgupload, {'gitdisable': 'yes'}, name='dwguploadnogit'), # used in testing path('logbookedit/', logbookedit, name='logbookedit'), - path('logbookedit/<int:year>', logbookedit, name='logbookedit'), # year=2023 + path('logbookedit/<slug:slug>', logbookedit, name='logbookedit'), # Renaming an uploaded file path('expofilerename/<path:filepath>', expofilerename, name='expofilerename'),