Add git commit messages when editing via website. Make sure cust menus are not deleted.

This commit is contained in:
Martin Green 2022-06-23 21:31:57 +01:00
parent 97a9f2aae6
commit ef68db080a
2 changed files with 13 additions and 22 deletions

View File

@ -341,7 +341,8 @@ def editexpopage(request, path):
if result != html: # Check if content changed
try:
version_control.write_and_commit(filepath, result)
change_message = pageform.cleaned_data["change_message"]
version_control.write_and_commit(filepath, result, f'{change_message} - online edit of {path}')
except version_control.WriteAndCommitError as e:
return render(request,'errors/generic.html', {'message': e.message})
@ -353,20 +354,14 @@ def editexpopage(request, path):
title, = m.groups()
else:
title = ""
pageform = ExpoPageForm({"html": body, "title": title})
pageform = ExpoPageForm(initial = {"html": body, "title": title})
else:
body = "### File not found ###\n" + str(filepath)
import sys
import locale
body = body + "\nsys.getdefaultencoding() = " + str(sys.getdefaultencoding())
body = body + "\nsys.getfilesystemencoding() = " + str(sys.getfilesystemencoding())
body = body + "\nlocale.getdefaultlocale() = " + str(locale.getdefaultlocale())
body = body + "\nlocale.getpreferredencoding() = " + str(locale.getpreferredencoding())
pageform = ExpoPageForm({"html": body, "title": "Missing"})
pageform = ExpoPageForm()
return render(request, 'editexpopage.html', {'path': path, 'form': pageform, })
class ExpoPageForm(forms.Form):
'''The form used by the editexpopage function
'''
title = forms.CharField(widget=forms.TextInput(attrs={'size':'60'}))
html = forms.CharField(widget=forms.Textarea(attrs={"cols":80, "rows":20}))
title = forms.CharField(widget=forms.TextInput(attrs={'size':'60', 'placeholder': "Enter title (displayed in tab)"}))
html = forms.CharField(widget=forms.Textarea(attrs={"cols":80, "rows":20, 'placeholder': "Enter page content (using HTML)"}))
change_message = forms.CharField(widget=forms.Textarea(attrs={"cols":80, "rows":5, 'placeholder': "Descibe the change made (for git)"}))

View File

@ -1,7 +1,7 @@
import troggle.settings as settings
import subprocess
def write_and_commit(filepath, content):
def write_and_commit(filepath, content, message):
"""Writes the content to the filepath and adds and commits the file to git. If this fails, a WriteAndCommitError is raised."""
cwd = filepath.parent
filename = filepath.name
@ -16,26 +16,22 @@ def write_and_commit(filepath, content):
# os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
f.write(content)
except PermissionError:
message = f'CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this.'
raise WriteAndCommitError(message)
raise WriteAndCommitError(f'CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this.')
try:
cp_add = subprocess.run([git, "add", filename], cwd=cwd, capture_output=True, text=True)
if cp_add.returncode != 0:
msgdata = 'Ask a nerd to fix this.\n\n' + cp_add.stderr + '\n\n' + cp_add.stdout + '\n\nreturn code: ' + str(cp_add.returncode)
message = f'CANNOT git on server for this file {filename}. Edits saved but not added to git.\n\n' + msgdata
raise WriteAndCommitError(message)
raise WriteAndCommitError(f'CANNOT git on server for this file {filename}. Edits saved but not added to git.\n\n' + msgdata)
cp_commit = subprocess.run([git, "commit", "-m", f'Troggle online: Edit this page - {filename}'], cwd=cwd, capture_output=True, text=True)
cp_commit = subprocess.run([git, "commit", "-m", message], cwd=cwd, capture_output=True, text=True)
# This produces return code = 1 if it commits OK, but when the repo still needs to be pushed to origin/expoweb
if cp_commit.returncode != 0 and cp_commit.stdout != 'nothing to commit, working tree clean':
msgdata = 'Ask a nerd to fix this.\n\n' + cp_commit.stderr + '\n\n' + cp_commit.stdout + '\n\nreturn code: ' + str(cp_commit.returncode)
message = f'Error code with git on server for this file {filename}. Edits saved, added to git, but NOT committed.\n\n' + msgdata
raise WriteAndCommitError(message)
raise WriteAndCommitError(f'Error code with git on server for this file {filename}. Edits saved, added to git, but NOT committed.\n\n' + msgdata)
except subprocess.SubprocessError:
message = f'CANNOT git on server for this file {filename}. Subprocess error. Edits not saved.\nAsk a nerd to fix this.'
raise WriteAndCommitError(message)
raise WriteAndCommitError(f'CANNOT git on server for this file {filename}. Subprocess error. Edits not saved.\nAsk a nerd to fix this.')
class WriteAndCommitError(Exception):
"""Exception class for errors writing files and comitting them to git"""