From 52600df2f2d377da0140868c9c193e910bb085e1 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sun, 29 Dec 2024 21:18:07 +0000 Subject: [PATCH] logbookedit now using git functions in troggle.utils --- core/views/logbook_edit.py | 83 +++++++++++--------------------------- templates/logbookform.html | 6 +++ 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 '", + "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 %} />

+ + +