From 7dbaea5d24feff4cfa166bbea0a15acb27bb19f9 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Fri, 9 Aug 2024 00:58:40 +0300 Subject: [PATCH] forbid changing year when editing existing logbook entry --- core/views/uploads.py | 35 ++++++++++++++++++++++++----------- templates/logbookform.html | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/core/views/uploads.py b/core/views/uploads.py index ae8e69a..0dcc7f3 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -311,17 +311,19 @@ def logbookedit(request, year=None, slug=None): if "prev_slug" in request.POST: prev_slug = request.POST["prev_slug"].strip() # if we are re-editing the same entry again entry = entry.replace('\r','') # remove HTML-standard CR inserted from form. - entry = entry.replace('\n\n','\n

\n') # replace 2 \n with

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

- # entry = entry.replace('

','
\n tag with

- # entry = entry.replace('

\n tag with attributes with

- # entry = entry.replace('
','
') # clean up previous hack + entry = entry.replace('\n\n','\n

\n') # replace 2 \n with

tu = request.POST["tu"].strip() tu = clean_tu(tu) try: odate = datetime.strptime(date.replace(".", "-"), "%Y-%m-%d").date() - dateflag = False + print(f"{odate.year=}") + if str(odate.year) == year: + dateflag = False + else: + print(f"Trying to change the year ! No!! {odate.year=}") + odate = datetime.strptime(f"{year}-01-01", "%Y-%m-%d").date() + dateflag = True except: odate = datetime.strptime(f"{year}-01-01", "%Y-%m-%d").date() print(f"! Invalid date string {date}, setting to {odate}") @@ -469,7 +471,10 @@ def logbookedit(request, year=None, slug=None): # the next time this code is run it thinks a new slug needs to be created. So we should # actually redirect to a new URL (an edit not a create) not simply return a render object. # logbookedit/2022-08-21a - return HttpResponseRedirect(f'/logbookedit/{slug}') + + # HOWEVER by doing a redirect rather than returning a rendered page, we lose all the + # error settings e.g dateflag and authroflag so the user gets no feedback about bad data entered. + return HttpResponseRedirect(f"/logbookedit/{slug}?dateflag={dateflag}&authorflag={authorflag}") # return render( # request, @@ -492,6 +497,16 @@ def logbookedit(request, year=None, slug=None): else: form = LogbookEditForm() year = validate_year(year) + + if request.GET.get('dateflag', 'False') == "True": + dateflag = True + else: + dateflag = False + + if request.GET.get('authorflag', 'False') == "True": + authorflag = True + else: + authorflag = False if not slug: # no slug or bad slug for an lbe which does not exist return new_entry_form() @@ -516,11 +531,9 @@ def logbookedit(request, year=None, slug=None): # people.append(p.personexpedition.person.fullname) people.append(p.nickname_used) others =', '.join(people) - print(f"{others=}") if lbe.other_people: others = others + ", " + lbe.other_people lenothers = min(70,max(20, len(others))) - print(f"{others=}") text = lbe.text rows = max(5,len(text)/50) @@ -530,8 +543,8 @@ def logbookedit(request, year=None, slug=None): { "form": form, "year": year, - "date": lbe.date.isoformat(), - "author": author, + "date": lbe.date.isoformat(), "dateflag": dateflag, + "author": author, "authorflag": authorflag, "others": others, "lenothers": lenothers, "place": lbe.place, diff --git a/templates/logbookform.html b/templates/logbookform.html index ebc5111..4209e87 100644 --- a/templates/logbookform.html +++ b/templates/logbookform.html @@ -32,7 +32,7 @@ - +