From d08a6aa204144e20e1e697fde3f6444edce814f1 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sat, 26 Aug 2023 18:39:29 +0300 Subject: Nicknames preserved, date checked --- core/views/uploads.py | 56 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) (limited to 'core/views/uploads.py') diff --git a/core/views/uploads.py b/core/views/uploads.py index e7675b8..a4f5d9c 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -1,5 +1,6 @@ import subprocess import hashlib +from datetime import datetime from pathlib import Path from django import forms @@ -20,6 +21,11 @@ and that core/forms.py contains Django class-based forms for caves and entrances """ todo = """ +- munge the URL of images in the logbook entry so that they work from both the " /logbookedit/" page, + the logbook /years/2023/ page and the logbook fragment page /logbookentry// + Note that this munging has already been done when the entry is imported into the database, so + when doing an online edit it has already been fixed. + - Ideally we should validate uploaded file as being a valid file type, not a dubious script or hack Validate image files using a magic recogniser in walletedit() https://pypi.org/project/reportlab/ or @@ -62,15 +68,11 @@ class LogbookEditForm(forms.Form): # not a model-form, just a form-form @login_required_if_public 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. + This is daft: we have the parsed identity of the person and we render it to text as 'nickname_used' + (or, previously, 'fullname'), to be re-parsed on re-importing. + And there is no guarantee that this will be the same thing. - 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. - Fix is to add "alias_used" as a field in class PersonLogEntry, so that we can preserve - all those cute names. But it's rather a large manual effort (with some scripting) to recover the aliases from the original logbook - html files which are now only in the git history. Bother. Very sorry. + Someone can put in a nickname which is invalid (e.g. 2 Sophies on expo). When is this checked? """ def clean_tu(tu): if tu =="": @@ -89,12 +91,19 @@ def logbookedit(request, year=None, slug=None): 2 hex digits would seem adequate for each expo day, but we might get a collision. The hash is based on the content after substitution of

so should be stable. Which means these ids can be used elsewhere in the troggle system as permanent slugs. + + When SAVING an edited entry (as opposed to a new one) we will have a different hash so we will have to + delete the original database object """ sha.update(text.encode('utf-8')) return sha.hexdigest()[0:n] if not year: - year = 2023 + if not slug: + year = 2023 + else: + year = slug[0:4] + print(year) if request.method == "POST": form = LogbookEditForm(request.POST) @@ -116,13 +125,26 @@ def logbookedit(request, year=None, slug=None): entry = entry.replace('
','
') # clean up previous hack tu = request.POST["tu"].strip() tu = clean_tu(tu) - uniq = unique_id(entry,2) - print(uniq) + + try: + odate = datetime.strptime(date.replace(".", "-"), "%Y-%m-%d").date() + dateflag = False + except: + odate = datetime.strptime(f"{year}-01-01", "%Y-%m-%d").date() + print(f"! Invalid date string {date}, setting to {odate}") + dateflag = True + date = odate.isoformat() + + newslug = f"{date}_{unique_id(entry,2)}" + if slug: + if slug != newslug: + print(f"! Entry id changed! from {slug} to {newslug}") + # 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} @@ -133,7 +155,7 @@ def logbookedit(request, year=None, slug=None): { "form": form, "year": year, - "date": date, + "date": date, "dateflag": dateflag, "author": author, "others": others, "place": place, @@ -158,11 +180,13 @@ def logbookedit(request, year=None, slug=None): tu = clean_tu(lbe.time_underground) people = [] - for p in lbe.personlogentry_set.filter(logbook_entry=lbe): + for p in lbe.personlogentry_set.filter(logbook_entry=lbe): # p is a PersonLogEntry object if p.is_logbook_entry_author: - author = p.personexpedition.person.fullname + # author = p.personexpedition.person.fullname + author = p.nickname_used else: - people.append(p.personexpedition.person.fullname) + # people.append(p.personexpedition.person.fullname) + people.append(p.nickname_used) others =', '.join(people) lenothers = min(70,max(20, len(others))) print(f"{lenothers}") -- cgit v1.2.3