2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-17 00:27:07 +00:00

refactor & git author work

This commit is contained in:
2024-12-29 03:42:58 +00:00
parent 13c5d14a9f
commit 6d2484376a
6 changed files with 64 additions and 130 deletions

View File

@@ -12,7 +12,7 @@ from troggle.core.models.caves import GetCaveLookup
from troggle.core.models.logbooks import LogbookEntry, PersonLogEntry, writelogbook
from troggle.core.models.survex import DrawingFile
from troggle.core.models.troggle import DataIssue, Expedition, PersonExpedition
from troggle.core.utils import alphabet_suffix, current_expo, sanitize_name, unique_slug, write_and_commit
from troggle.core.utils import COOKIE_MAX_AGE, alphabet_suffix, current_expo, get_cookie, git_string, sanitize_name, unique_slug, write_and_commit
from troggle.parsers.people import GetPersonExpeditionNameLookup, known_foreigner
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
@@ -150,6 +150,12 @@ class ExpofileRenameForm(forms.Form): # not a model-form, just a form-form
class ExpotextfileForm(forms.Form): # not a model-form, just a form-form
text = forms.CharField(strip=True, required=False)
who_are_you = forms.CharField(
widget=forms.TextInput(
attrs={"size": 100, "placeholder": "You are editing this page, who are you ? e.g. 'Wookey' or 'Animal <mta@gasthof.expo>'",
"style": "vertical-align: text-top;"}
)
)
class LogbookEditForm(forms.Form): # not a model-form, just a form-form
author = forms.CharField(strip=True, required=False)
@@ -160,7 +166,8 @@ def edittxtpage(request, path, filepath):
Yes this is a security hazard as arbitrary text can be uploaded and it is not enclosed in any HTML furniture.
"""
def simple_get(viewtext):
form = ExpotextfileForm()
print(f"simple_get {editor=}")
form = ExpotextfileForm(initial={"who_are_you":editor})
return render(
request,
"textfileform.html",
@@ -188,6 +195,7 @@ def edittxtpage(request, path, filepath):
print(message)
return render(request, "errors/generic.html", {"message": message})
editor = get_cookie(request)
if request.method == "GET":
return simple_get(originaltext)
@@ -198,6 +206,9 @@ def edittxtpage(request, path, filepath):
print(message)
return render(request, "errors/generic.html", {"message": message})
else:
editor = form.cleaned_data["who_are_you"]
editor = git_string(editor)
# for i in request.POST:
# print(":: ",i, " => ", request.POST[i])
newtext = request.POST["text"]
@@ -215,7 +226,7 @@ def edittxtpage(request, path, filepath):
if newtext != originaltext: # Check if content has changed at all
print("text changed.. saving and committing")
try:
write_and_commit([(filepath, newtext, "utf-8")], f"Online edit of {path}")
write_and_commit([(filepath, newtext, "utf-8")], f"Online edit of {path}", editor)
except WriteAndCommitError as e:
return render(request, "errors/generic.html", {"message": e.message})
@@ -228,7 +239,9 @@ def edittxtpage(request, path, filepath):
return render(request, "errors/generic.html", {"message": e.message})
savepath = "/" + path
print(f"redirect {savepath}")
return redirect(savepath) # Redirect after POST
response = redirect(savepath) # Redirect after POST
response.set_cookie('editor_id', editor, max_age=COOKIE_MAX_AGE) # cookie expires after COOKIE_MAX_AGE seconds
return response
else:
# no changes