mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2026-05-18 11:31:30 +01:00
recognising names not on expo
This commit is contained in:
+106
-13
@@ -1,7 +1,9 @@
|
||||
import re
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from troggle.parsers.people import who_is_this
|
||||
from core.position_utils import which_area # file-type import, not module type.
|
||||
|
||||
class NewHoleForm(forms.Form):
|
||||
@@ -22,9 +24,12 @@ class NewHoleForm(forms.Form):
|
||||
widget=forms.TextInput(attrs={'placeholder': 'e.g. Dour, Animal, Becka'}),
|
||||
max_length=255, required=True)
|
||||
discovery_date = forms.DateField(label="Trip date", widget=forms.DateInput(attrs={'type': 'date'}), required=True)
|
||||
surface_wallet = forms.CharField(label="Wallet used to find the entrance (if any)",
|
||||
surface_wallet = forms.CharField(label="Old wallet used to find the entrance (if any)",
|
||||
widget=forms.TextInput(attrs={'placeholder': 'e.g. 2005 # 63'}),
|
||||
max_length=100, required=False)
|
||||
survey_wallet = forms.CharField(label="New Wallet for all this data (must match date of trip)",
|
||||
widget=forms.TextInput(attrs={'placeholder': 'e.g. 2029 # 88'}),
|
||||
required=True)
|
||||
|
||||
# GPS Data
|
||||
gps_owner = forms.CharField(label="GPS: Whose device?",
|
||||
@@ -56,19 +61,17 @@ class NewHoleForm(forms.Form):
|
||||
# Status & Surveys
|
||||
is_explored = forms.BooleanField(label="Exploration complete?", required=False)
|
||||
ug_survey_done = forms.BooleanField(label="Survex data recorded?", required=False)
|
||||
ug_survey_wallet = forms.CharField(label="Wallet id for all this data",
|
||||
widget=forms.TextInput(attrs={'placeholder': 'e.g. 2029 # 88'}),
|
||||
required=True)
|
||||
|
||||
# Media: Entrance Photo (Replaced dropdown with checkboxes)
|
||||
photo_ent_no = forms.BooleanField(label="Entrance photos ?", required=False)
|
||||
photo_ent_who = forms.CharField(label="Who has entrance photos ?", required=False)
|
||||
|
||||
# Media: Tag Photo (Replaced dropdown with checkboxes)
|
||||
photo_tag_no = forms.BooleanField(label="Photos of tag ?", required=False)
|
||||
photo_tag_who = forms.CharField(label="Who has tag photos ?", required=False)
|
||||
|
||||
photo_ent_who = forms.CharField(label="Who has photos of entrance, tag and GPS?", required=False)
|
||||
|
||||
who_are_you = forms.CharField(strip=True,
|
||||
widget=forms.TextInput(
|
||||
attrs={"size": 100, "placeholder": "You are entering data, who are you ? e.g. 'Becka' or 'Animal <mta@gasthof.expo>'",
|
||||
"style": "vertical-align: text-top;"}
|
||||
)
|
||||
)
|
||||
# VALIDATIONS
|
||||
# Django's validation logic will automatically trigger these.
|
||||
# Trigger: When form.is_valid() is called in your new_hole view, Django automatically looks
|
||||
@@ -86,6 +89,96 @@ class NewHoleForm(forms.Form):
|
||||
# Always call the parent clean() first to get cleaned_data dictionary
|
||||
cleaned_data = super().clean()
|
||||
|
||||
trip_date = cleaned_data.get("discovery_date")
|
||||
wallet_id = cleaned_data.get("survey_wallet")
|
||||
|
||||
if wallet_id and trip_date:
|
||||
try:
|
||||
clean_wallet = "".join(wallet_id.split()) # should already be cleaned
|
||||
wallet_year = int(clean_wallet[:4])
|
||||
|
||||
# Get the year from the discovery_date (a datetime.date object)
|
||||
trip_year = trip_date.year
|
||||
|
||||
if wallet_year != trip_year:
|
||||
# Point the error at the wallet field
|
||||
self.add_error('ug_survey_wallet',
|
||||
f"Year mismatch: Wallet year ({wallet_year}) does not match Trip date year ({trip_year}).")
|
||||
|
||||
except (ValueError, IndexError):
|
||||
# Individual field cleaners (regex) will handle malformed wallet strings
|
||||
pass
|
||||
|
||||
# 2. Extract the year from survey_wallet (YYYY#NN)
|
||||
# We only proceed if wallet_id passed its own validation earlier,
|
||||
# which removes whitespace and checks the year
|
||||
if wallet_id and "#" in wallet_id:
|
||||
year = int(clean_wallet[:4])
|
||||
|
||||
camera_persons = cleaned_data.get("photo_ent_who").split(",")
|
||||
discoverers = cleaned_data.get("discoverers").split(",")
|
||||
gps_owner = cleaned_data.get("gps_owner")
|
||||
|
||||
# 1.Check discoverers are valid people
|
||||
intrepids = []
|
||||
for caver in discoverers:
|
||||
caver = caver.strip()
|
||||
|
||||
try:
|
||||
person_id = who_is_this(year, caver)
|
||||
except (ValueError, IndexError) as e:
|
||||
print(f"ERROR: {caver} {repr(e)}")
|
||||
person_id = None
|
||||
|
||||
if person_id:
|
||||
intrepids.append(person_id)
|
||||
|
||||
else:
|
||||
print(f"unrecognised: {caver} ")
|
||||
error_html = mark_safe(
|
||||
f"'{caver}' is not a recognized explorer for the year {year}. "
|
||||
f"See <a href='/aliases/{year}'>aliases list</a>"
|
||||
)
|
||||
self.add_error('discoverers', error_html)
|
||||
|
||||
# 1.Check photographer(s) are valid people
|
||||
cameramen = []
|
||||
for caver in camera_persons:
|
||||
|
||||
try:
|
||||
person_id = who_is_this(year, caver)
|
||||
except (ValueError, IndexError) as e:
|
||||
print(f"ERROR: {caver} {repr(e)}")
|
||||
person_id = None
|
||||
|
||||
if person_id:
|
||||
cameramen.append(person_id)
|
||||
|
||||
else:
|
||||
print(f"unrecognised: {caver} ")
|
||||
error_html = mark_safe(
|
||||
f"'{caver}' is not a recognized explorer for the year {year}. "
|
||||
f"See <a href='/aliases/{year}'>aliases list</a>"
|
||||
)
|
||||
self.add_error('photo_ent_who', error_html)
|
||||
|
||||
try:
|
||||
person_id = who_is_this(year, gps_owner)
|
||||
except (ValueError, IndexError) as e:
|
||||
print(f"ERROR: {gps_owner} {repr(e)}")
|
||||
person_id = None
|
||||
|
||||
if person_id:
|
||||
pass
|
||||
else:
|
||||
print(f"unrecognised: {gps_owner} ")
|
||||
error_html = mark_safe(
|
||||
f"'{gps_owner}' is not a recognized explorer for the year {year}. "
|
||||
f"See <a href='/aliases/{year}'>aliases list</a>"
|
||||
)
|
||||
self.add_error('gps_owner', error_html)
|
||||
|
||||
|
||||
# Entrance Photo Logic
|
||||
photo_ent_on_camera = cleaned_data.get("photo_ent_on_camera")
|
||||
gps_screenshot = cleaned_data.get("gps_screenshot")
|
||||
@@ -170,13 +263,13 @@ class NewHoleForm(forms.Form):
|
||||
raise forms.ValidationError("Bearing must be between 0 and 360 degrees.")
|
||||
return bearing
|
||||
|
||||
def clean_ug_survey_wallet(self):
|
||||
data = self.cleaned_data.get('ug_survey_wallet')
|
||||
def clean_survey_wallet(self):
|
||||
data = self.cleaned_data.get('survey_wallet')
|
||||
|
||||
if not data:
|
||||
return data # Skip if empty (assumes required=False)
|
||||
|
||||
# 1. Remove all whitespace
|
||||
# 1. Remove all whitespace, catches tabs etc (\t), (\n)
|
||||
clean_text = "".join(data.split())
|
||||
|
||||
# 2. Check general format using Regex (4 digits, #, 2 digits)
|
||||
|
||||
Reference in New Issue
Block a user