diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/utils.py | 28 | ||||
-rw-r--r-- | core/views/expo.py | 14 |
2 files changed, 37 insertions, 5 deletions
diff --git a/core/utils.py b/core/utils.py index a958d36..50746eb 100644 --- a/core/utils.py +++ b/core/utils.py @@ -233,14 +233,35 @@ nothing to commit, working tree clean f"CANNOT git COMMIT on server for this file {filename}. Subprocess error. Edits not saved.\nAsk a nerd to fix this." ) +def g_string(author_string): -def write_and_commit(files, message): + # Regular expression for a git-compatible author string + # valid example "John Doe <john.doe@example.com>" + + 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}") + 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}'") + return editor + +def write_and_commit(files, message, editor=None): """Writes the content to the filepath and adds and commits the file to git. If this fails, a WriteAndCommitError is raised. These need refactoring """ git = settings.GIT commands = [] + if editor: + editor = g_string(editor) + else: + # cannot happen as form verification has this as an obligatory field + editor = "Automatic <automaton@potatohut.expo>" try: for filepath, content, encoding in files: cwd = filepath.parent @@ -296,8 +317,9 @@ def write_and_commit(files, message): else: print(f"No change {filepath}") filepaths = [filepath for filepath, content, encoding in files] - message = message + " " + str(filepaths) - cmd_commit = [git, "commit", "-m", message] + # message = message + " " + str(filepaths) + print([git, "commit", "-m", message, "--author", f"{editor}"]) + cmd_commit = [git, "commit", "-m", message, "--author", f"{editor}"] cm_status = subprocess.run(cmd_commit, cwd=cwd, capture_output=True, text=True) commands.append(cmd_commit) if cm_status.returncode != 0: diff --git a/core/views/expo.py b/core/views/expo.py index bf9e78d..f6cd76e 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -482,7 +482,8 @@ def editexpopage(request, path): if not filefound or result != html: # Check if content changed at all try: change_message = pageform.cleaned_data["change_message"] - write_and_commit([(filepath, result, "utf-8")], f"{change_message} - online edit of {path}") + editor = pageform.cleaned_data["who_are_you"] + write_and_commit([(filepath, result, "utf-8")], f"{change_message} - online edit of {path}", editor) except WriteAndCommitError as e: return render(request, "errors/generic.html", {"message": e.message}) @@ -521,6 +522,15 @@ class ExpoPageForm(forms.Form): ) change_message = forms.CharField( widget=forms.Textarea( - attrs={"cols": 80, "rows": 3, "placeholder": "Describe the change made (for version control records)"} + attrs={"cols": 80, "rows": 2, "placeholder": "Describe the change made (for version control records)", + "style": "vertical-align: text-top;"} ) ) + who_are_you = forms.CharField( + widget=forms.Textarea( + attrs={"cols": 90, "rows": 1, "placeholder": "You have edited this page, who are you ? e.g. 'Animal <mta@loveshack.expo>'", + "style": "vertical-align: text-top;"} + ), + label = "Editor" + ) + |