diff options
-rw-r--r-- | core/utils.py | 8 | ||||
-rw-r--r-- | core/views/expo.py | 22 |
2 files changed, 18 insertions, 12 deletions
diff --git a/core/utils.py b/core/utils.py index 6086b60..a251ba0 100644 --- a/core/utils.py +++ b/core/utils.py @@ -234,10 +234,10 @@ nothing to commit, working tree clean ) def git_string(author_string): - - # Regular expression for a git-compatible author string - # valid example "John Doe <john.doe@example.com>" - + """Rewrites the supplied editor string intoa git-complient author string + Uses a regular expression for a git-compatible author string + valid example "John Doe <john.doe@example.com>" + """ author_regex = re.compile(r'^[a-zA-Z][\w\s\_\.\-]* <[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-_]+\.[a-zA-Z]{2,}>$') if author_regex.match(author_string): 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( |