diff --git a/core/views/caves.py b/core/views/caves.py
index 07ea315..e053ffd 100644
--- a/core/views/caves.py
+++ b/core/views/caves.py
@@ -376,8 +376,7 @@ def edit_cave(request, path="", slug=None):
ceinst.save()
try:
cave_file = cave.file_output()
- # print(cave_file)
- write_and_commit([cave_file], f"Online edit of {cave}")
+ write_and_commit([cave_file], f"Online edit of cave {cave}")
# leave other exceptions unhandled so that they bubble up to user interface
except PermissionError:
message = f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {cave.filename}. Ask a nerd to fix this."
@@ -411,7 +410,7 @@ def edit_cave(request, path="", slug=None):
@login_required_if_public
-def edit_entrance(request, path="", caveslug=None, slug=None):
+def edit_entrance(request, path="", caveslug=None, entslug=None):
"""This is the form that edits the entrance data for a single entrance and writes out
an XML file in the :expoweb: repo folder
@@ -425,9 +424,15 @@ def edit_entrance(request, path="", caveslug=None, slug=None):
try:
cave = Cave.objects.get(caveslug__slug=caveslug)
except:
- return render(request, "errors/badslug.html", {"badslug": f"{slug} {caveslug} - from edit_entrance()"})
+ return render(request, "errors/badslug.html", {"badslug": f"for cave {caveslug} - from edit_entrance()"})
- if slug:
+ try:
+ entrance = Entrance.objects.get(slug=entslug)
+ except:
+ return render(request, "errors/badslug.html", {"badslug": f"for entrance {slug} - from edit_entrance()"})
+
+ if entslug:
+ # print(f"{caveslug=} {entslug=} {path=}")
caveAndEntrance = CaveAndEntrance.objects.get(entrance=entrance, cave=cave)
entlettereditable = False
else:
@@ -441,24 +446,31 @@ def edit_entrance(request, path="", caveslug=None, slug=None):
if form.is_valid() and entletter.is_valid():
entrance = form.save(commit=False)
entrance_letter = entletter.save(commit=False)
- if slug is None:
+ print(f"- POST {caveslug=} {entslug=} {path=}")
+ if entslug is None:
if entletter.cleaned_data["entrance_letter"]:
slugname = cave.slug() + entletter.cleaned_data["entrance_letter"]
else:
slugname = cave.slug()
+ entrance.slug = slugname
entrance.cached_primary_slug = slugname
entrance.filename = slugname + ".html"
entrance.save()
entrance_file = entrance.file_output()
+ print(f"Online edit of entrance {entrance.slug}")
cave_file = cave.file_output()
- write_and_commit([entrance_file, cave_file], f"Online edit of {cave}{entletter}")
entrance.save()
- if slug is None:
+ write_and_commit([entrance_file, cave_file], f"Online edit of entrance {entrance.slug}")
+ if entslug is None:
entrance_letter.save()
return HttpResponseRedirect("/" + cave.url)
else:
+ # re-read entrance data from file.
+ filename = str(entrance.slug +".html")
+ readentrance(filename, ent=entrance)
+
form = EntranceForm(instance=entrance)
- if slug is None:
+ if entslug is None:
entletter = EntranceLetterForm()
else:
entletter = caveAndEntrance.entrance_letter
diff --git a/parsers/caves.py b/parsers/caves.py
index be1bc5a..add6f36 100644
--- a/parsers/caves.py
+++ b/parsers/caves.py
@@ -307,6 +307,14 @@ def getXML(text, itemname, minItems=1, maxItems=None, context=""):
items = [""]
return items
+
+def boolify(boolstrs):
+ return {
+ "True": True,
+ "False": False,
+ "true": True,
+ "false": False}[boolstrs[0]]
+
def readentrance(filename, ent=None):
"""Reads an entrance description from the .html file
@@ -315,7 +323,6 @@ def readentrance(filename, ent=None):
"""
def getXMLmax1(field):
return getXML(entrancecontents, field, maxItems=1, context=context)
- # return getXML(entrancecontents, field, maxItems=1, context=context)[0]
global entrances_xslug
global caves_xslug
@@ -325,55 +332,81 @@ def readentrance(filename, ent=None):
with open(os.path.join(ENTRANCEDESCRIPTIONS, filename)) as f:
contents = f.read()
context = filename
+
# print("Reading file ENTRANCE {} / {}".format(ENTRANCEDESCRIPTIONS, filename))
entrancecontentslist = getXML(contents, "entrance", maxItems=1, context=context)
if len(entrancecontentslist) != 1:
message = f'! BAD ENTRANCE at "{filename}". Loading aborted. '
DataIssue.objects.create(parser="entrances", message=message)
print(message)
- else:
- entrancecontents = entrancecontentslist[0]
- lastvisit = getXML(entrancecontents, "last visit date", maxItems=1, minItems=0, context=context)
- slugs = getXML(entrancecontents, "slug", context=context)
-
- alt = getXMLmax1("alt")
- approach = getXMLmax1("approach")
- bearings = getXMLmax1("bearings")
- easting = getXMLmax1("easting")
- entrance_description = getXMLmax1("entrance_description")
- exact_station = getXMLmax1("exact_station")
- explorers = getXMLmax1("explorers")
- findability = getXMLmax1("findability")
- findability_description = getXMLmax1("findability_description")
- location_description = getXMLmax1("location_description")
- map_description = getXMLmax1("map_description")
- marking = getXMLmax1("marking")
- marking_comment = getXMLmax1("marking_comment")
- name = getXMLmax1("name")
- non_public = getXMLmax1("non_public")
- northing = getXMLmax1("northing")
- other_description = getXMLmax1("other_description")
- other_station = getXMLmax1("other_station")
- photo = getXMLmax1("photo")
- tag_station = getXMLmax1("tag_station")
- underground_description = getXMLmax1("underground_description")
- url = getXMLmax1("url")
+ return
- if len(slugs) >1:
- # Only ever one of these per entrance in the expo dataset
- message = f" ! - More than one slug for an entrance: {entrance}, slugs: {slugs}. Aborting."
- DataIssue.objects.create(parser="entrances", message=message, url=f"/cave/{slug}/edit/")
- print(message)
- return
-
+ entrancecontents = entrancecontentslist[0]
+ slugs = getXML(entrancecontents, "slug", context=context)
+
+ if len(slugs) >1:
+ # Only ever one of these per entrance in the expo dataset
+ message = f" ! - More than one slug for an entrance: {entrance}, slugs: {slugs}. Aborting."
+ DataIssue.objects.create(parser="entrances", message=message, url=f"/cave/{slug}/edit/")
+ print(message)
+ return
+
+ lastvisit = getXML(entrancecontents, "last visit date", maxItems=1, minItems=0, context=context)
+
+ alt = getXMLmax1("alt")
+ approach = getXMLmax1("approach")
+ bearings = getXMLmax1("bearings")
+ easting = getXMLmax1("easting")
+ entrance_description = getXMLmax1("entrance_description")
+ exact_station = getXMLmax1("exact_station")
+ explorers = getXMLmax1("explorers")
+ findability = getXMLmax1("findability")
+ findability_description = getXMLmax1("findability_description")
+ location_description = getXMLmax1("location_description")
+ map_description = getXMLmax1("map_description")
+ marking = getXMLmax1("marking")
+ marking_comment = getXMLmax1("marking_comment")
+ name = getXMLmax1("name")
+ non_public = getXMLmax1("non_public")
+ northing = getXMLmax1("northing")
+ other_description = getXMLmax1("other_description")
+ other_station = getXMLmax1("other_station")
+ photo = getXMLmax1("photo")
+ tag_station = getXMLmax1("tag_station")
+ underground_description = getXMLmax1("underground_description")
+ url = getXMLmax1("url")
+
+ if ent:
+ ent.name=name[0]
+ ent.non_public=boolify(non_public)
+ ent.alt=alt[0]
+ ent.approach=approach[0]
+ ent.bearings=bearings[0]
+ ent.easting=easting[0]
+ ent.entrance_description=entrance_description[0]
+ ent.exact_station=exact_station[0]
+ ent.explorers=explorers[0]
+ ent.filename=filename
+ ent.findability=findability[0]
+ ent.findability_description=findability_description[0]
+ ent.lastvisit=lastvisit[0]
+ ent.location_description=location_description[0]
+ ent.map_description=map_description[0]
+ ent.marking=marking[0]
+ ent.marking_comment=marking_comment[0]
+ ent.northing=northing[0]
+ ent.other_description=other_description[0]
+ ent.other_station=other_station[0]
+ ent.photo=photo[0]
+ ent.slug=slugs[0]
+ ent.tag_station=tag_station[0]
+ ent.underground_description=underground_description[0]
+ ent.url=url[0]
+ ent.save()
+ else:
e, state = Entrance.objects.update_or_create(
name=name[0],
- non_public={
- "True": True,
- "False": False,
- "true": True,
- "false": False,
- }[non_public[0]],
+ non_public=boolify(non_public),
alt=alt[0],
approach=approach[0],
bearings=bearings[0],
@@ -398,7 +431,7 @@ def readentrance(filename, ent=None):
underground_description=underground_description[0],
url=url[0],
)
-
+ e.save()
def readcave(filename, cave=None):
"""Reads an entrance description from the .html file
@@ -409,9 +442,12 @@ def readcave(filename, cave=None):
but this is OK, a search will find them in the db.
"""
def do_entrances():
- for entrance in entrances:
- eslug = getXML(entrance, "entranceslug", maxItems=1, context=context)[0]
- letter = getXML(entrance, "letter", maxItems=1, context=context)[0]
+ """For both bulk import and individual re-reading of cave_data file,
+ fix the entrances
+ """
+ for e in entrances:
+ eslug = getXML(e, "entranceslug", maxItems=1, context=context)[0]
+ letter = getXML(e, "letter", maxItems=1, context=context)[0]
if len(entrances) == 1 and not eslug: # may be empty: