summaryrefslogtreecommitdiffstats
path: root/parsers/caves.py
diff options
context:
space:
mode:
Diffstat (limited to 'parsers/caves.py')
-rw-r--r--parsers/caves.py151
1 files changed, 94 insertions, 57 deletions
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)
+ 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
- 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 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: <entranceslug></entranceslug>
set_dummy_entrance(slug[5:], slug, c, msg="DUMMY: no entrance slug read from file")
else:
@@ -419,16 +455,24 @@ def readcave(filename, cave=None):
if eslug in entrances_xslug:
entrance = entrances_xslug[eslug]
else:
- # entrance = Entrance.objects.get(entranceslug__slug=eslug)
entrance = Entrance.objects.get(slug=eslug)
entrances_xslug[eslug] = entrance
CaveAndEntrance.objects.update_or_create(
cave=c, entrance_letter=letter, entrance=entrance
)
+ print(f"-- {entrance=}")
except:
message = f' ! Entrance setting failure, slug:"{slug}" #entrances:{len(entrances)} {entrance} letter:"{letter}" cave:"{c}" filename:"cave_data/{filename}"'
DataIssue.objects.create(parser="entrances", message=message, url=f"{c.url}_edit/")
print(message)
+ def reload_entrances():
+ """For individual re-reading of a cave_data file when editing,
+ also re-read the entrance_data files
+ """
+ for eslug in entrances_xslug:
+ entrance = entrances_xslug[eslug]
+ readentrance(entrance.filename, ent=entrance)
+ entrance.save()
global entrances_xslug
global caves_xslug
@@ -508,11 +552,7 @@ def readcave(filename, cave=None):
if cave:
# this a re-load prior to editing and we already know the cave id
- cave.non_public={
- "True": True,
- "False": False,
- "true": True,
- "false": False}[non_public[0]]
+ cave.non_public=boolify(non_public)
cave.official_name=official_name[0]
cave.kataster_code=kataster_code[0]
cave.kataster_number=kataster_number[0]
@@ -550,16 +590,13 @@ def readcave(filename, cave=None):
c = cave
do_entrances()
+ print(f"- {entrances_xslug=}")
+ reload_entrances()
cave.save()
else:
try:
c, state = Cave.objects.update_or_create(
- non_public={
- "True": True,
- "False": False,
- "true": True,
- "false": False,
- }[non_public[0]],
+ non_public=boolify(non_public),
official_name=official_name[0],
kataster_code=kataster_code[0],
kataster_number=kataster_number[0],