From 410a285085c85838f7f194b66f1f8a750d912006 Mon Sep 17 00:00:00 2001 From: Martin Green Date: Wed, 5 Jul 2023 17:43:57 +0100 Subject: [PATCH] Bug fixes to allow adding of a new cave. No longer ask users about filenames or urls. --- core/forms.py | 20 ++++++++++++-------- core/models/caves.py | 11 +++++++---- core/utils.py | 10 ++++++---- core/views/caves.py | 8 +++++++- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/core/forms.py b/core/forms.py index a2ea45b..f6fdda6 100644 --- a/core/forms.py +++ b/core/forms.py @@ -61,12 +61,12 @@ class CaveForm(ModelForm): survex_file = forms.CharField( required=False, label="Survex file eg. caves-1623/000/000.svx", widget=forms.TextInput(attrs={"size": "45"}) ) - url = forms.CharField(required=True, label="URL eg. 1623/000/000 (no .html)", widget=forms.TextInput(attrs={"size": "45"})) + #url = forms.CharField(required=True, label="URL eg. 1623/000/000 (no .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) + #cave_slug = forms.CharField() class Meta: model = Cave @@ -74,8 +74,12 @@ class CaveForm(ModelForm): 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 get_area(self): + for a in self.cleaned_data["area"]: + if a.kat_area(): + return a.kat_area() + 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"]: @@ -91,11 +95,11 @@ class CaveForm(ModelForm): 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_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): diff --git a/core/models/caves.py b/core/models/caves.py index 0c8fdad..7ab5d46 100644 --- a/core/models/caves.py +++ b/core/models/caves.py @@ -144,12 +144,15 @@ class Cave(TroggleModel): def ours(self): return bool(re.search(r"CUCC", self.explorers)) + + def number(self): + if self.kataster_number: + return self.kataster_number + else: + return self.unofficial_number def reference(self): - if self.kataster_number: - return f"{self.kat_area()}-{self.kataster_number}" - else: - return f"{self.kat_area()}-{self.unofficial_number}" + return f"{self.kat_area()}-{self.number()}" def get_absolute_url(self): if self.kataster_number: diff --git a/core/utils.py b/core/utils.py index 38ae026..36f0982 100644 --- a/core/utils.py +++ b/core/utils.py @@ -2,6 +2,7 @@ import logging import random import resource import subprocess +import os from decimal import getcontext from pathlib import Path @@ -108,7 +109,7 @@ def write_and_commit(files, message): filename = filepath.name # GIT see also core/views/uploads.py dwgupload() # GIT see also core/views/expo.py editexpopage() - + os.makedirs(os.path.dirname(filepath), exist_ok = True) if encoding: mode = "w" kwargs = {"encoding": encoding} @@ -144,10 +145,11 @@ def write_and_commit(files, message): ) else: print(f"No change {filepath}") - subprocess.run([git, "commit", filename, "-m", message], cwd=cwd, capture_output=True, text=True) - cp_status = subprocess.run([git, "status", filename], cwd=cwd, capture_output=True, text=True) + filenames = [filepath.name for filepath, content, encoding in files] + subprocess.run([git, "commit"] + filenames + ["-m", message], cwd=cwd, capture_output=True, text=True) + cp_status = subprocess.run([git, "status"] + filenames, cwd=cwd, capture_output=True, text=True) #This produces return code = 1 if it commits OK, but when the repo still needs to be pushed to origin/expoweb - if cp_status.stdout.split("\n")[-2] != "nothing to commit, working tree clean": + if not cp_status.stdout or len(cp_status.stdout) < 2 or cp_status.stdout.split("\n")[-2] != "nothing to commit, working tree clean": msgdata = ( "Ask a nerd to fix this.\n\n" + cp_status.stderr diff --git a/core/views/caves.py b/core/views/caves.py index db47a74..187517c 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -268,6 +268,8 @@ def rendercave(request, cave, slug, cave_id=""): svxstem = Path(settings.SURVEX_DATA) / Path(cave.survex_file) # print(f" ! rendercave: slug:'{slug}' '' ++ '{svxstem}'") except: + svx3d = "" + svxstem = "" print(f" ! rendercave: slug:'{slug}' FAIL TO MANAGE survex file:'{cave.survex_file}'") # NOTE the template itself loads the 3d file using javascript before it loads anything else. # Django cannot see what this javascript is doing, so we need to ensure that the 3d file exists first. @@ -374,10 +376,14 @@ def edit_cave(request, path="", slug=None): if form.is_valid() and ceFormSet.is_valid(): # print(f'! POST is valid. {cave}') cave = form.save(commit=False) + if not cave.filename: + cave.filename = form.get_area() + "-" + cave.number() + ".html" + if not cave.url: + cave.url = form.get_area() + "/" + cave.number() + ".html" cave.save() form.save_m2m() if slug is None: - cs = CaveSlug(cave=cave, slug=form.cleaned_data["cave_slug"], primary=True) + cs = CaveSlug(cave=cave, slug=cave.reference(), primary=True) cs.save() ceinsts = ceFormSet.save(commit=False) for ceinst in ceinsts: