summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2024-12-29 21:18:07 +0000
committerPhilip Sargent <philip.sargent@gmail.com>2024-12-29 21:18:07 +0000
commit52600df2f2d377da0140868c9c193e910bb085e1 (patch)
tree3b5e32e21099d74e48a564e9e92054ff87374db4
parentf2a43558f92ef97b274c7b4f38c3a2da6126fb05 (diff)
downloadtroggle-52600df2f2d377da0140868c9c193e910bb085e1.tar.gz
troggle-52600df2f2d377da0140868c9c193e910bb085e1.tar.bz2
troggle-52600df2f2d377da0140868c9c193e910bb085e1.zip
logbookedit now using git functions in troggle.utils
-rw-r--r--core/views/logbook_edit.py83
-rw-r--r--templates/logbookform.html6
2 files changed, 30 insertions, 59 deletions
diff --git a/core/views/logbook_edit.py b/core/views/logbook_edit.py
index 7411149..3819794 100644
--- a/core/views/logbook_edit.py
+++ b/core/views/logbook_edit.py
@@ -14,6 +14,7 @@ from troggle.core.models.survex import DrawingFile
from troggle.core.models.troggle import DataIssue, Expedition, PersonExpedition
from troggle.core.utils import (
COOKIE_MAX_AGE,
+ add_commit,
alphabet_suffix,
current_expo,
get_cookie,
@@ -180,6 +181,8 @@ def logbookedit(request, year=None, slug=None):
year = current_expo()
author = ""
+ editor = get_cookie(request)
+
if request.method == "POST":
prev_slug = "" # None value pending overwrite from submitted form
@@ -189,6 +192,8 @@ def logbookedit(request, year=None, slug=None):
print(message)
return render(request, "errors/generic.html", {"message": message})
else:
+ editor = form.cleaned_data["who_are_you"]
+ editor = git_string(editor)
# if there is no slug then this is probably a completely new lbe and we need to enter it into the db
# otherwise it is an update
# validation all to be done yet..
@@ -300,62 +305,10 @@ def logbookedit(request, year=None, slug=None):
# We do author validation on the form as displayed by GET, not at the moment of POST.
# If we had JS validation then we could be more timely.
- git = settings.GIT
dirpath = Path(settings.EXPOWEB) / "years" / str(year)
- lbe_add = subprocess.run(
- [git, "add", filename], cwd=dirpath, capture_output=True, text=True
- )
- msgdata = (
- lbe_add.stderr
- + "\n"
- + lbe_add.stdout
- + "\nreturn code: "
- + str(lbe_add.returncode)
- )
- message = f'! - FORM Logbook Edit {slug} - Success: git ADD on server for this file {filename}.' + msgdata
- print(message)
- if lbe_add.returncode != 0:
- msgdata = (
- "Ask a nerd to fix this.\n\n"
- + lbe_add.stderr
- + "\n\n"
- + lbe_add.stdout
- + "\n\nreturn code: "
- + str(lbe_add.returncode)
- )
- message = (
- f"! - FORM Logbook Edit - CANNOT git ADD on server for this file {filename}. {slug} edits saved but not added to git.\n"
- + msgdata
- )
- print(message)
- return render(request, "errors/generic.html", {"message": message})
-
- lbe_commit = subprocess.run(
- [git, "commit", "-m", f"Logbook edited {slug}"],
- cwd=dirpath,
- capture_output=True,
- text=True,
- )
- message = f'! - FORM Logbook Edit - {filename}. {slug} edits saved, added to git, and COMMITTED.\n' + msgdata
- print(message)
- #This produces return code = 1 if it commits OK
- if lbe_commit.returncode != 0:
- msgdata = (
- "Ask a nerd to fix this.\n\n"
- + lbe_commit.stderr
- + "\n"
- + lbe_commit.stdout
- + "\nreturn code: "
- + str(lbe_commit.returncode)
- )
- message = (
- f"! - FORM Logbook Edit - Error code '{lbe_commit.returncode}' with git on server for {filename}. {slug} edits saved, added to git, but NOT committed.\n"
- + msgdata
- )
- print(message)
- if not (lbe_commit.returncode ==1 and settings.DEVSERVER):
- # rc=1 is OK on the development server
- return render(request, "errors/generic.html", {"message": message})
+ contents_path = dirpath / filename
+ commit_msg = f"Online edit of logbookentry {slug}"
+ add_commit(contents_path, commit_msg, editor)
# This does not change the URL in the browser, so despite a new slug being created,
# the next time this code is run it thinks a new slug needs to be created. So we should
@@ -364,9 +317,12 @@ def logbookedit(request, year=None, slug=None):
# HOWEVER by doing a redirect rather than returning a rendered page, we lose all the
# error settings e.g dateflag and authroflag so the user gets no feedback about bad data entered.
- # so we need to pass the flags explicitly int he url and then extract them from the request in the GET bit. sigh.
- return HttpResponseRedirect(f"/logbookedit/{slug}?dateflag={dateflag}&authorflag={authorflag}")
+ # so we need to pass the flags explicitly in the url and then extract them from the request in the GET bit. sigh.
+ response = HttpResponseRedirect(f"/logbookedit/{slug}?dateflag={dateflag}&authorflag={authorflag}")
+ response.set_cookie('editor_id', editor, max_age=COOKIE_MAX_AGE) # cookie expires after COOKIE_MAX_AGE seconds
+ return response
+ # Do the redirect instead of this:
# return render(
# request,
# "logbookform.html",
@@ -384,9 +340,11 @@ def logbookedit(request, year=None, slug=None):
# "slug": slug,
# },
# )
- # GET here
+
+ # GET here. Does not fall-through from the POST section.
else:
- form = LogbookEditForm()
+ # Since this form is manually made, not Django constructed, this "initial=" setting has no effect.
+ form = LogbookEditForm(initial={"who_are_you":editor})
year = validate_year(year)
if request.GET.get('dateflag', 'False') == "True":
@@ -444,8 +402,15 @@ def logbookedit(request, year=None, slug=None):
"entry": text,
"textrows": rows,
"slug": slug,
+ "who_are_you": editor,
},
)
class LogbookEditForm(forms.Form): # not a model-form, just a form-form
author = forms.CharField(strip=True, required=False)
+ who_are_you = forms.CharField(
+ widget=forms.TextInput( # a manual form, not a Django generated form, so this widget is not used.
+ 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;"}
+ )
+ ) \ No newline at end of file
diff --git a/templates/logbookform.html b/templates/logbookform.html
index 632b52c..1be61f2 100644
--- a/templates/logbookform.html
+++ b/templates/logbookform.html
@@ -92,6 +92,12 @@
{% if tu %}value="{{tu}}"{% else %}placeholder="0.1" {% endif %}
/>
<br /><br />
+ <label for="who_are_you">Who are you, editing this logbook entry?</label>
+ <input {% if not user.username %} disabled{% endif %}
+ label = "Who are you" name = "who_are_you" size ="70"
+ title="Who are you"
+ placeholder="editor's name for version control e.g. 'Animal <mta@gasthof.expo>'" value="{{who_are_you}}" required/>
+ <br /><br />
<input type="hidden" value="{{slug}}" name="slug">
<button class="fancybutton2" style="padding: 0.5em 25px; margin-left: 110px" type = "submit" value = "save" >
Update logbook entry