From 82aaa2b523be876aeed2e0caad7165701563a979 Mon Sep 17 00:00:00 2001 From: Martin Green Date: Sun, 30 Apr 2023 19:00:22 +0100 Subject: [PATCH] Improved cave form, layout text and validations --- core/forms.py | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/core/forms.py b/core/forms.py index 8d3ea61..5cf2735 100644 --- a/core/forms.py +++ b/core/forms.py @@ -6,7 +6,10 @@ from django.forms.models import modelformset_factory from troggle.core.models.caves import Cave, CaveAndEntrance, Entrance from troggle.core.views.editor_helpers import HTMLarea +from django.core.exceptions import ValidationError + # from tinymce.widgets import TinyMCE +import re """These are all the class-based Forms used by troggle. @@ -54,31 +57,60 @@ class CaveForm(ModelForm): required=False, widget=HTMLarea(attrs={"height": "80%", "rows": 20, "placeholder": "Enter page content (using HTML)"}), ) - description_file = forms.CharField(required=False, widget=forms.TextInput(attrs={"size": "45"})) + description_file = forms.CharField(required=False, label="Path of top-level description file for this cave, when a separate file is used. Otherwise blank.", widget=forms.TextInput(attrs={"size": "45"}), help_text="") survex_file = forms.CharField( - required=False, label="Survex file [caves-1623/000/000.svx]", widget=forms.TextInput(attrs={"size": "45"}) + required=False, label="Survex file eg. caves-1623/000/000.svx", widget=forms.TextInput(attrs={"size": "45"}) ) - url = forms.CharField(required=True, label="URL [1623/000/000]", widget=forms.TextInput(attrs={"size": "45"})) + url = forms.CharField(required=True, label="URL eg. 1623/000/000.html", widget=forms.TextInput(attrs={"size": "45"})) length = forms.CharField(required=False, label="Length (m)") depth = forms.CharField(required=False, label="Depth (m)") extent = forms.CharField(required=False, label="Extent (m)") + + cave_slug = forms.CharField(widget = forms.HiddenInput(), required = False) class Meta: model = Cave exclude = ("filename",) - + + field_order = ['area', 'unofficial_number', 'kataster_number', 'official_name', 'underground_description', 'explorers', 'equipment', 'survey', 'kataster_status', 'underground_centre_line', 'notes', 'references', 'description_file', 'survex_file', 'url', 'length', 'depth', 'extent'] + + def clean_cave_slug(self): + print(self.cleaned_data["cave_slug"] == "") + if self.cleaned_data["cave_slug"] == "": + myArea = "" + for a in self.cleaned_data["area"]: + if a.kat_area(): + myArea = a.kat_area() + if self.data["kataster_number"]: + cave_slug = f"{myArea}-{self.cleaned_data['kataster_number']}" + else: + cave_slug = f"{myArea}-{self.cleaned_data['unofficial_number']}" + else: + cave_slug = self.cleaned_data["cave_slug"] + # Converting a PENDING cave to a real cave by saving this form + print("EEE", cave_slug.replace("-PENDING-", "-")) + return cave_slug.replace("-PENDING-", "-") + + def clean_url(self): + data = self.cleaned_data["url"] + if not re.match("\d\d\d\d/.", data): + raise ValidationError("URL must start with a four digit Kataster area.") + return data + + def clean(self): - if self.cleaned_data.get("kataster_number") == "" and self.cleaned_data.get("unofficial_number") == "": + cleaned_data = super(CaveForm, self).clean() + if self.data.get("kataster_number") == "" and self.data.get("unofficial_number") == "": self._errors["unofficial_number"] = self.error_class( ["Either the kataster or unoffical number is required."] ) # if self.cleaned_data.get("kataster_number") != "" and self.cleaned_data.get("official_name") == "": # self._errors["official_name"] = self.error_class(["This field is required when there is a kataster number."]) - if self.cleaned_data.get("area") == []: + if cleaned_data.get("area") == []: self._errors["area"] = self.error_class(["This field is required."]) - if self.cleaned_data.get("url") and self.cleaned_data.get("url").startswith("/"): + if cleaned_data.get("url") and cleaned_data.get("url").startswith("/"): self._errors["url"] = self.error_class(["This field cannot start with a /."]) - return self.cleaned_data + return cleaned_data class EntranceForm(ModelForm):