2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-22 07:11:52 +00:00

Detect unwriteable file permissions earlier

This commit is contained in:
Philip Sargent 2022-03-05 17:05:15 +00:00
parent 32377f4e6c
commit d7fd6b00ae
6 changed files with 29 additions and 19 deletions

View File

@ -42,7 +42,8 @@ def writetrogglefile(filepath, filecontent):
'''Set permissions to rw-rw-r-- and commit the new saved file to git
Callers to cave.writeDataFile() or entrance.writeDataFile() should handle the exception PermissionsError explicitly
'''
# see also core/views/expo.py editexpopage()
# GIT see also core/views/expo.py editexpopage()
# GIT see also core/views/uploads.py dwgupload()
filepath = Path(filepath)
cwd = filepath.parent
filename = filepath.name
@ -56,7 +57,7 @@ def writetrogglefile(filepath, filecontent):
#os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
# should replace .call with .run and capture_output=True
call([git, "add", filename], cwd=cwd)
call([git, "commit", "-m", 'Troggle online cave or entrance edit'], cwd=cwd)
call([git, "commit", "-m", f'Troggle online: cave or entrance edit -{filename}'], cwd=cwd)
class Area(TroggleModel):

View File

@ -142,7 +142,7 @@ def expowebpage(request, expowebpath, path):
if m:
editable = False
else:
editable = True
editable = os.access(Path(expowebpath / path), os.W_OK) # are file permissions writeable?
has_menu = False
menumatch = re.match(r'(.*)<div id="menu">', body, re.DOTALL + re.IGNORECASE)
@ -253,7 +253,7 @@ def getmimetype(path):
@ensure_csrf_cookie
def editexpopage(request, path):
'''Manages the 'Edit this Page' capability for expo handbook and other html pages.
Relies on javascript to provide the in-browser editing environment.
Relies on HTML5 or javascript to provide the in-browser editing environment.
'''
try:
# if a cave not a webpage at all.
@ -310,7 +310,8 @@ def editexpopage(request, path):
cwd = filepath.parent
filename = filepath.name
git = settings.GIT
# see also core/models/cave.py writetrogglefile()
# GIT see also core/models/cave.py writetrogglefile()
# GIT see also core/views/uploads.py dwgupload()
try:
with open(filepath, "w") as f:
print(f'WRITING{cwd}---{filename} ')
@ -328,11 +329,11 @@ def editexpopage(request, path):
message = f'CANNOT git on server for this file {filename}. Edits saved but not added to git.\n\n' + msgdata
return render(request,'errors/generic.html', {'message': message})
cp_commit = subprocess.run([git, "commit", "-m", 'Edit this page'], cwd=cwd, capture_output=True, text=True)
# This produces return code = 1 if it commits OK, but if the repo still needs to be pushed to origin/expoweb
cp_commit = subprocess.run([git, "commit", "-m", f'Troggle online: Edit this page - {filename}'], 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'Eror code with git on server for this file {filename}. Edits saved, added to git but NOT committed.\n\n' + msgdata
message = f'Error code with git on server for this file {filename}. Edits saved, added to git, but NOT committed.\n\n' + msgdata
return render(request,'errors/generic.html', {'message': message})
except subprocess.SubprocessError:

View File

@ -144,6 +144,9 @@ def dwgupload(request, folder=None, gitdisable='no'):
actual_saved = []
refused = []
# GIT see also core/views/expo.py editexpopage()
# GIT see also core/models/cave.py writetrogglefile()
if gitdisable != 'yes': # set in url 'dwguploadnogit/'
git = settings.GIT
else:

View File

@ -4,13 +4,13 @@
{% block bodyattrs %}{% if homepage %} id="homepage"{% endif %}{% endblock %}
{% block body %}
{{ body|safe }}
{% if homepage %}
{% if editable %}
<a href="{% url "editexpopage" path %}">Edit</a>
{% endif %}
{% else %}
{% if not has_menu %}
{% include "menu.html" %}
{% if homepage %}
{% if editable %}
<a href="{% url "editexpopage" path %}">Edit</a>
{% endif %}
{% endif %}
{% else %}
{% if not has_menu %}
{% include "menu.html" %}
{% endif %}
{% endif %}
{% endblock %}

View File

@ -11,7 +11,8 @@
<li><form name=P method=get action="/search" target="_top">
<input id="omega-autofocus" type=search name=P size=8 autofocus>
<input type=submit value="Search"></li>
{% if editable %}<li><a href="{% url "editexpopage" path %}" class="editlink"><strong>Edit this page</strong></a></li>{% endif %}
{% if editable %}<li><a href="{% url "editexpopage" path %}" class="editlink"><strong>Edit this page</strong></a></li>
{% else %}<a href="/handbook/computing/hbmanual1.html#auto">This page not editable</a></li>{% endif %}
{% if cave_editable %}<li><a href="{% url "edit_cave" cave.slug %}" class="editlink"><strong>Edit this cave</strong></a></li>{% endif %}
</ul>
{% endif %}

View File

@ -71,6 +71,9 @@ else:
# accounts/reset/<uidb64>/<token>/ [name='password_reset_confirm']
# accounts/reset/done/ [name='password_reset_complete']
# BUT many of these are set up by opinionated Django even if 'django.contrib.auth.urls' is NOT included.
# Some overlap with 'admin.site.urls' needs to be investigated.
trogglepatterns = [
path('expofiles/', include(expofilesurls)), # intercepted by Apache, if it is running.
path('expofiles', include(expofilesurls)), # curious interaction with the include() here, not just a slash problem.
@ -171,7 +174,7 @@ trogglepatterns = [
re_path(r'^cave/(?P<cave_id>[^/]+)/(?P<year>\d\d\d\d)-(?P<qm_id>\d*)(?P<grade>[ABCDX]?)?$', caves.qm, name="qm"),
# Prospecting Guide document
re_path(r'^prospecting_guide/$', prospecting),
re_path(r'^prospecting_guide/$', prospecting), # disabled. Bad links, incompatible image package use and very, very out of date.
# This next set are all intercepted by Apache, if it is running.
re_path(r'^photos/(?P<subpath>.*)$', mediapage, {'doc_root': settings.PHOTOS_ROOT}, name="mediapage"), # photo galleries
@ -203,7 +206,8 @@ urlpatterns = [
# Alias /robots.txt /home/expo/troggle/media/robots.txt # does not exist!
# Alias /favicon.ico /home/expo/troggle/media/favicon.ico # comes from /expoweb/* when running runserver
# Alias /javascript /home/expo/troggle/media/jslib # empty
# Alias /search ? # teh text search thinggy
# Alias /search ? # the Xapian text search thinggy
# Alias /kanboard ? # the Kanban Trello-clone thinggy
# Copy of old standard apache configurations:
# Alias /expofiles /home/expo/expofiles