2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-15 23:17:08 +00:00

logbookedit now using git functions in troggle.utils

This commit is contained in:
2024-12-29 21:18:07 +00:00
parent f2a43558f9
commit 52600df2f2
2 changed files with 30 additions and 59 deletions

View File

@@ -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;"}
)
)

View File

@@ -91,6 +91,12 @@
title="Time underground (hours)"
{% 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" >