2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-16 23:47:04 +00:00

loading cave aliases from file now working

This commit is contained in:
2024-07-03 19:27:37 +03:00
parent b6ffcb63bf
commit 1c8c36c82f
4 changed files with 104 additions and 21 deletions

View File

@@ -12,7 +12,7 @@ from troggle.core.models.caves import GetCaveLookup
from troggle.core.models.logbooks import LogbookEntry, writelogbook, PersonLogEntry
from troggle.core.models.survex import DrawingFile
from troggle.core.models.troggle import DataIssue, Expedition, PersonExpedition
from troggle.core.utils import alphabet_suffix, current_expo, sanitize_name, unique_slug
from troggle.core.utils import alphabet_suffix, current_expo, sanitize_name, unique_slug, write_and_commit
from troggle.parsers.people import GetPersonExpeditionNameLookup, known_foreigner
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
@@ -152,11 +152,11 @@ class ExpotextfileForm(forms.Form): # not a model-form, just a form-form
class LogbookEditForm(forms.Form): # not a model-form, just a form-form
author = forms.CharField(strip=True, required=False)
@login_required_if_public
def edittxtpage(request, path, filepath):
"""Editing a .txt file on expoweb/
"""
message=""
def simple_get():
def simple_get(viewtext):
form = ExpotextfileForm()
return render(
request,
@@ -166,23 +166,26 @@ def edittxtpage(request, path, filepath):
"path": path,
"message": message,
"filepath": filepath,
"text": text,
"text": viewtext,
},
)
message=""
if not filepath.is_file():
print(f"Not a file: {filepath}")
errpage = f"<html>" + default_head + f"<h3>File not found '{filepath}'<br><br>failure detected in expowebpage() in views.expo.py</h3> </body>"
return HttpResponse(errpage)
try:
with open(filepath) as f:
text = f.read()
with open(filepath, "r") as f:
originaltext = f.read()
except IOError:
print("### File reading failue, but it exists.. ### ", filepath)
filefound = False
message = f'Cannot open {filepath} for text file reading even though it is a file.'
print(message)
return render(request, "errors/generic.html", {"message": message})
if request.method == "GET":
return simple_get()
return simple_get(originaltext)
elif request.method == "POST":
form = ExpotextfileForm(request.POST)
@@ -191,22 +194,44 @@ def edittxtpage(request, path, filepath):
print(message)
return render(request, "errors/generic.html", {"message": message})
else:
for i in request.POST:
print(":: ",i, " => ", request.POST[i])
# for i in request.POST:
# print(":: ",i, " => ", request.POST[i])
newtext = request.POST["text"]
print("POST")
if "Cancel" in request.POST:
print("cancel")
return simple_get()
return simple_get(originaltext)
if "Save" in request.POST:
print("submitted for saving..")
message="submitted for saving.. not implemented yet.."
# INSERT FILE SAVING AND git committing on server
return simple_get()
# mistake, abort
if newtext != originaltext: # Check if content has changed at all
print("text changed.. saving and committing")
try:
write_and_commit([(filepath, newtext, "utf-8")], f"Online edit of {path}")
except WriteAndCommitError as e:
return render(request, "errors/generic.html", {"message": e.message})
print("re-reading from file..")
try:
with open(filepath) as f:
rereadtext = f.read()
except:
print("### File reading failure, but it exists.. ### ", filepath)
return render(request, "errors/generic.html", {"message": e.message})
savepath = "/" + path
print(f"redirect {savepath}")
return redirect(savepath) # Redirect after POST
else:
# no changes
pass
return simple_get(originaltext)
else:
# mistake not POST or GET
message="Something went wrong"
return simple_get()
print(message)
return simple_get(originaltext)
@login_required_if_public