summaryrefslogtreecommitdiffstats
path: root/core/views/uploads.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2024-07-03 19:27:37 +0300
committerPhilip Sargent <philip.sargent@gmail.com>2024-07-03 19:27:37 +0300
commit1c8c36c82fccead17af96b91720c266600805a8b (patch)
treebf30effad4cfef878ef7b42795343fa0b53ddc85 /core/views/uploads.py
parentb6ffcb63bf186e154dbf25d9dce8754c031d7a82 (diff)
downloadtroggle-1c8c36c82fccead17af96b91720c266600805a8b.tar.gz
troggle-1c8c36c82fccead17af96b91720c266600805a8b.tar.bz2
troggle-1c8c36c82fccead17af96b91720c266600805a8b.zip
loading cave aliases from file now working
Diffstat (limited to 'core/views/uploads.py')
-rw-r--r--core/views/uploads.py59
1 files changed, 42 insertions, 17 deletions
diff --git a/core/views/uploads.py b/core/views/uploads.py
index 6ebec8c..3d4e987 100644
--- a/core/views/uploads.py
+++ b/core/views/uploads.py
@@ -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