summaryrefslogtreecommitdiffstats
path: root/core/views/expo.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2024-12-27 20:23:04 +0000
committerPhilip Sargent <philip.sargent@gmail.com>2024-12-27 20:23:04 +0000
commit60d24dc48ed7a32752b2292e4e8670903fad8638 (patch)
tree598c17039cbdecd988427980a60b38bfbcde11d0 /core/views/expo.py
parentdccd46535468d01af325c4e17f0e190c6404b037 (diff)
downloadtroggle-60d24dc48ed7a32752b2292e4e8670903fad8638.tar.gz
troggle-60d24dc48ed7a32752b2292e4e8670903fad8638.tar.bz2
troggle-60d24dc48ed7a32752b2292e4e8670903fad8638.zip
Cookie setting enabled for 'git editor'
Diffstat (limited to 'core/views/expo.py')
-rw-r--r--core/views/expo.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/core/views/expo.py b/core/views/expo.py
index 5fa3b33..3fd6ddc 100644
--- a/core/views/expo.py
+++ b/core/views/expo.py
@@ -14,7 +14,7 @@ from django.views.decorators.csrf import ensure_csrf_cookie
import troggle.core.views.caves
import troggle.settings as settings
from troggle.core.models.caves import Cave
-from troggle.core.utils import WriteAndCommitError, current_expo, write_and_commit
+from troggle.core.utils import WriteAndCommitError, current_expo, git_string, write_and_commit
from troggle.core.views.editor_helpers import HTMLarea
from troggle.core.views.uploads import edittxtpage
@@ -26,7 +26,7 @@ Then it was incorporated into troggle directly, rather than being an unnecessary
This is a succession of hacks and needs to be redisgned and refactored.
"""
-
+COOKIE_MAX_AGE = 12*60*60 # seconds
default_head = """<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CUCC Expedition - index</title>
@@ -451,14 +451,17 @@ def editexpopage(request, path):
print("### File not found ### ", filepath)
filefound = False
- editor_name = request.COOKIES.get('editor_id', '') # if no cookie, then get empty string
+ print(f"Reading cookie...")
+ editor_id = request.COOKIES.get('editor_id', 'speleologist') # 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.method == "POST": # If the form has been submitted...
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"]
# print("### \n", str(pageform)[0:300])
# print("### \n csrfmiddlewaretoken: ",request.POST['csrfmiddlewaretoken'])
- if not editor_name:
if filefound:
headmatch = re.match(r"(.*)<title>.*</title>(.*)", head, re.DOTALL + re.IGNORECASE)
if headmatch:
@@ -483,14 +486,17 @@ def editexpopage(request, path):
result = f"{preheader}<head{headerargs}>{head}</head>{postheader}<body{bodyargs}>\n{body}</body>{postbody}"
if not filefound or result != html: # Check if content changed at all
+ edit_response = HttpResponseRedirect(reverse("expopage", args=[path])) # Redirect after POST
+ edit_response.set_cookie('editor_id', editor, max_age=COOKIE_MAX_AGE) # cookie expires after COOKIE_MAX_AGE seconds
+ print(f"Cookie set: {editor} for {COOKIE_MAX_AGE} seconds")
try:
change_message = pageform.cleaned_data["change_message"]
editor = pageform.cleaned_data["who_are_you"]
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})
-
- return HttpResponseRedirect(reverse("expopage", args=[path])) # Redirect after POST
+ print(f"Returning response now, which should set cookie on client browser")
+ return edit_response
else:
if filefound:
m = re.search(r"<title>(.*)</title>", head, re.DOTALL + re.IGNORECASE)
@@ -498,9 +504,9 @@ def editexpopage(request, path):
(title,) = m.groups()
else:
title = ""
- pageform = ExpoPageForm(initial={"who_are_you":editor_name, "html": body, "title": title})
+ pageform = ExpoPageForm(initial={"who_are_you":editor, "html": body, "title": title})
else:
- pageform = ExpoPageForm(initial={"who_are_you":editor_name})
+ pageform = ExpoPageForm(initial={"who_are_you":editor})
return render(