diff --git a/core/models/troggle.py b/core/models/troggle.py index 1ca7a9a..4a20afd 100644 --- a/core/models/troggle.py +++ b/core/models/troggle.py @@ -73,17 +73,6 @@ class Expedition(TroggleModel): def get_absolute_url(self): return urljoin(settings.URL_ROOT, reverse("expedition", args=[self.year])) - -# class ExpeditionDay(TroggleModel): -# """Exists only on Expedition now. Removed links from logbookentry, personlogentry, survex stuff etc. -# """ -# expedition = models.ForeignKey("Expedition",on_delete=models.CASCADE) -# date = models.DateField() - -# class Meta: -# ordering = ('date',) - - class Person(TroggleModel): """single Person, can go on many years""" diff --git a/core/utils.py b/core/utils.py index 6f07df9..8cd94e2 100644 --- a/core/utils.py +++ b/core/utils.py @@ -8,6 +8,8 @@ import subprocess from decimal import getcontext from pathlib import Path +from troggle.core.models.troggle import Expedition + getcontext().prec = 2 # use 2 significant figures for decimal calculations import settings @@ -73,7 +75,14 @@ def alphabet_suffix(n): suffix = alphabet[n-1] else: suffix = "_X_" + random.choice(string.ascii_lowercase) + random.choice(string.ascii_lowercase) - return suffix + return suffix + +def current_expo(): + expos = Expedition.objects.all().order_by('-year') + if expos: + return expos[0].year + else: + return "1970" def only_commit(fname, message): """Only used to commit a survex file edited and saved in view/survex.py""" diff --git a/core/views/uploads.py b/core/views/uploads.py index 67ffb48..232eb5e 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -12,7 +12,7 @@ from troggle.core.models.caves import GetCaveLookup from troggle.core.models.logbooks import LogbookEntry, writelogbook, PersonLogEntry from troggle.core.models.survex import DrawingFile from troggle.core.models.troggle import DataIssue, Expedition, PersonExpedition -from troggle.core.utils import alphabet_suffix +from troggle.core.utils import alphabet_suffix, current_expo from troggle.parsers.people import GetPersonExpeditionNameLookup, known_foreigner # from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time* @@ -158,6 +158,22 @@ def logbookedit(request, year=None, slug=None): No check is done on the other people on the trip as this is picked up anyway by parsing on import and we don't really care at this point. """ + def validate_year(year): + try: + expo = Expedition.objects.get(year=year) + except: + year = current_expo() + return year + + def new_entry_form(): + return render( + request, + "logbookform.html", + { + "form": form, + "year": year, + }, + ) def clean_tu(tu): if tu =="": return 0 @@ -169,10 +185,14 @@ def logbookedit(request, year=None, slug=None): if not year: if not slug: - year = 2023 # we need a CURRENT_EXPO() function, we use this in a lot of places.. + year = current_expo() else: year = slug[0:4] - print(year) + try: + year = str(int(year)) + except: + year = current_expo() + author = "" if request.method == "POST": @@ -206,7 +226,8 @@ def logbookedit(request, year=None, slug=None): print(f"! Invalid date string {date}, setting to {odate}") dateflag = True date = odate.isoformat() - + + year = validate_year(year) expo = Expedition.objects.get(year=year) personyear = GetPersonExpeditionNameLookup(expo).get(author.lower()) if personyear: @@ -328,12 +349,17 @@ def logbookedit(request, year=None, slug=None): # GET here else: form = LogbookEditForm() - - if slug: + year = validate_year(year) + + if not slug: # no slug or bad slug for an lbe which does not exist + return new_entry_form() + else: lbes = LogbookEntry.objects.filter(slug=slug) - if lbes: + if not lbes: + return new_entry_form() + else: if len(lbes) > 1: - return render(request, "object_list.html", {"object_list": lbe}) # ie a bug + return render(request, "object_list.html", {"object_list": lbes}) # ie a bug else: lbe = lbes[0] print(f"{lbe}") @@ -373,19 +399,6 @@ def logbookedit(request, year=None, slug=None): "textrows": rows, }, ) - else: # no slug or bad slug for an lbe which does not exist - # NEW logbook entry - return render( - request, - "logbookform.html", - { - "form": form, - "year": year, - }, - ) - - - @login_required_if_public def expofilerename(request, filepath):