summaryrefslogtreecommitdiffstats
path: root/core/views
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2024-12-29 03:42:58 +0000
committerPhilip Sargent <philip.sargent@gmail.com>2024-12-29 03:42:58 +0000
commit6d2484376a5816387fedfb03eb83c42bcb81d6a1 (patch)
tree827fc860739fe0cdcea4904bb07513b81486c752 /core/views
parent13c5d14a9fb102d1f5ea78fcd58b6e2a30fe81b7 (diff)
downloadtroggle-6d2484376a5816387fedfb03eb83c42bcb81d6a1.tar.gz
troggle-6d2484376a5816387fedfb03eb83c42bcb81d6a1.tar.bz2
troggle-6d2484376a5816387fedfb03eb83c42bcb81d6a1.zip
refactor & git author work
Diffstat (limited to 'core/views')
-rw-r--r--core/views/editor_helpers.py23
-rw-r--r--core/views/expo.py1
-rw-r--r--core/views/uploads.py21
3 files changed, 35 insertions, 10 deletions
diff --git a/core/views/editor_helpers.py b/core/views/editor_helpers.py
index 7f80a9b..ead0bc8 100644
--- a/core/views/editor_helpers.py
+++ b/core/views/editor_helpers.py
@@ -11,7 +11,7 @@ from django.views.decorators.csrf import ensure_csrf_cookie
from PIL import Image
import troggle.settings as settings
-from troggle.core.utils import WriteAndCommitError, write_and_commit
+from troggle.core.utils import COOKIE_MAX_AGE, WriteAndCommitError, get_cookie, git_string, write_and_commit
from .auth import login_required_if_public
@@ -97,12 +97,16 @@ def new_image_form(request, path):
"""Manages a form to upload new images"""
directory = get_dir(path)
print(f"new_image_form(): {directory=} {path=}")
+
+ editor = get_cookie(request)
if request.method == "POST":
print(f"new_image_form(): POST ")
form = NewWebImageForm(request.POST, request.FILES, directory=directory)
if form.is_valid():
print(f"new_image_form(): form is valid ")
print(f"new_image_form(): files: {request.FILES['file_']}")
+ editor = form.cleaned_data["who_are_you"]
+ editor = git_string(editor)
f = request.FILES["file_"]
binary_data = io.BytesIO()
for chunk in f.chunks():
@@ -171,6 +175,7 @@ def new_image_form(request, path):
(thumb_path, tb.getbuffer(), False),
],
f"{change_message} - online adding of an image",
+ editor # this works, a new who_are_you typed on the Image form is used as the git comment
)
except WriteAndCommitError as e:
print(f"new_image_form(): WriteAndCommitError: {e.message}")
@@ -183,10 +188,12 @@ def new_image_form(request, path):
html_snippet = linked_image_template.render(
{"thumbnail_url": f"/{thumb_rel_path}", "page_url": f"/{desc_rel_path}"}, request
)
- return JsonResponse({"html": html_snippet})
+ j_response = JsonResponse({"html": html_snippet})
+ j_response.set_cookie('editor_id', editor, max_age=COOKIE_MAX_AGE) # does not seem to work updating who_are_you cookie
+ return j_response
else:
print(f"new_image_form(): not POST ")
- form = NewWebImageForm(directory=directory)
+ form = NewWebImageForm(directory=directory, initial={"who_are_you":editor})
print(f"new_image_form(): POST and not POST ")
template = loader.get_template("new_image_form.html")
htmlform = template.render({"form": form, "path": path}, request)
@@ -212,9 +219,15 @@ class NewWebImageForm(forms.Form):
widget=forms.TextInput(attrs={"size": "60", "placeholder": "Year photo was taken"}), required=False
)
change_message = forms.CharField(
- widget=forms.Textarea(attrs={"cols": 80, "rows": 3, "placeholder": "Descibe the change made (for git)"})
+ widget=forms.Textarea(attrs={"cols": 80, "rows": 3, "placeholder": "Describe the change made (for git)"})
)
-
+ who_are_you = forms.CharField(
+ widget=forms.TextInput(
+ attrs={"size": 60, "placeholder": "You are editing this page, who are you ? e.g. 'Becka' or 'Animal <mta@gasthof.expo>'",
+ "style": "vertical-align: text-top;"}
+ )
+ )
+
def __init__(self, *args, **kwargs):
self.directory = Path(kwargs.pop("directory"))
super(forms.Form, self).__init__(*args, **kwargs)
diff --git a/core/views/expo.py b/core/views/expo.py
index d0b721e..d3d649b 100644
--- a/core/views/expo.py
+++ b/core/views/expo.py
@@ -80,7 +80,6 @@ def mapfile(request, path):
return render(request, "errors/generic.html", {"message": message})
-
def expofilessingle(request, filepath):
"""sends a single binary file to the user, if not found, show the parent directory
If the path actually is a directory, then show that.
diff --git a/core/views/uploads.py b/core/views/uploads.py
index c7b7e74..f4e2278 100644
--- a/core/views/uploads.py
+++ b/core/views/uploads.py
@@ -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