mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-17 23:07:12 +00:00
ran 'black' to reformat all the core files
This commit is contained in:
@@ -15,20 +15,19 @@ from django.views.decorators.csrf import ensure_csrf_cookie
|
||||
import parsers.survex
|
||||
import troggle.settings as settings
|
||||
from troggle.core.models.caves import Cave
|
||||
from troggle.core.models.logbooks import LogbookEntry #, PersonLogEntry
|
||||
from troggle.core.models.survex import (SurvexBlock, SurvexDirectory,
|
||||
SurvexFile, SurvexPersonRole)
|
||||
from troggle.core.models.logbooks import LogbookEntry # , PersonLogEntry
|
||||
from troggle.core.models.survex import SurvexBlock, SurvexDirectory, SurvexFile, SurvexPersonRole
|
||||
from troggle.core.models.troggle import Expedition, Person, PersonExpedition
|
||||
from troggle.core.utils import WriteAndCommitError, only_commit
|
||||
from troggle.parsers.people import GetPersonExpeditionNameLookup
|
||||
|
||||
'''Everything that views survexfiles
|
||||
"""Everything that views survexfiles
|
||||
but also displays data on a cave or caves when there is ambiguity
|
||||
'''
|
||||
"""
|
||||
|
||||
todo='''survexcavesingle is not properly producing any result for Homecoming, 1626-359, 2018-dm-07
|
||||
todo = """survexcavesingle is not properly producing any result for Homecoming, 1626-359, 2018-dm-07
|
||||
even though there are dozens of surveys.
|
||||
'''
|
||||
"""
|
||||
|
||||
survexdatasetpath = Path(settings.SURVEX_DATA)
|
||||
|
||||
@@ -105,44 +104,45 @@ survextemplatefile = """; *** THIS IS A TEMPLATE FILE NOT WHAT YOU MIGHT BE EXPE
|
||||
|
||||
|
||||
class SvxForm(forms.Form):
|
||||
'''Two-pane form, upper half is the raw survex file, lower half (with green background)
|
||||
"""Two-pane form, upper half is the raw survex file, lower half (with green background)
|
||||
is the output of running 'cavern' on the survex file.
|
||||
'''
|
||||
dirname = forms.CharField(widget=forms.TextInput(attrs={"readonly":True}))
|
||||
filename = forms.CharField(widget=forms.TextInput(attrs={"readonly":True}))
|
||||
datetime = forms.DateTimeField(widget=forms.TextInput(attrs={"readonly":True}))
|
||||
outputtype = forms.CharField(widget=forms.TextInput(attrs={"readonly":True}))
|
||||
code = forms.CharField(widget=forms.Textarea(attrs={"cols":140, "rows":36}))
|
||||
|
||||
"""
|
||||
|
||||
dirname = forms.CharField(widget=forms.TextInput(attrs={"readonly": True}))
|
||||
filename = forms.CharField(widget=forms.TextInput(attrs={"readonly": True}))
|
||||
datetime = forms.DateTimeField(widget=forms.TextInput(attrs={"readonly": True}))
|
||||
outputtype = forms.CharField(widget=forms.TextInput(attrs={"readonly": True}))
|
||||
code = forms.CharField(widget=forms.Textarea(attrs={"cols": 140, "rows": 36}))
|
||||
|
||||
template = False
|
||||
|
||||
|
||||
def GetDiscCode(self):
|
||||
fname = survexdatasetpath / (self.data['filename'] + ".svx")
|
||||
fname = survexdatasetpath / (self.data["filename"] + ".svx")
|
||||
if not fname.is_file():
|
||||
print(">>> >>> WARNING - svx file not found, showing TEMPLATE SVX",fname, flush=True)
|
||||
print(">>> >>> WARNING - svx file not found, showing TEMPLATE SVX", fname, flush=True)
|
||||
self.template = True
|
||||
return survextemplatefile
|
||||
try:
|
||||
fin = open(fname, "r",encoding='utf8',newline='')
|
||||
svxtext = fin.read()
|
||||
fin = open(fname, "r", encoding="utf8", newline="")
|
||||
svxtext = fin.read()
|
||||
fin.close()
|
||||
except:
|
||||
# hack. Replace this with something better.
|
||||
fin = open(fname, "r",encoding='iso-8859-1',newline='')
|
||||
svxtext = fin.read()
|
||||
fin = open(fname, "r", encoding="iso-8859-1", newline="")
|
||||
svxtext = fin.read()
|
||||
fin.close()
|
||||
return svxtext
|
||||
|
||||
|
||||
def DiffCode(self, rcode):
|
||||
code = self.GetDiscCode()
|
||||
difftext = difflib.unified_diff(code.splitlines(), rcode.splitlines())
|
||||
difflist = [ diffline.strip() for diffline in difftext if not re.match(r"\s*$", diffline) ]
|
||||
difflist = [diffline.strip() for diffline in difftext if not re.match(r"\s*$", diffline)]
|
||||
return difflist
|
||||
|
||||
def SaveCode(self, rcode):
|
||||
fname = survexdatasetpath / (self.data['filename'] + ".svx")
|
||||
fname = survexdatasetpath / (self.data["filename"] + ".svx")
|
||||
if not os.path.isfile(fname):
|
||||
if re.search(r"\[|\]", rcode):
|
||||
if re.search(r"\[|\]", rcode):
|
||||
return "Error: remove all []s from the text. They are only template guidance."
|
||||
mbeginend = re.search(r"(?s)\*begin\s+(\w+).*?\*end\s+(\w+)", rcode)
|
||||
if not mbeginend:
|
||||
@@ -152,228 +152,243 @@ class SvxForm(forms.Form):
|
||||
|
||||
# Make this create new survex folders if needed
|
||||
try:
|
||||
fout = open(fname, "w", encoding='utf8',newline='\n')
|
||||
fout = open(fname, "w", encoding="utf8", newline="\n")
|
||||
except FileNotFoundError:
|
||||
pth = os.path.dirname(self.data['filename'])
|
||||
pth = os.path.dirname(self.data["filename"])
|
||||
newpath = survexdatasetpath / pth
|
||||
if not os.path.exists(newpath):
|
||||
os.makedirs(newpath)
|
||||
fout = open(fname, "w", encoding='utf8',newline='\n')
|
||||
os.makedirs(newpath)
|
||||
fout = open(fname, "w", encoding="utf8", newline="\n")
|
||||
except PermissionError:
|
||||
return "CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file. Ask a nerd to fix this."
|
||||
return (
|
||||
"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file. Ask a nerd to fix this."
|
||||
)
|
||||
|
||||
# javascript seems to insert CRLF on WSL1 whatever you say. So fix that:
|
||||
res = fout.write(rcode.replace("\r",""))
|
||||
res = fout.write(rcode.replace("\r", ""))
|
||||
res = fout.write("\n")
|
||||
fout.close()
|
||||
|
||||
|
||||
if socket.gethostname() == "expo":
|
||||
comment = f"Online survex edit: {self.data['filename']}.svx"
|
||||
else:
|
||||
comment = f"Online survex edit: {self.data['filename']}.svx on dev machine '{socket.gethostname()}' "
|
||||
comment = f"Online survex edit: {self.data['filename']}.svx on dev machine '{socket.gethostname()}' "
|
||||
only_commit(fname, comment)
|
||||
|
||||
|
||||
return "SAVED and committed to git"
|
||||
|
||||
def Process(self):
|
||||
print(">>>>....\n....Processing\n")
|
||||
froox = os.fspath(survexdatasetpath / (self.data['filename'] + ".svx"))
|
||||
froog = os.fspath(survexdatasetpath / (self.data['filename'] + ".log"))
|
||||
froox = os.fspath(survexdatasetpath / (self.data["filename"] + ".svx"))
|
||||
froog = os.fspath(survexdatasetpath / (self.data["filename"] + ".log"))
|
||||
cwd = os.getcwd()
|
||||
os.chdir(os.path.split(froox)[0])
|
||||
os.system(settings.CAVERN + " --log " + froox )
|
||||
os.system(settings.CAVERN + " --log " + froox)
|
||||
os.chdir(cwd)
|
||||
|
||||
# Update this to use the new syntax..
|
||||
# sp = subprocess.run([settings.CAVERN, "--log", f'--output={outputdir}', f'{fullpath}.svx'],
|
||||
# capture_output=True, check=False, text=True)
|
||||
# if sp.returncode != 0:
|
||||
# message = f' ! Error running {settings.CAVERN}: {fullpath}'
|
||||
|
||||
# Update this to use the new syntax..
|
||||
# sp = subprocess.run([settings.CAVERN, "--log", f'--output={outputdir}', f'{fullpath}.svx'],
|
||||
# capture_output=True, check=False, text=True)
|
||||
# if sp.returncode != 0:
|
||||
# message = f' ! Error running {settings.CAVERN}: {fullpath}'
|
||||
# DataIssue.objects.create(parser='entrances', message=message)
|
||||
# print(message)
|
||||
# print(f'stderr:\n\n' + str(sp.stderr) + '\n\n' + str(sp.stdout) + '\n\nreturn code: ' + str(sp.returncode))
|
||||
|
||||
filepatherr = Path(survexdatasetpath / str(self.data['filename'] + ".err"))
|
||||
filepatherr = Path(survexdatasetpath / str(self.data["filename"] + ".err"))
|
||||
if filepatherr.is_file():
|
||||
if filepatherr.stat().st_size == 0:
|
||||
filepatherr.unlink() # delete empty closure error file
|
||||
filepatherr.unlink() # delete empty closure error file
|
||||
|
||||
fin = open(froog, "r",encoding='utf8')
|
||||
fin = open(froog, "r", encoding="utf8")
|
||||
log = fin.read()
|
||||
fin.close()
|
||||
#log = re.sub("(?s).*?(Survey contains)", "\\1", log) # this omits any ERROR MESSAGES ! Don't do it.
|
||||
for s in ["Removing trailing traverses...\n\n",
|
||||
"Concatenating traverses...\n\n"
|
||||
"Simplifying network...\n\n",
|
||||
# log = re.sub("(?s).*?(Survey contains)", "\\1", log) # this omits any ERROR MESSAGES ! Don't do it.
|
||||
for s in [
|
||||
"Removing trailing traverses...\n\n",
|
||||
"Concatenating traverses...\n\n" "Simplifying network...\n\n",
|
||||
"Calculating network...\n\n",
|
||||
"Calculating traverses...\n\n",
|
||||
"Calculating trailing traverses...\n\n",
|
||||
"Calculating statistics...\n\n"]:
|
||||
log = log.replace(s,"")
|
||||
"Calculating statistics...\n\n",
|
||||
]:
|
||||
log = log.replace(s, "")
|
||||
return log
|
||||
|
||||
|
||||
@ensure_csrf_cookie
|
||||
def svx(request, survex_file):
|
||||
'''Displays a single survex file in an textarea window (using a javascript online editor to enable
|
||||
"""Displays a single survex file in an textarea window (using a javascript online editor to enable
|
||||
editing) with buttons which allow SAVE, check for DIFFerences from saved, and RUN (which runs the
|
||||
cavern executable and displays the output below the main textarea).
|
||||
cavern executable and displays the output below the main textarea).
|
||||
Requires CSRF to be set up correct;ly, and requires permission to write to the filesystem.
|
||||
'''
|
||||
"""
|
||||
warning = False
|
||||
|
||||
|
||||
# get the basic data from the file given in the URL
|
||||
dirname = os.path.split(survex_file)[0]
|
||||
dirname += "/"
|
||||
nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
nowtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
outputtype = "normal"
|
||||
form = SvxForm({'filename':survex_file, 'dirname':dirname, 'datetime':nowtime, 'outputtype':outputtype})
|
||||
|
||||
form = SvxForm({"filename": survex_file, "dirname": dirname, "datetime": nowtime, "outputtype": outputtype})
|
||||
|
||||
# if the form has been returned
|
||||
difflist = [ ]
|
||||
difflist = []
|
||||
logmessage = ""
|
||||
message = ""
|
||||
|
||||
if request.method == 'POST': # If the form has been submitted...
|
||||
rform = SvxForm(request.POST) #
|
||||
if rform.is_valid(): # All validation rules pass (how do we check it against the filename and users?)
|
||||
rcode = rform.cleaned_data['code']
|
||||
outputtype = rform.cleaned_data['outputtype']
|
||||
if request.method == "POST": # If the form has been submitted...
|
||||
rform = SvxForm(request.POST) #
|
||||
if rform.is_valid(): # All validation rules pass (how do we check it against the filename and users?)
|
||||
rcode = rform.cleaned_data["code"]
|
||||
outputtype = rform.cleaned_data["outputtype"]
|
||||
difflist = form.DiffCode(rcode)
|
||||
#print(">>>> ", rform.data)
|
||||
|
||||
# print(">>>> ", rform.data)
|
||||
|
||||
if "revert" in rform.data:
|
||||
pass
|
||||
if "process" in rform.data:
|
||||
if not difflist:
|
||||
logmessage = form.Process()
|
||||
if logmessage:
|
||||
message = f"OUTPUT FROM PROCESSING\n{logmessage}"
|
||||
message = f"OUTPUT FROM PROCESSING\n{logmessage}"
|
||||
else:
|
||||
message = "SAVE FILE FIRST"
|
||||
form.data['code'] = rcode
|
||||
form.data["code"] = rcode
|
||||
if "save" in rform.data:
|
||||
if request.user.is_authenticated:
|
||||
message = form.SaveCode(rcode)
|
||||
else:
|
||||
message = "You do not have authority to save this file. Please log in."
|
||||
if message != "SAVED":
|
||||
form.data['code'] = rcode
|
||||
form.data["code"] = rcode
|
||||
if "diff" in rform.data:
|
||||
print("Differences: ")
|
||||
form.data['code'] = rcode
|
||||
|
||||
#process(survex_file)
|
||||
if 'code' not in form.data:
|
||||
form.data['code'] = form.GetDiscCode()
|
||||
form.data["code"] = rcode
|
||||
|
||||
# process(survex_file)
|
||||
if "code" not in form.data:
|
||||
form.data["code"] = form.GetDiscCode()
|
||||
if form.template:
|
||||
warning = True
|
||||
if not difflist:
|
||||
difflist.append("No differences - file was saved")
|
||||
if message:
|
||||
difflist.insert(0, message)
|
||||
|
||||
#print [ form.data['code'] ]
|
||||
svxincludes = re.findall(r'(?i)\*include\s+(\S+)', form.data['code'] or "")
|
||||
|
||||
vmap = {'settings': settings,
|
||||
'warning': warning,
|
||||
'has_3d': (Path(survexdatasetpath) / Path(survex_file + ".3d")).is_file(),
|
||||
'title': survex_file,
|
||||
'svxincludes': svxincludes,
|
||||
'difflist': difflist,
|
||||
'logmessage':logmessage,
|
||||
'form':form}
|
||||
# vmap.update(csrf(request)) # this now refreshes to the wrong value, now that we user render(request,
|
||||
|
||||
|
||||
# print [ form.data['code'] ]
|
||||
svxincludes = re.findall(r"(?i)\*include\s+(\S+)", form.data["code"] or "")
|
||||
|
||||
vmap = {
|
||||
"settings": settings,
|
||||
"warning": warning,
|
||||
"has_3d": (Path(survexdatasetpath) / Path(survex_file + ".3d")).is_file(),
|
||||
"title": survex_file,
|
||||
"svxincludes": svxincludes,
|
||||
"difflist": difflist,
|
||||
"logmessage": logmessage,
|
||||
"form": form,
|
||||
}
|
||||
# vmap.update(csrf(request)) # this now refreshes to the wrong value, now that we user render(request,
|
||||
|
||||
if outputtype == "ajax":
|
||||
return render(request, 'svxfiledifflistonly.html', vmap)
|
||||
|
||||
return render(request, 'svxfile.html', vmap)
|
||||
return render(request, "svxfiledifflistonly.html", vmap)
|
||||
|
||||
return render(request, "svxfile.html", vmap)
|
||||
|
||||
|
||||
# The cavern running function. This is NOT where it is run inside the form! see SvxForm.Process() for that
|
||||
def process(survex_file):
|
||||
'''This runs cavern only where a .3d, .log or .err file is requested.
|
||||
'''
|
||||
filepathsvx = survexdatasetpath / str( survex_file + ".svx")
|
||||
"""This runs cavern only where a .3d, .log or .err file is requested."""
|
||||
filepathsvx = survexdatasetpath / str(survex_file + ".svx")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(os.path.split(os.fspath(survexdatasetpath / survex_file))[0])
|
||||
os.chdir(os.path.split(os.fspath(survexdatasetpath / survex_file))[0])
|
||||
os.system(settings.CAVERN + " --log " + str(filepathsvx))
|
||||
os.chdir(cwd)
|
||||
|
||||
|
||||
# Update this to use the new syntax..
|
||||
# sp = subprocess.run([settings.CAVERN, "--log", f'--output={outputdir}', f'{fullpath}.svx'],
|
||||
# capture_output=True, check=False, text=True)
|
||||
# capture_output=True, check=False, text=True)
|
||||
# if sp.returncode != 0:
|
||||
# message = f' ! Error running {settings.CAVERN}: {fullpath}'
|
||||
# DataIssue.objects.create(parser='entrances', message=message)
|
||||
# print(message)
|
||||
# print(f'stderr:\n\n' + str(sp.stderr) + '\n\n' + str(sp.stdout) + '\n\nreturn code: ' + str(sp.returncode))
|
||||
# message = f' ! Error running {settings.CAVERN}: {fullpath}'
|
||||
# DataIssue.objects.create(parser='entrances', message=message)
|
||||
# print(message)
|
||||
# print(f'stderr:\n\n' + str(sp.stderr) + '\n\n' + str(sp.stdout) + '\n\nreturn code: ' + str(sp.returncode))
|
||||
|
||||
filepatherr = Path(survexdatasetpath / str(survex_file + ".err"))
|
||||
if filepatherr.is_file():
|
||||
if filepatherr.stat().st_size == 0:
|
||||
filepatherr.unlink() # delete empty closure error file
|
||||
|
||||
filepatherr.unlink() # delete empty closure error file
|
||||
|
||||
|
||||
def threed(request, survex_file):
|
||||
filepath3d = survexdatasetpath / str(survex_file + ".3d")
|
||||
filepathlog = survexdatasetpath / str(survex_file + ".log")
|
||||
filepath3d = survexdatasetpath / str(survex_file + ".3d")
|
||||
filepathlog = survexdatasetpath / str(survex_file + ".log")
|
||||
if filepath3d.is_file():
|
||||
threed = open(filepath3d, "rb")
|
||||
return HttpResponse(threed, content_type="application/x-aven")
|
||||
else:
|
||||
process(survex_file) # should not need to do this if it already exists, as it should.
|
||||
log = open(survexdatasetpath / str(survex_file + ".log"), "r",encoding='utf-8')
|
||||
process(survex_file) # should not need to do this if it already exists, as it should.
|
||||
log = open(survexdatasetpath / str(survex_file + ".log"), "r", encoding="utf-8")
|
||||
return HttpResponse(log, content_type="text")
|
||||
|
||||
|
||||
def svxlog(request, survex_file):
|
||||
'''Used for rendering .log files from survex outputtype'''
|
||||
filepathlog = survexdatasetpath / str(survex_file + ".log")
|
||||
"""Used for rendering .log files from survex outputtype"""
|
||||
filepathlog = survexdatasetpath / str(survex_file + ".log")
|
||||
if not filepathlog.is_file():
|
||||
process(survex_file)
|
||||
process(survex_file)
|
||||
log = open(filepathlog, "r")
|
||||
return HttpResponse(log,content_type="text/plain; charset=utf-8") #default: "text/html; charset=utf-8"
|
||||
return HttpResponse(log, content_type="text/plain; charset=utf-8") # default: "text/html; charset=utf-8"
|
||||
|
||||
|
||||
def err(request, survex_file):
|
||||
filepatherr = survexdatasetpath / str(survex_file + ".err")
|
||||
if not filepatherr.is_file(): # probably not there because it was empty, but re-run anyway
|
||||
process(survex_file)
|
||||
filepatherr = survexdatasetpath / str(survex_file + ".err")
|
||||
if not filepatherr.is_file(): # probably not there because it was empty, but re-run anyway
|
||||
process(survex_file)
|
||||
process(survex_file)
|
||||
if filepatherr.is_file():
|
||||
err = open(filepatherr, "r")
|
||||
return HttpResponse(err, content_type="text/plain; charset=utf-8")
|
||||
else:
|
||||
return HttpResponse(f'No closure errors. \nEmpty {filepatherr} file produced. \nSee the .log file.', content_type="text/plain; charset=utf-8")
|
||||
return HttpResponse(
|
||||
f"No closure errors. \nEmpty {filepatherr} file produced. \nSee the .log file.",
|
||||
content_type="text/plain; charset=utf-8",
|
||||
)
|
||||
|
||||
|
||||
def identifycavedircontents(gcavedir):
|
||||
# find the primary survex file in each cave directory
|
||||
# this should be in a configuration, not buried in the code...
|
||||
name = os.path.split(gcavedir)[1]
|
||||
subdirs = [ ]
|
||||
subsvx = [ ]
|
||||
subdirs = []
|
||||
subsvx = []
|
||||
primesvx = None
|
||||
for f in os.listdir(gcavedir): # These may get outdated as data gets tidied up. This should not be in the code!
|
||||
for f in os.listdir(gcavedir): # These may get outdated as data gets tidied up. This should not be in the code!
|
||||
if name == "204" and (f in ["skel.svx", "template.svx", "204withents.svx"]):
|
||||
pass
|
||||
elif name == "136" and (f in ["136-noents.svx"]):
|
||||
pass
|
||||
elif name == "115" and (f in ["115cufix.svx", "115fix.svx"]):
|
||||
pass
|
||||
|
||||
|
||||
elif os.path.isdir(os.path.join(gcavedir, f)):
|
||||
if f[0] != ".":
|
||||
subdirs.append(f)
|
||||
elif f[-4:] == ".svx":
|
||||
nf = f[:-4]
|
||||
|
||||
if nf.lower() == name.lower() or nf[:3] == "all" or (name, nf) in [("resurvey2005", "145-2005"), ("cucc", "cu115")]:
|
||||
|
||||
if (
|
||||
nf.lower() == name.lower()
|
||||
or nf[:3] == "all"
|
||||
or (name, nf) in [("resurvey2005", "145-2005"), ("cucc", "cu115")]
|
||||
):
|
||||
if primesvx:
|
||||
if nf[:3] == "all":
|
||||
#assert primesvx[:3] != "all", (name, nf, primesvx, gcavedir, subsvx)
|
||||
# assert primesvx[:3] != "all", (name, nf, primesvx, gcavedir, subsvx)
|
||||
primesvx = nf
|
||||
else:
|
||||
#assert primesvx[:3] == "all", (name, nf, primesvx, gcavedir, subsvx)
|
||||
# assert primesvx[:3] == "all", (name, nf, primesvx, gcavedir, subsvx)
|
||||
pass
|
||||
else:
|
||||
primesvx = nf
|
||||
@@ -381,151 +396,165 @@ def identifycavedircontents(gcavedir):
|
||||
subsvx.append(nf)
|
||||
else:
|
||||
pass
|
||||
#assert re.match(".*?(?:.3d|.log|.err|.txt|.tmp|.diff|.e?spec|~)$", f), (gcavedir, f)
|
||||
# assert re.match(".*?(?:.3d|.log|.err|.txt|.tmp|.diff|.e?spec|~)$", f), (gcavedir, f)
|
||||
subsvx.sort()
|
||||
#assert primesvx, (gcavedir, subsvx)
|
||||
# assert primesvx, (gcavedir, subsvx)
|
||||
if primesvx:
|
||||
subsvx.insert(0, primesvx)
|
||||
return subdirs, subsvx
|
||||
|
||||
|
||||
|
||||
def get_survexareapath(area):
|
||||
return survexdatasetpath / str("caves-" + area)
|
||||
return survexdatasetpath / str("caves-" + area)
|
||||
|
||||
|
||||
# direct local non-database browsing through the svx file repositories
|
||||
# every time the page is viewed! Should cache this.
|
||||
def survexcaveslist(request):
|
||||
'''This reads the entire list of caves in the Loser repo directory and produces a complete report.
|
||||
"""This reads the entire list of caves in the Loser repo directory and produces a complete report.
|
||||
It can find caves which have not yet been properly registered in the system by Databasereset.py because
|
||||
someone may have uploaded the survex files without doing the rest of the integration process.
|
||||
'''
|
||||
"""
|
||||
# TO DO - filter out the non-public caves from display UNLESS LOGGED IN
|
||||
# This is very impenetrable code, original from Aaron Curtis I think.
|
||||
onefilecaves = [ ]
|
||||
multifilecaves = [ ]
|
||||
subdircaves = [ ]
|
||||
fnumlist = [ ]
|
||||
|
||||
onefilecaves = []
|
||||
multifilecaves = []
|
||||
subdircaves = []
|
||||
fnumlist = []
|
||||
|
||||
for area in ["1623", "1626", "1624", "1627"]:
|
||||
cavesdir = get_survexareapath(area)
|
||||
arealist = sorted([ (area, -int(re.match(r"\d*", f).group(0) or "0"), f) for f in os.listdir(cavesdir) ])
|
||||
arealist = sorted([(area, -int(re.match(r"\d*", f).group(0) or "0"), f) for f in os.listdir(cavesdir)])
|
||||
fnumlist += arealist
|
||||
|
||||
#print(fnumlist)
|
||||
|
||||
|
||||
# print(fnumlist)
|
||||
|
||||
# go through the list and identify the contents of each cave directory
|
||||
for area, num, cavedir in fnumlist:
|
||||
|
||||
|
||||
# these have sub dirs /cucc/ /arge/ /old/ but that is no reason to hide them in this webpage
|
||||
# so these are now treated the same as 142 and 113 which also have a /cucc/ sub dir
|
||||
#if cavedir in ["144", "40"]:
|
||||
# if cavedir in ["144", "40"]:
|
||||
# continue
|
||||
|
||||
# This all assumes that the first .svx file has the same name as the cave name,
|
||||
# This all assumes that the first .svx file has the same name as the cave name,
|
||||
# which usually but not always true. e.g. caves-1623/78/allkaese.svx not caves-1623/78/78.svx
|
||||
# which is why we now also pass through the cavedir
|
||||
|
||||
|
||||
# Still fails for loutitohoehle etc even though this is set correctly when the pending cave is created
|
||||
cavesdir = get_survexareapath(area)
|
||||
gcavedir = os.path.join(cavesdir, cavedir)
|
||||
if os.path.isdir(gcavedir) and cavedir[0] != ".":
|
||||
subdirs, subsvx = identifycavedircontents(gcavedir)
|
||||
|
||||
caveid = check_cave_registered(area, cavedir) # should do this only once per database load or it will be slow
|
||||
survdirobj = [ ]
|
||||
|
||||
caveid = check_cave_registered(
|
||||
area, cavedir
|
||||
) # should do this only once per database load or it will be slow
|
||||
survdirobj = []
|
||||
for lsubsvx in subsvx:
|
||||
survdirobj.append(("caves-" +area+ "/" +cavedir+"/"+lsubsvx, lsubsvx))
|
||||
|
||||
survdirobj.append(("caves-" + area + "/" + cavedir + "/" + lsubsvx, lsubsvx))
|
||||
|
||||
# caves with subdirectories
|
||||
if subdirs:
|
||||
subsurvdirs = [ ]
|
||||
subsurvdirs = []
|
||||
for subdir in subdirs:
|
||||
dsubdirs, dsubsvx = identifycavedircontents(os.path.join(gcavedir, subdir))
|
||||
# assert not dsubdirs # handle case of empty sub directory
|
||||
lsurvdirobj = [ ]
|
||||
lsurvdirobj = []
|
||||
for lsubsvx in dsubsvx:
|
||||
lsurvdirobj.append(("caves-" +area+ "/" +cavedir+"/"+subdir+"/"+lsubsvx, lsubsvx))
|
||||
lsurvdirobj.append(("caves-" + area + "/" + cavedir + "/" + subdir + "/" + lsubsvx, lsubsvx))
|
||||
if len(dsubsvx) >= 1:
|
||||
subsurvdirs.append((subdir,lsurvdirobj[0], lsurvdirobj[0:])) # list now includes the first item too
|
||||
subsurvdirs.append(
|
||||
(subdir, lsurvdirobj[0], lsurvdirobj[0:])
|
||||
) # list now includes the first item too
|
||||
subdircaves.append((cavedir, (survdirobj[0], survdirobj[1:]), subsurvdirs))
|
||||
|
||||
|
||||
# multifile caves
|
||||
elif len(survdirobj) > 1:
|
||||
multifilecaves.append((survdirobj[0], cavedir, survdirobj[1:]))
|
||||
# single file caves
|
||||
elif len(survdirobj) == 1:
|
||||
onefilecaves.append(survdirobj[0])
|
||||
|
||||
return render(request, 'svxfilecavelist.html', {'settings': settings, "onefilecaves":onefilecaves, "multifilecaves":multifilecaves, "subdircaves":subdircaves })
|
||||
|
||||
return render(
|
||||
request,
|
||||
"svxfilecavelist.html",
|
||||
{
|
||||
"settings": settings,
|
||||
"onefilecaves": onefilecaves,
|
||||
"multifilecaves": multifilecaves,
|
||||
"subdircaves": subdircaves,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def survexcavesingle(request, survex_cave):
|
||||
'''parsing all the survex files of a single cave and showing that it's consistent and can find all
|
||||
"""parsing all the survex files of a single cave and showing that it's consistent and can find all
|
||||
the files and people. Should explicitly fix the kataster number thing.
|
||||
kataster numbers are not unique across areas. This used to be a db constraint but we need to manage
|
||||
kataster numbers are not unique across areas. This used to be a db constraint but we need to manage
|
||||
this ourselves as we don't want the parser aborting with an error message.
|
||||
|
||||
|
||||
Should use getCave() from models_caves
|
||||
'''
|
||||
"""
|
||||
sc = survex_cave
|
||||
try:
|
||||
cave = Cave.objects.get(kataster_number=sc) # This may not be unique.
|
||||
return render(request, 'svxcavesingle.html', {'settings': settings, "cave":cave })
|
||||
cave = Cave.objects.get(kataster_number=sc) # This may not be unique.
|
||||
return render(request, "svxcavesingle.html", {"settings": settings, "cave": cave})
|
||||
|
||||
except ObjectDoesNotExist:
|
||||
# can get here if the survex file is in a directory labelled with unofficial number not kataster number.
|
||||
# maybe - and _ mixed up, or CUCC-2017- instead of 2017-CUCC-, or CUCC2015DL01 . Let's not get carried away..
|
||||
|
||||
|
||||
# or it might be an exact search for a specific survefile but just missing the '.svx.
|
||||
if (Path(survexdatasetpath) / Path(survex_cave + ".svx")).is_file():
|
||||
return svx(request, survex_cave)
|
||||
|
||||
for unoff in [sc, sc.replace('-','_'), sc.replace('_','-'), sc.replace('-',''), sc.replace('_','')]:
|
||||
|
||||
for unoff in [sc, sc.replace("-", "_"), sc.replace("_", "-"), sc.replace("-", ""), sc.replace("_", "")]:
|
||||
try:
|
||||
cave = Cave.objects.get(unofficial_number=unoff) # return on first one we find
|
||||
return render(request, 'svxcavesingle.html', {'settings': settings, "cave":cave })
|
||||
cave = Cave.objects.get(unofficial_number=unoff) # return on first one we find
|
||||
return render(request, "svxcavesingle.html", {"settings": settings, "cave": cave})
|
||||
except ObjectDoesNotExist:
|
||||
continue # next attempt in for loop
|
||||
return render(request, 'errors/svxcavesingle404.html', {'settings': settings, "cave":sc })
|
||||
continue # next attempt in for loop
|
||||
return render(request, "errors/svxcavesingle404.html", {"settings": settings, "cave": sc})
|
||||
|
||||
except MultipleObjectsReturned:
|
||||
caves = Cave.objects.filter(kataster_number=survex_cave)
|
||||
return render(request, 'svxcaveseveral.html', {'settings': settings, "caves":caves })
|
||||
caves = Cave.objects.filter(kataster_number=survex_cave)
|
||||
return render(request, "svxcaveseveral.html", {"settings": settings, "caves": caves})
|
||||
|
||||
except:
|
||||
return render(request, 'errors/svxcavesingle404.html', {'settings': settings, "cave":sc })
|
||||
return render(request, "errors/svxcavesingle404.html", {"settings": settings, "cave": sc})
|
||||
|
||||
|
||||
|
||||
def check_cave_registered(area, survex_cave):
|
||||
'''Checks whether a cave has been properly registered when it is found in the Loser repo
|
||||
"""Checks whether a cave has been properly registered when it is found in the Loser repo
|
||||
This should really be called by Databasereset not here in a view
|
||||
Currently Caves are only registered if they are listed in :expoweb: settings.CAVEDESCRIPTIONS
|
||||
so we need to add in any more here.
|
||||
|
||||
|
||||
This function runs but does not seem to be used?!
|
||||
A serious bodge anyway.
|
||||
'''
|
||||
"""
|
||||
try:
|
||||
cave = Cave.objects.get(kataster_number=survex_cave)
|
||||
cave = Cave.objects.get(kataster_number=survex_cave)
|
||||
return str(cave)
|
||||
|
||||
except MultipleObjectsReturned:
|
||||
caves = Cave.objects.filter(kataster_number=survex_cave)
|
||||
for c in caves:
|
||||
if str(c) == area + "-" + survex_cave :
|
||||
return str(c) # just get the first that matches
|
||||
return None # many returned but none in correct area
|
||||
|
||||
caves = Cave.objects.filter(kataster_number=survex_cave)
|
||||
for c in caves:
|
||||
if str(c) == area + "-" + survex_cave:
|
||||
return str(c) # just get the first that matches
|
||||
return None # many returned but none in correct area
|
||||
|
||||
except ObjectDoesNotExist:
|
||||
pass
|
||||
|
||||
try:
|
||||
cave = Cave.objects.get(unofficial_number=survex_cave) # should be unique!
|
||||
cave = Cave.objects.get(unofficial_number=survex_cave) # should be unique!
|
||||
if cave.kataster_number:
|
||||
return str(cave)
|
||||
else:
|
||||
return None
|
||||
except ObjectDoesNotExist:
|
||||
pass
|
||||
|
||||
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user