diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2024-12-28 23:49:26 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2024-12-28 23:49:26 +0000 |
commit | dac3e6e2889b1d194069c1b8d1eb98fea6d1f7eb (patch) | |
tree | 8a6f48048fdd5939b940e5d4d09b3051ae2fea07 /core | |
parent | e7444d20a4759cc3b1a9232c5129499ba5e1abfc (diff) | |
download | troggle-dac3e6e2889b1d194069c1b8d1eb98fea6d1f7eb.tar.gz troggle-dac3e6e2889b1d194069c1b8d1eb98fea6d1f7eb.tar.bz2 troggle-dac3e6e2889b1d194069c1b8d1eb98fea6d1f7eb.zip |
Entrances now do cookie / git author thing. + tidyup.
Diffstat (limited to 'core')
-rw-r--r-- | core/forms.py | 27 | ||||
-rw-r--r-- | core/models/caves.py | 2 | ||||
-rw-r--r-- | core/utils.py | 4 | ||||
-rw-r--r-- | core/views/caves.py | 22 | ||||
-rw-r--r-- | core/views/expo.py | 2 | ||||
-rw-r--r-- | core/views/survex.py | 6 |
6 files changed, 40 insertions, 23 deletions
diff --git a/core/forms.py b/core/forms.py index 37795d2..a5e6b1a 100644 --- a/core/forms.py +++ b/core/forms.py @@ -164,11 +164,11 @@ class EntranceForm(ModelForm): ) explorers = forms.CharField(required=False, widget=forms.TextInput(attrs={"size": "45"})) # explorers = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10})) - map_description = forms.CharField( - label="Map (is this used?)", - required=False, - widget=HTMLarea(attrs={"height": "80%", "rows": 20, "placeholder": "Enter text (using HTML)"}), - ) + # map_description = forms.CharField( + # label="Map (is this used?)", + # required=False, + # widget=HTMLarea(attrs={"height": "80%", "rows": 20, "placeholder": "Enter text (using HTML)"}), + # ) location_description = forms.CharField( label="Location", required=False, @@ -232,13 +232,22 @@ 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"})) - - field_order = ['name', 'entrance_description', 'explorers', 'map_description', 'location_description', 'lastvisit', '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 = forms.CharField( + widget=forms.TextInput( + attrs={"size": 100, "placeholder": "You are editing this page, who are you ? e.g. 'Becka' or 'Animal <mta@gasthof.expo>'", + "style": "vertical-align: text-top;"} + ) + ) + + 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'] class Meta: model = Entrance exclude = ( "cached_primary_slug", + "map_description", # No entrance has any data on this field, so it is being retired. "filename", "slug", "bearings" @@ -259,8 +268,10 @@ class EntranceForm(ModelForm): class EntranceLetterForm(ModelForm): """Form to link entrances to caves, along with an entrance number. - Nb. The relationship between caves and entrances has historically been a many to many relationship. + NOTE. The relationship between caves and entrances was originally designed to be a many to many relationship. With entrances gaining new caves and letters when caves are joined. + However, so far as I can see, this was never actually done in practice on Expo and each Entrance belongs + to only one Cave. see https://docs.djangoproject.com/en/5.1/topics/forms/modelforms/ """ diff --git a/core/models/caves.py b/core/models/caves.py index 003db9b..3154acb 100644 --- a/core/models/caves.py +++ b/core/models/caves.py @@ -254,7 +254,7 @@ class Entrance(TroggleModel): lat_wgs84 = models.TextField(blank=True, null=True) # manually entered not calculated location_description = models.TextField(blank=True, null=True) long_wgs84 = models.TextField(blank=True, null=True) # manually entered not calculated - # map_description = models.TextField(blank=True, null=True) + # map_description = models.TextField(blank=True, null=True) # retired marking = models.CharField(max_length=2, choices=MARKING_CHOICES, default="?") marking_comment = models.TextField(blank=True, null=True) name = models.CharField(max_length=100, blank=True, null=True) diff --git a/core/utils.py b/core/utils.py index 5ba1b4b..26d88c6 100644 --- a/core/utils.py +++ b/core/utils.py @@ -218,7 +218,9 @@ def git_string(author_string): return author_string else: editor = author_string.replace("@","_at_") - editor = re.sub('[^0-9a-zA-Z_\.]+', '*', editor) + editor = re.sub('[^0-9a-zA-Z_\.]+', '_', editor) + if editor.startswith("_"): + editor = "X" + editor editor += f" <{editor}@potatohut.expo>" print(f"++ Not git-compatible author string '{author_string}', replacing as '{editor}'") return editor diff --git a/core/views/caves.py b/core/views/caves.py index ba74f7b..a40b314 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -484,6 +484,7 @@ def edit_cave(request, path="", slug=None): if form.is_valid(): print(f'edit_cave(): POST is valid. Editing {cave}') editor = form.cleaned_data["who_are_you"] + editor = git_string(editor) cave = form.save(commit=False) # print(cave) if not cave.filename: @@ -645,10 +646,7 @@ def edit_entrance(request, path="", caveslug=None, entslug=None): 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=}") + editor = get_cookie(request) if request.POST: print(f"POST Online edit of entrance: '{entrance}' where {cave=}") @@ -682,8 +680,9 @@ def edit_entrance(request, path="", caveslug=None, entslug=None): return render(request, "errors/generic.html", {"message": message}) else: - print(f"- POST {caveslug=} {entslug=} {entranceletter=} {path=}") + print(f"'edit_entrance(): POST is valid {caveslug=} Editing {entslug=} {entranceletter=} {path=}") editor = entform.cleaned_data["who_are_you"] + editor = git_string(editor) if entslug is None: # we are creating a new entrance entrance = entform.save(commit=False) @@ -731,14 +730,17 @@ 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}", editor) - return HttpResponseRedirect("/" + cave.url) + edit_response = HttpResponseRedirect("/" + cave.url) + edit_response.set_cookie('editor_id', editor, max_age=COOKIE_MAX_AGE) # cookie expires after COOKIE_MAX_AGE seconds + return edit_response except Exception as e: efilepath, econtent, eencoding = entrance_file cfilepath, ccontent, cencoding = cave_file message = f"- FAIL write_and_commit \n entr:'{efilepath}'\n cave:'{cfilepath}'\n\n{e}" print(message) return render(request, "errors/generic.html", {"message": message}) - + + # Unlike other similar forms, we do NOT drop into this GET code after processing the POST else: # GET the page, not POST, or if either of the forms were invalid when POSTed entletterform = None entletter = "" @@ -753,7 +755,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) + entform = EntranceForm(instance=entrance, initial={"who_are_you":editor}) if entslug is None: entletterform = EntranceLetterForm() # print(f" Getting entletter from EntranceLetterForm") @@ -764,9 +766,9 @@ def edit_entrance(request, path="", caveslug=None, entslug=None): print(f" Blank value: getting entletter from EntranceLetterForm") print(f"{entletter=} ") else: - entform = EntranceForm() + entform = EntranceForm(initial={"who_are_you":editor}) entletterform = EntranceLetterForm() - + return render( request, "editentrance.html", diff --git a/core/views/expo.py b/core/views/expo.py index 848fe21..3432ac3 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -457,6 +457,7 @@ def editexpopage(request, path): pageform = ExpoPageForm(request.POST) # A form bound to the POST data if pageform.is_valid(): # Form valid therefore write file editor = pageform.cleaned_data["who_are_you"] + editor = git_string(editor) # print("### \n", str(pageform)[0:300]) # print("### \n csrfmiddlewaretoken: ",request.POST['csrfmiddlewaretoken']) if filefound: @@ -489,6 +490,7 @@ def editexpopage(request, path): try: change_message = pageform.cleaned_data["change_message"] editor = pageform.cleaned_data["who_are_you"] + editor = git_string(editor) write_and_commit([(filepath, result, "utf-8")], f"{change_message} - online edit of {path}", editor) except WriteAndCommitError as e: return render(request, "errors/generic.html", {"message": e.message}) diff --git a/core/views/survex.py b/core/views/survex.py index afa6a69..5707720 100644 --- a/core/views/survex.py +++ b/core/views/survex.py @@ -324,8 +324,9 @@ def svx(request, survex_file): if request.method == "POST": # If the form has been submitted... rform = SvxForm(request.POST) # - if rform.is_valid(): # All validation rules pass (how do we check it against the filename and users?) + if rform.is_valid(): # All Django syntax validation rules pass (how do we check it against a valid filename and users?) editor = rform.cleaned_data["who_are_you"] + editor = git_string(editor) rcode = rform.cleaned_data["code"] outputtype = rform.cleaned_data["outputtype"] # used by CodeMirror ajax I think difflist = form.DiffCode(rcode) @@ -352,9 +353,8 @@ def svx(request, survex_file): if "save" in rform.data: if request.user.is_authenticated: if difflist: - editor = rform.cleaned_data["who_are_you"] print(f"Saving code and editor id {editor=}") - message = form.SaveCode(rcode, editor) + message = form.SaveCode(rcode, editor) # saves file and does git thing else: message = "NO DIFFERENCES - so not saving the file" else: |