From 9fd86dc0c4576259028d2666c5360d1d60d380b0 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 13 Feb 2025 17:30:40 +0000 Subject: cookie and logon working nicely together: cave & entrance --- core/forms.py | 9 +++++++-- core/views/caves.py | 24 ++++++++++++++++-------- templates/editentrance.html | 11 +++++++---- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/core/forms.py b/core/forms.py index a5e6b1a..0bcd6ef 100644 --- a/core/forms.py +++ b/core/forms.py @@ -45,6 +45,7 @@ todo = """ class CaveForm(ModelForm): """Only those fields for which we want to override defaults are listed here the other fields of the class Cave are present on the form, but use the default presentation style + Extra fields, not in the model Cave, are also created here, e.g. who_are_you see https://docs.djangoproject.com/en/5.1/topics/forms/modelforms/ """ @@ -106,6 +107,7 @@ class CaveForm(ModelForm): subarea = forms.CharField(required=False, label="Subarea (do not use for new caves)", widget=forms.TextInput(attrs={"placeholder": "usually blank, archaic"})) #cave_slug = forms.CharField() + identified_login = forms.BooleanField(required=False,widget=forms.CheckboxInput(attrs={"onclick":"return false"})) # makes it readonly who_are_you = forms.CharField( widget=forms.TextInput( attrs={"size": 100, "placeholder": "You are editing this page, who are you ? e.g. 'Becka' or 'Animal '", @@ -119,7 +121,7 @@ class CaveForm(ModelForm): field_order = ['unofficial_number', 'kataster_number', 'official_name', 'underground_description', 'survey', 'underground_centre_line', 'explorers', 'equipment', 'notes', 'references', 'description_file', 'survex_file', 'areacode', 'subarea', 'length', 'depth', 'extent', - 'kataster_code', 'kataster_status', 'fully_explored', 'non_public', 'who_are_you'] + 'kataster_code', 'kataster_status', 'fully_explored', 'non_public', 'identified_login', 'who_are_you'] def clean_cave_slug(self): if self.cleaned_data["cave_slug"] == "": @@ -232,6 +234,7 @@ class EntranceForm(ModelForm): ) alt = forms.CharField(required=False, label="Altitude (m) - from GPS if you have it, but let it settle.") # url = forms.CharField(required=False, label="URL [usually blank]", widget=forms.TextInput(attrs={"size": "45"})) + identified_login = forms.BooleanField(required=False,widget=forms.CheckboxInput(attrs={"onclick":"return false"})) # makes it readonly who_are_you = forms.CharField( widget=forms.TextInput( attrs={"size": 100, "placeholder": "You are editing this page, who are you ? e.g. 'Becka' or 'Animal '", @@ -241,7 +244,7 @@ class EntranceForm(ModelForm): field_order = ['name', 'entrance_description', 'explorers', 'map_description', 'location_description', 'lastvisit', 'non_public', 'findability', 'marking', 'approach', 'underground_description', 'photo', 'marking_comment', 'findability_description', 'other_description', - 'bearings', 'tag_station', 'other_station', 'easting', 'northing', 'lat_wgs84', 'long_wgs84', 'alt', 'who_are_you'] + 'bearings', 'tag_station', 'other_station', 'easting', 'northing', 'lat_wgs84', 'long_wgs84', 'alt', 'identified_login', 'who_are_you'] class Meta: model = Entrance @@ -274,6 +277,8 @@ class EntranceLetterForm(ModelForm): to only one Cave. see https://docs.djangoproject.com/en/5.1/topics/forms/modelforms/ + + To be re-written when we move the 'letter' field into Entrance """ # This only needs to be required=True for the second and subsequent entrances, not the first. Tricky. diff --git a/core/views/caves.py b/core/views/caves.py index 75971bb..c4f5a4c 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -24,9 +24,9 @@ from troggle.core.utils import ( COOKIE_MAX_AGE, WriteAndCommitError, current_expo, - get_cookie, - git_string, + get_editor, write_and_commit, + is_identified_user, ) from troggle.core.views import expo from troggle.parsers.caves import read_cave, read_entrance @@ -493,7 +493,8 @@ def edit_cave(request, path="", slug=None): if not (cave:= get_cave_from_slug(slug)): # walrus operator return render(request, "errors/badslug.html", {"badslug": f"for cave {slug} - from edit_cave()"}) - editor = get_cookie(request) + identified_login = is_identified_user(request.user) + editor = get_editor(request) if request.POST: form = CaveForm(request.POST, instance=cave) @@ -554,9 +555,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(), "who_are_you":editor}) + form = CaveForm(instance=cave, initial={'cave_slug': cave.slug(), "identified_login": identified_login, "who_are_you":editor}) else: - form = CaveForm(initial={"who_are_you":editor}) + form = CaveForm(initial={"identified_login": identified_login, "who_are_you":editor}) # The way formsets are rendered changed between Django 4 and Django 5 major, _, _, _, _ = django.VERSION @@ -565,6 +566,9 @@ def edit_cave(request, path="", slug=None): else: tabletype = "div" + if identified_login: + # disable editing the git id string as we get it from the logged-on user data + form.fields["who_are_you"].widget.attrs["readonly"]="readonly" return render( request, "editcave.html", @@ -662,7 +666,8 @@ def edit_entrance(request, path="", caveslug=None, entslug=None): imgpath = Path(path) / cave.areacode / cave.number() print(f"Edit Entrance {imgpath=}") - editor = get_cookie(request) + identified_login = is_identified_user(request.user) + editor = get_editor(request) if request.POST: print(f"POST Online edit of entrance: '{entrance}' where {cave=}") @@ -771,7 +776,7 @@ def edit_entrance(request, path="", caveslug=None, entslug=None): # ent only in db not on file. Interesting, let's run with it using whatever we have in the db print(f"ENTRANCE NOT read from file: entranceletter = '{ce.entranceletter}'") - entform = EntranceForm(instance=entrance, initial={"who_are_you":editor}) + entform = EntranceForm(instance=entrance, initial={"identified_login": identified_login, "who_are_you":editor}) if entslug is None: entletterform = EntranceLetterForm() # print(f" Getting entletter from EntranceLetterForm") @@ -782,9 +787,12 @@ def edit_entrance(request, path="", caveslug=None, entslug=None): print(f" Blank value: getting entletter from EntranceLetterForm") print(f"{entletter=} ") else: - entform = EntranceForm(initial={"who_are_you":editor}) + entform = EntranceForm(initial={"identified_login": identified_login, "who_are_you":editor}) entletterform = EntranceLetterForm() + if identified_login: + # disable editing the git id string as we get it from the logged-on user data + entform.fields["who_are_you"].widget.attrs["readonly"]="readonly" return render( request, "editentrance.html", diff --git a/templates/editentrance.html b/templates/editentrance.html index 957351f..d9957a0 100644 --- a/templates/editentrance.html +++ b/templates/editentrance.html @@ -35,16 +35,19 @@ at troggle/core/forms.py ass this uses a Django magic form creation thinggy. -->
  • Read the brief explanation of location data in the handbook. +{% if ent.bearings %} +
    + +{{ent.bearings|safe}} +{% endif %} + {% if entlettereditable %} {{ entletterform }}
    {% else %}
    Entrance Letter{{ entletter }}
    {% endif %} {{ entform }} -{% if ent.bearings %} - -{% endif %} +
    -{{ent.bearings|safe}}

    -- cgit v1.2.3