mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2026-04-01 11:41:57 +01:00
cave edit working with cookies
This commit is contained in:
@@ -198,6 +198,13 @@ def parse_aliases(aliasfile):
|
||||
return [(None, None)], "Fail on file reading"
|
||||
return aliases, report
|
||||
|
||||
def get_cookie(request):
|
||||
NO_COOKIE_DEFAULT = 'Höhlenforscher <hohlenforscher@stonebridge.expo>'
|
||||
print(f"-- Getting cookie...")
|
||||
editor_id = request.COOKIES.get('editor_id', NO_COOKIE_DEFAULT) # 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 to be used: {editor=}")
|
||||
return editor
|
||||
|
||||
def git_string(author_string):
|
||||
"""Rewrites the supplied editor string intoa git-complient author string
|
||||
@@ -207,13 +214,13 @@ def git_string(author_string):
|
||||
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):
|
||||
print(f"Valid git-compatible author string: '{author_string}'")
|
||||
print(f"++ Is valid git-compatible author string: '{author_string}'")
|
||||
return author_string
|
||||
else:
|
||||
editor = author_string.replace("@","_at_")
|
||||
editor = re.sub('[^0-9a-zA-Z_\.]+', '*', editor)
|
||||
editor += f" <{editor}@potatohut.expo>"
|
||||
print(f"Not git-compatible author string, replacing as '{editor}'")
|
||||
print(f"++ Not git-compatible author string '{author_string}', replacing as '{editor}'")
|
||||
return editor
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ from troggle.core.forms import CaveForm, EntranceForm, EntranceLetterForm # Cav
|
||||
from troggle.core.models.caves import Cave, CaveAndEntrance, Entrance, GetCaveLookup, get_cave_leniently
|
||||
from troggle.core.models.logbooks import QM
|
||||
from troggle.core.models.wallets import Wallet
|
||||
from troggle.core.utils import COOKIE_MAX_AGE, WriteAndCommitError, current_expo, git_string, write_and_commit
|
||||
from troggle.core.utils import COOKIE_MAX_AGE, WriteAndCommitError, current_expo, get_cookie, git_string, write_and_commit
|
||||
from troggle.core.views import expo
|
||||
from troggle.parsers.caves import read_cave, read_entrance
|
||||
from troggle.settings import CAVEDESCRIPTIONS, ENTRANCEDESCRIPTIONS
|
||||
@@ -477,10 +477,7 @@ def edit_cave(request, path="", slug=None):
|
||||
if not (cave:= get_cave_from_slug(slug)): # walrus operator
|
||||
return render(request, "errors/badslug.html", {"badslug": f"for cave {caveslug} - from edit_cave()"})
|
||||
|
||||
print(f"Reading cookie...")
|
||||
editor_id = request.COOKIES.get('editor_id', 'Höhlenforscher <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:
|
||||
form = CaveForm(request.POST, instance=cave)
|
||||
@@ -511,7 +508,7 @@ def edit_cave(request, path="", slug=None):
|
||||
|
||||
try:
|
||||
cave_file = cave.file_output()
|
||||
write_and_commit([cave_file], f"Online edit of cave {cave}")
|
||||
write_and_commit([cave_file], f"Online edit of cave {cave}", editor)
|
||||
# leave other exceptions unhandled so that they bubble up to user interface
|
||||
except PermissionError:
|
||||
message = f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {cave.filename}. Ask a nerd to fix this."
|
||||
|
||||
@@ -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 COOKIE_MAX_AGE, WriteAndCommitError, current_expo, git_string, write_and_commit
|
||||
from troggle.core.utils import COOKIE_MAX_AGE, WriteAndCommitError, current_expo, get_cookie, git_string, write_and_commit
|
||||
from troggle.core.views.editor_helpers import HTMLarea
|
||||
from troggle.core.views.uploads import edittxtpage
|
||||
|
||||
@@ -451,10 +451,7 @@ def editexpopage(request, path):
|
||||
print("### File not found ### ", filepath)
|
||||
filefound = False
|
||||
|
||||
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=}")
|
||||
editor = get_cookie(request)
|
||||
|
||||
if request.method == "POST": # If the form has been submitted...
|
||||
pageform = ExpoPageForm(request.POST) # A form bound to the POST data
|
||||
@@ -488,7 +485,7 @@ def editexpopage(request, path):
|
||||
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")
|
||||
print(f"Cookie set: {editor} for {COOKIE_MAX_AGE/3600} hours")
|
||||
try:
|
||||
change_message = pageform.cleaned_data["change_message"]
|
||||
editor = pageform.cleaned_data["who_are_you"]
|
||||
|
||||
@@ -19,7 +19,7 @@ from troggle.core.models.caves import Cave, GetCaveLookup
|
||||
from troggle.core.models.logbooks import LogbookEntry
|
||||
from troggle.core.models.survex import SurvexBlock, SurvexFile #, SurvexDirectory
|
||||
from troggle.core.models.wallets import Wallet
|
||||
from troggle.core.utils import COOKIE_MAX_AGE, current_expo, git_string, add_commit
|
||||
from troggle.core.utils import COOKIE_MAX_AGE, current_expo, get_cookie, git_string, add_commit
|
||||
from troggle.parsers.survex import parse_one_file
|
||||
|
||||
"""Everything that views survexfiles
|
||||
@@ -313,10 +313,7 @@ def svx(request, survex_file):
|
||||
nowtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
outputtype = "normal"
|
||||
|
||||
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=}")
|
||||
editor = get_cookie(request)
|
||||
|
||||
form = SvxForm({"filename": survex_file, "dirname": dirname, "datetime": nowtime, "outputtype": outputtype, "who_are_you":editor})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user