mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-17 23:17:11 +00:00
nearly done cave edit commit thing with cookie
This commit is contained in:
@@ -20,7 +20,7 @@ from troggle.core.forms import CaveForm, EntranceForm, EntranceLetterForm # Cav
|
||||
from troggle.core.models.caves import Cave, CaveAndEntrance, Entrance, GetCaveLookup, get_cave_leniently
|
||||
from troggle.core.models.logbooks import QM
|
||||
from troggle.core.models.wallets import Wallet
|
||||
from troggle.core.utils import current_expo, write_and_commit
|
||||
from troggle.core.utils import COOKIE_MAX_AGE, WriteAndCommitError, current_expo, git_string, write_and_commit
|
||||
from troggle.core.views import expo
|
||||
from troggle.parsers.caves import read_cave, read_entrance
|
||||
from troggle.settings import CAVEDESCRIPTIONS, ENTRANCEDESCRIPTIONS
|
||||
@@ -400,12 +400,14 @@ def cavepage(request, karea=None, subpath=None):
|
||||
kpath = karea + subpath
|
||||
#print(f" ! cavepage:'{kpath}' kataster area:'{karea}' rest of path:'{subpath}'")
|
||||
|
||||
caves = Cave.objects.filter(url=kpath)
|
||||
# replace this with .count()
|
||||
caves = Cave.objects.filter(url=kpath)
|
||||
if len(caves) == 1:
|
||||
cave = caves[0]
|
||||
return rendercave(request, cave, cave.slug())
|
||||
|
||||
|
||||
# HORRIBLE HACK, to be removed..
|
||||
subpath = subpath.strip("//")
|
||||
# re do all this using pathlib functions
|
||||
parts = subpath.strip("/").split("/")
|
||||
@@ -453,12 +455,19 @@ def cavepage(request, karea=None, subpath=None):
|
||||
def edit_cave(request, path="", slug=None):
|
||||
"""This is the form that edits all the cave data and writes out an XML file in the :expoweb: repo folder
|
||||
The format for the file being saved is in templates/dataformat/cave.xml
|
||||
Warning. This uses Django deep magic in the CaveForm processing.
|
||||
|
||||
It saves the data into into the database and into the html file, which it then commits to git.
|
||||
|
||||
We basically ignore the <path> as the <slug> is of the format 1624-114 and contains the area code
|
||||
|
||||
Warning. This uses Django deep magic in the CaveForm processing.
|
||||
See https://docs.djangoproject.com/en/5.1/topics/forms/modelforms/
|
||||
https://django-formset.fly.dev/styling/
|
||||
which generates the HTML form fields and also manages the syntax validation.
|
||||
|
||||
See class CaveForm(ModelForm) in troggle/core/forms.py
|
||||
"""
|
||||
|
||||
print(f"edit_cave(): {path=} {slug=}")
|
||||
message = ""
|
||||
if slug is None:
|
||||
@@ -467,11 +476,17 @@ def edit_cave(request, path="", slug=None):
|
||||
print(f"{slug=}")
|
||||
if not (cave:= get_cave_from_slug(slug)): # walrus operator
|
||||
return render(request, "errors/badslug.html", {"badslug": f"for cave {caveslug} - from edit_cave()"})
|
||||
|
||||
print(f"Reading cookie...")
|
||||
editor_id = request.COOKIES.get('editor_id', 'Höhlenforscher <hohlenforscher@stonebridge.expo>') # if no cookie, then default string
|
||||
editor = git_string(editor_id) # belt and braces, should have been validity checked on saving already
|
||||
print(f"Cookie read: {editor_id=} reformatted as: {editor=}")
|
||||
|
||||
if request.POST:
|
||||
form = CaveForm(request.POST, instance=cave)
|
||||
if form.is_valid():
|
||||
print(f'edit_cave(): POST is valid. Editing {cave}')
|
||||
editor = form.cleaned_data["who_are_you"]
|
||||
cave = form.save(commit=False)
|
||||
# print(cave)
|
||||
if not cave.filename:
|
||||
@@ -486,6 +501,14 @@ def edit_cave(request, path="", slug=None):
|
||||
cs = CaveSlug(cave=cave, slug=slug, primary=True)
|
||||
print(f"edit_cave(): New CaveSlug saved {slug}")
|
||||
cs.save()
|
||||
|
||||
if cave.entrances().count() > 0:
|
||||
# Redirect after POST
|
||||
edit_response = HttpResponseRedirect("/" + cave.url)
|
||||
else:
|
||||
edit_response = HttpResponseRedirect(reverse("newentrance", args = [cave.url_parent(), cave.slug()]))
|
||||
edit_response.set_cookie('editor_id', editor, max_age=COOKIE_MAX_AGE) # cookie expires after COOKIE_MAX_AGE seconds
|
||||
|
||||
try:
|
||||
cave_file = cave.file_output()
|
||||
write_and_commit([cave_file], f"Online edit of cave {cave}")
|
||||
@@ -493,16 +516,18 @@ def edit_cave(request, path="", slug=None):
|
||||
except PermissionError:
|
||||
message = f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {cave.filename}. Ask a nerd to fix this."
|
||||
return render(request, "errors/generic.html", {"message": message})
|
||||
except subprocess.SubprocessError:
|
||||
message = f"CANNOT git on server for this file {cave.filename}. Edits may not be committed.\nAsk a nerd to fix this."
|
||||
return render(request, "errors/generic.html", {"message": message})
|
||||
except WriteAndCommitError as e:
|
||||
message = f"CANNOT git on server for this file {cave.filename}.\n{e}\nEdits may not be committed.\nAsk a nerd to fix this."
|
||||
return render(request, "errors/generic.html", {"message": e.message})
|
||||
except subprocess.SubprocessError as e:
|
||||
message = f"CANNOT update server for this file {cave.filename}.\n{e}\nEdits may not be committed.\nAsk a nerd to fix this."
|
||||
return render(request, "errors/generic.html", {"message": message})
|
||||
except:
|
||||
raise
|
||||
if cave.entrances().count() > 0:
|
||||
return HttpResponseRedirect("/" + cave.url)
|
||||
else:
|
||||
return HttpResponseRedirect(reverse("newentrance", args = [cave.url_parent(), cave.slug()]))
|
||||
print(f"Returning response now, which should set cookie on client browser")
|
||||
return edit_response
|
||||
|
||||
# if a GET; and also falls-through from the POST handling to refresh the page
|
||||
else:
|
||||
if slug is not None:
|
||||
# re-read cave data from file.
|
||||
@@ -515,9 +540,9 @@ def edit_cave(request, path="", slug=None):
|
||||
print(f"edit_cave(): EXCEPTION attempting to read_cave({cave.filename})\n{e}")
|
||||
raise
|
||||
|
||||
form = CaveForm(instance=cave, initial={'cave_slug': cave.slug()})
|
||||
form = CaveForm(instance=cave, initial={'cave_slug': cave.slug(), "who_are_you":editor})
|
||||
else:
|
||||
form = CaveForm()
|
||||
form = CaveForm(initial={"who_are_you":editor})
|
||||
|
||||
# The way formsets are rendered changed between Django 4 and Django 5
|
||||
major, _, _, _, _ = django.VERSION
|
||||
@@ -622,6 +647,11 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
|
||||
print(f"{cave=}")
|
||||
imgpath = Path(path) / cave.areacode / cave.number()
|
||||
print(f"Edit Entrance {imgpath=}")
|
||||
|
||||
print(f"Reading cookie...")
|
||||
editor_id = request.COOKIES.get('editor_id', 'Hohlenforscher <hohlenforscher@stonebridge.expo>') # if no cookie, then default string
|
||||
editor = git_string(editor_id) # belt and braces, should have been validity checked on saving already
|
||||
print(f"Cookie read: {editor_id=} reformatted as: {editor=}")
|
||||
|
||||
if request.POST:
|
||||
print(f"POST Online edit of entrance: '{entrance}' where {cave=}")
|
||||
@@ -656,6 +686,7 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
|
||||
else:
|
||||
|
||||
print(f"- POST {caveslug=} {entslug=} {entranceletter=} {path=}")
|
||||
editor = entform.cleaned_data["who_are_you"]
|
||||
if entslug is None:
|
||||
# we are creating a new entrance
|
||||
entrance = entform.save(commit=False)
|
||||
@@ -702,7 +733,7 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
|
||||
|
||||
print(f"- POST WRITE letter: '{ce}' {entrance=}")
|
||||
try:
|
||||
write_and_commit([entrance_file, cave_file], f"Online edit of entrance {entrance.slug}")
|
||||
write_and_commit([entrance_file, cave_file], f"Online edit of entrance {entrance.slug}", editor)
|
||||
return HttpResponseRedirect("/" + cave.url)
|
||||
except Exception as e:
|
||||
efilepath, econtent, eencoding = entrance_file
|
||||
|
||||
Reference in New Issue
Block a user