From 94b8b357fb646a0d777d092655c07fb8f467e2cd Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sat, 22 Apr 2023 03:26:53 +0100 Subject: [PATCH] Fix entrance edit too, saving slug now --- core/views/caves.py | 30 ++++-- parsers/caves.py | 151 +++++++++++++++++++----------- templates/dataformat/cave.xml | 4 +- templates/dataformat/entrance.xml | 7 +- urls.py | 2 +- 5 files changed, 123 insertions(+), 71 deletions(-) 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: 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], diff --git a/templates/dataformat/cave.xml b/templates/dataformat/cave.xml index 42fc122..f7e0c6b 100644 --- a/templates/dataformat/cave.xml +++ b/templates/dataformat/cave.xml @@ -10,7 +10,9 @@ though, you do not need to do a data import as it happens automatically --> -This file is generated by troggle on {{date}} UTC using the form documented at /handbook/survey/caveentry.html +This file is generated by troggle on {{date}} UTC using the form documented at +the form documented at +handbook/survey/caveentry.html
diff --git a/templates/dataformat/entrance.xml b/templates/dataformat/entrance.xml index 92d8757..3f0bc3e 100644 --- a/templates/dataformat/entrance.xml +++ b/templates/dataformat/entrance.xml @@ -14,11 +14,12 @@ though, you do not need to do a data import as it happens automatically --> -This file is generated by troggle on {{date}} UTC using the form documented at handbook/survey/ententry.html +This file is generated by troggle on {{date}} UTC using the form documented at +handbook/survey/ententry.html
-{{ entrance.non_public }}{% for slug in entrance.entranceslug_set.all %} -{{ slug.slug|default_if_none:""|safe }}{% endfor %} +{{ entrance.non_public }} +{{ entrance.slug|safe }} {{ entrance.name|default_if_none:""|safe }} {{ entrance.entrance_description|default_if_none:""|safe }} {{ entrance.explorers|default_if_none:""|safe }} diff --git a/urls.py b/urls.py index c06b94a..88c3578 100644 --- a/urls.py +++ b/urls.py @@ -143,7 +143,7 @@ trogglepatterns = [ # Edit caves and entrances re_path(r'^(?P.*)/(?P[^/]+)_cave_edit/$', edit_cave, name="edit_cave"), # edit_cave needed by cave.html template for url matching - re_path(r'^(?P.*)/(?P[^/]+):(?P[^:]+)_entrance_edit', edit_entrance, name = "editentrance"), #edit existing entrance + re_path(r'^(?P.*)/(?P[^/]+):(?P[^:]+)_entrance_edit', edit_entrance, name = "editentrance"), #edit existing entrance re_path(r'^(?P.*)/(?P[^/]+)_entrance_new$', edit_entrance, name = "newentrance"), # new entrance for a cave re_path(r'^(.*)_edit$', editexpopage, name="editexpopage"),