summaryrefslogtreecommitdiffstats
path: root/core/views/survex.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2023-03-06 04:52:41 +0000
committerPhilip Sargent <philip.sargent@gmail.com>2023-03-06 04:52:41 +0000
commitccfc44a4234e3fb6b590d79fa76f7e03b6d5dca0 (patch)
tree3bb46dec172b5127d899a0fc67d3e890b5fb68a8 /core/views/survex.py
parent8f3b3295527ac76c20e11cf29a94895a4c7d6a17 (diff)
downloadtroggle-ccfc44a4234e3fb6b590d79fa76f7e03b6d5dca0.tar.gz
troggle-ccfc44a4234e3fb6b590d79fa76f7e03b6d5dca0.tar.bz2
troggle-ccfc44a4234e3fb6b590d79fa76f7e03b6d5dca0.zip
Saving new survex file parses contents.
Diffstat (limited to 'core/views/survex.py')
-rw-r--r--core/views/survex.py57
1 files changed, 33 insertions, 24 deletions
diff --git a/core/views/survex.py b/core/views/survex.py
index e60df6e..ea00fcc 100644
--- a/core/views/survex.py
+++ b/core/views/survex.py
@@ -38,7 +38,7 @@ even though there are dozens of surveys.
- the primary survex file in each cave directory should be in a configuration, not buried in the code...
-- Save the edited survexfile as a survexfile object and re-parse it, and update
+- Save and re-parse an edited survexfile which already exists in the db, and update
all its dependencies (work in progress)
"""
@@ -115,6 +115,24 @@ survextemplatefile = """; *** THIS IS A TEMPLATE FILE NOT WHAT YOU MIGHT BE EXPE
*end [surveyname]
"""
+def get_survexfile(filename):
+ """Gets the SurvexFile object from the survex path for the file
+ in a robust way
+ """
+ refs = SurvexFile.objects.filter(path=filename)
+ if len(refs)==0: # new survex file, not created in db yet
+ survexfile = False
+ elif len(refs)==1:
+ survexfile = SurvexFile.objects.get(path=filename)
+ else:
+ survexfile = refs[0]
+ # OK this is due to a bug in the import file parsing, whoops. Now fixed ?!
+ print("BUG - to be fixed in the survex parser - not critical..")
+ print(f"Number of SurvexFile objects found: {len(refs)}")
+ for s in refs:
+ print (s.path, s.survexdirectory, s.cave)
+ # print(type(survexfile), filename)
+ return survexfile
class SvxForm(forms.Form):
"""Two-pane form, upper half is the raw survex file, lower half (with green background)
@@ -138,18 +156,8 @@ class SvxForm(forms.Form):
self.template = True
self.survexfile = False
return survextemplatefile
- refs = SurvexFile.objects.filter(path=self.data["filename"])
- if len(refs)==0: # new survex file, not created in db yet
- self.survexfile = False
- elif len(refs)==1:
- self.survexfile = SurvexFile.objects.get(path=self.data["filename"])
- else:
- self.survexfile = refs[0]
- # OK this is due to a bug in the import file parsing, whoops.
- print("BUG - to be fixed in the survex parser - not critical..")
- print(f"Number of SurvexFile objects found: {len(refs)}")
- for s in refs:
- print (s.path, s.survexdirectory, s.cave)
+ if not self.survexfile:
+ self.survexfile = get_survexfile(self.data["filename"])
try:
fin = open(fname, "r", encoding="utf8", newline="")
svxtext = fin.read()
@@ -205,10 +213,8 @@ class SvxForm(forms.Form):
comment = f"Online survex edit: {self.data['filename']}.svx on dev machine '{socket.gethostname()}' "
only_commit(fname, comment)
-
parse_one_file(self.data["filename"])
-
return "SAVED and committed to git (if there were differences)"
def Process(self):
@@ -260,6 +266,8 @@ def svx(request, survex_file):
Originally the non-existence of difflist was used as a marker to say that the file had been saved
and that thuis there were no differences. This is inadequate, as a new file which has not been saved
also has no difflist.
+
+ Needs refactoring. Too many piecemeal edits and odd state dependencies.
"""
warning = False
@@ -281,8 +289,11 @@ def svx(request, survex_file):
rcode = rform.cleaned_data["code"]
outputtype = rform.cleaned_data["outputtype"] # used by CodeMirror ajax I think
difflist = form.DiffCode(rcode)
- # print(">>>> ", rform.data)
- svxfile = form.survexfile
+ # keys = []
+ # for key in rform.data:
+ # keys.append(key)
+ # print(">>>> ", keys)
+ sfile = form.survexfile
if "revert" in rform.data:
pass
@@ -291,7 +302,7 @@ def svx(request, survex_file):
if difflist:
message = "SAVE FILE FIRST"
form.data["code"] = rcode
- elif svxfile:
+ elif sfile:
logmessage = form.Process()
if logmessage:
message = f"OUTPUT FROM PROCESSING\n{logmessage}"
@@ -305,13 +316,14 @@ def svx(request, survex_file):
message = "You do not have authority to save this file. Please log in."
if message != "SAVED":
form.data["code"] = rcode
+
if "diff" in rform.data:
print("Differences: ")
form.data["code"] = rcode
- # GET or after POST-specific handling
- svxfile = form.survexfile # only valid once form.GetDiscCode() called
-
+ # GET, also fall-through after POST-specific handling
+ svxfile = get_survexfile(survex_file)
+
if "code" not in form.data:
form.data["code"] = form.GetDiscCode()
if form.template:
@@ -324,10 +336,8 @@ def svx(request, survex_file):
if message:
difflist.insert(0, message)
- # print [ form.data['code'] ]
svxincludes = re.findall(r"(?i)\*include\s+(\S+)", form.data["code"] or "")
-
# collect all the survex blocks which actually have a valid date
if svxfile:
try:
@@ -359,7 +369,6 @@ def svx(request, survex_file):
"form": form,
"events": events,
}
- # vmap.update(csrf(request)) # this now refreshes to the wrong value, now that we user render(request,
if outputtype == "ajax": # used by CodeMirror ajax I think
return render(request, "svxfiledifflistonly.html", vmap)