2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-25 08:41:51 +00:00

validate year in logbook entry form

This commit is contained in:
Philip Sargent 2023-09-05 17:19:09 +03:00
parent 220e1327d7
commit 8e2990ca7a
3 changed files with 44 additions and 33 deletions

View File

@ -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"""

View File

@ -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
@ -75,6 +77,13 @@ def alphabet_suffix(n):
suffix = "_X_" + random.choice(string.ascii_lowercase) + random.choice(string.ascii_lowercase)
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"""
git = settings.GIT

View File

@ -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":
@ -207,6 +227,7 @@ def logbookedit(request, year=None, slug=None):
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()
year = validate_year(year)
if slug:
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):