mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-17 00:17:07 +00:00
Cookie setting enabled for 'git editor'
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user