2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-04-03 09:21:48 +01:00

Fix entrance edit too, saving slug now

This commit is contained in:
Philip Sargent 2023-04-22 03:26:53 +01:00
parent 2ed66fe3d0
commit 94b8b357fb
5 changed files with 123 additions and 71 deletions

View File

@ -376,8 +376,7 @@ def edit_cave(request, path="", slug=None):
ceinst.save() ceinst.save()
try: try:
cave_file = cave.file_output() cave_file = cave.file_output()
# print(cave_file) write_and_commit([cave_file], f"Online edit of cave {cave}")
write_and_commit([cave_file], f"Online edit of {cave}")
# leave other exceptions unhandled so that they bubble up to user interface # leave other exceptions unhandled so that they bubble up to user interface
except PermissionError: except PermissionError:
message = f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {cave.filename}. Ask a nerd to fix this." 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 @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 """This is the form that edits the entrance data for a single entrance and writes out
an XML file in the :expoweb: repo folder an XML file in the :expoweb: repo folder
@ -425,9 +424,15 @@ def edit_entrance(request, path="", caveslug=None, slug=None):
try: try:
cave = Cave.objects.get(caveslug__slug=caveslug) cave = Cave.objects.get(caveslug__slug=caveslug)
except: 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) caveAndEntrance = CaveAndEntrance.objects.get(entrance=entrance, cave=cave)
entlettereditable = False entlettereditable = False
else: else:
@ -441,24 +446,31 @@ def edit_entrance(request, path="", caveslug=None, slug=None):
if form.is_valid() and entletter.is_valid(): if form.is_valid() and entletter.is_valid():
entrance = form.save(commit=False) entrance = form.save(commit=False)
entrance_letter = entletter.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"]: if entletter.cleaned_data["entrance_letter"]:
slugname = cave.slug() + entletter.cleaned_data["entrance_letter"] slugname = cave.slug() + entletter.cleaned_data["entrance_letter"]
else: else:
slugname = cave.slug() slugname = cave.slug()
entrance.slug = slugname
entrance.cached_primary_slug = slugname entrance.cached_primary_slug = slugname
entrance.filename = slugname + ".html" entrance.filename = slugname + ".html"
entrance.save() entrance.save()
entrance_file = entrance.file_output() entrance_file = entrance.file_output()
print(f"Online edit of entrance {entrance.slug}")
cave_file = cave.file_output() cave_file = cave.file_output()
write_and_commit([entrance_file, cave_file], f"Online edit of {cave}{entletter}")
entrance.save() 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() entrance_letter.save()
return HttpResponseRedirect("/" + cave.url) return HttpResponseRedirect("/" + cave.url)
else: else:
# re-read entrance data from file.
filename = str(entrance.slug +".html")
readentrance(filename, ent=entrance)
form = EntranceForm(instance=entrance) form = EntranceForm(instance=entrance)
if slug is None: if entslug is None:
entletter = EntranceLetterForm() entletter = EntranceLetterForm()
else: else:
entletter = caveAndEntrance.entrance_letter entletter = caveAndEntrance.entrance_letter

View File

@ -307,6 +307,14 @@ def getXML(text, itemname, minItems=1, maxItems=None, context=""):
items = [""] items = [""]
return items return items
def boolify(boolstrs):
return {
"True": True,
"False": False,
"true": True,
"false": False}[boolstrs[0]]
def readentrance(filename, ent=None): def readentrance(filename, ent=None):
"""Reads an entrance description from the .html file """Reads an entrance description from the .html file
@ -315,7 +323,6 @@ def readentrance(filename, ent=None):
""" """
def getXMLmax1(field): def getXMLmax1(field):
return getXML(entrancecontents, field, maxItems=1, context=context) return getXML(entrancecontents, field, maxItems=1, context=context)
# return getXML(entrancecontents, field, maxItems=1, context=context)[0]
global entrances_xslug global entrances_xslug
global caves_xslug global caves_xslug
@ -325,55 +332,81 @@ def readentrance(filename, ent=None):
with open(os.path.join(ENTRANCEDESCRIPTIONS, filename)) as f: with open(os.path.join(ENTRANCEDESCRIPTIONS, filename)) as f:
contents = f.read() contents = f.read()
context = filename context = filename
# print("Reading file ENTRANCE {} / {}".format(ENTRANCEDESCRIPTIONS, filename)) # print("Reading file ENTRANCE {} / {}".format(ENTRANCEDESCRIPTIONS, filename))
entrancecontentslist = getXML(contents, "entrance", maxItems=1, context=context) entrancecontentslist = getXML(contents, "entrance", maxItems=1, context=context)
if len(entrancecontentslist) != 1: if len(entrancecontentslist) != 1:
message = f'! BAD ENTRANCE at "{filename}". Loading aborted. ' message = f'! BAD ENTRANCE at "{filename}". Loading aborted. '
DataIssue.objects.create(parser="entrances", message=message) DataIssue.objects.create(parser="entrances", message=message)
print(message) print(message)
else: return
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")
if len(slugs) >1: entrancecontents = entrancecontentslist[0]
# Only ever one of these per entrance in the expo dataset slugs = getXML(entrancecontents, "slug", context=context)
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/") if len(slugs) >1:
print(message) # Only ever one of these per entrance in the expo dataset
return 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( e, state = Entrance.objects.update_or_create(
name=name[0], name=name[0],
non_public={ non_public=boolify(non_public),
"True": True,
"False": False,
"true": True,
"false": False,
}[non_public[0]],
alt=alt[0], alt=alt[0],
approach=approach[0], approach=approach[0],
bearings=bearings[0], bearings=bearings[0],
@ -398,7 +431,7 @@ def readentrance(filename, ent=None):
underground_description=underground_description[0], underground_description=underground_description[0],
url=url[0], url=url[0],
) )
e.save()
def readcave(filename, cave=None): def readcave(filename, cave=None):
"""Reads an entrance description from the .html file """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. but this is OK, a search will find them in the db.
""" """
def do_entrances(): def do_entrances():
for entrance in entrances: """For both bulk import and individual re-reading of cave_data file,
eslug = getXML(entrance, "entranceslug", maxItems=1, context=context)[0] fix the entrances
letter = getXML(entrance, "letter", maxItems=1, context=context)[0] """
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> 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") set_dummy_entrance(slug[5:], slug, c, msg="DUMMY: no entrance slug read from file")
else: else:
@ -419,16 +455,24 @@ def readcave(filename, cave=None):
if eslug in entrances_xslug: if eslug in entrances_xslug:
entrance = entrances_xslug[eslug] entrance = entrances_xslug[eslug]
else: else:
# entrance = Entrance.objects.get(entranceslug__slug=eslug)
entrance = Entrance.objects.get(slug=eslug) entrance = Entrance.objects.get(slug=eslug)
entrances_xslug[eslug] = entrance entrances_xslug[eslug] = entrance
CaveAndEntrance.objects.update_or_create( CaveAndEntrance.objects.update_or_create(
cave=c, entrance_letter=letter, entrance=entrance cave=c, entrance_letter=letter, entrance=entrance
) )
print(f"-- {entrance=}")
except: except:
message = f' ! Entrance setting failure, slug:"{slug}" #entrances:{len(entrances)} {entrance} letter:"{letter}" cave:"{c}" filename:"cave_data/{filename}"' 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/") DataIssue.objects.create(parser="entrances", message=message, url=f"{c.url}_edit/")
print(message) 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 entrances_xslug
global caves_xslug global caves_xslug
@ -508,11 +552,7 @@ def readcave(filename, cave=None):
if cave: if cave:
# this a re-load prior to editing and we already know the cave id # this a re-load prior to editing and we already know the cave id
cave.non_public={ cave.non_public=boolify(non_public)
"True": True,
"False": False,
"true": True,
"false": False}[non_public[0]]
cave.official_name=official_name[0] cave.official_name=official_name[0]
cave.kataster_code=kataster_code[0] cave.kataster_code=kataster_code[0]
cave.kataster_number=kataster_number[0] cave.kataster_number=kataster_number[0]
@ -550,16 +590,13 @@ def readcave(filename, cave=None):
c = cave c = cave
do_entrances() do_entrances()
print(f"- {entrances_xslug=}")
reload_entrances()
cave.save() cave.save()
else: else:
try: try:
c, state = Cave.objects.update_or_create( c, state = Cave.objects.update_or_create(
non_public={ non_public=boolify(non_public),
"True": True,
"False": False,
"true": True,
"false": False,
}[non_public[0]],
official_name=official_name[0], official_name=official_name[0],
kataster_code=kataster_code[0], kataster_code=kataster_code[0],
kataster_number=kataster_number[0], kataster_number=kataster_number[0],

View File

@ -10,7 +10,9 @@ though, you do not need to do a data import as it happens automatically -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head> </head>
<body> <body>
<b>This file is generated by troggle</b> on {{date}} UTC using the form documented at /handbook/survey/caveentry.html <b>This file is generated by troggle</b> on {{date}} UTC using the form documented at
the form documented at
<a ="/handbook/survey/caveentry.html">handbook/survey/caveentry.html</a>
<br> <br>
<cave> <cave>

View File

@ -14,11 +14,12 @@ though, you do not need to do a data import as it happens automatically -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head> </head>
<body> <body>
<b>This file is generated by troggle</b> on {{date}} UTC using the form documented at handbook/survey/ententry.html <b>This file is generated by troggle</b> on {{date}} UTC using the form documented at
<a ="/handbook/survey/ententry.html">handbook/survey/ententry.html</a>
<br> <br>
<entrance> <entrance>
<non_public>{{ entrance.non_public }}</non_public>{% for slug in entrance.entranceslug_set.all %} <non_public>{{ entrance.non_public }}</non_public>
<slug>{{ slug.slug|default_if_none:""|safe }}</slug>{% endfor %} <slug>{{ entrance.slug|safe }}</slug> <!-- one entrance and one slug per entrance_data file -->
<name>{{ entrance.name|default_if_none:""|safe }}</name> <name>{{ entrance.name|default_if_none:""|safe }}</name>
<entrance_description>{{ entrance.entrance_description|default_if_none:""|safe }}</entrance_description> <entrance_description>{{ entrance.entrance_description|default_if_none:""|safe }}</entrance_description>
<explorers>{{ entrance.explorers|default_if_none:""|safe }}</explorers> <explorers>{{ entrance.explorers|default_if_none:""|safe }}</explorers>

View File

@ -143,7 +143,7 @@ trogglepatterns = [
# Edit caves and entrances # Edit caves and entrances
re_path(r'^(?P<path>.*)/(?P<slug>[^/]+)_cave_edit/$', edit_cave, name="edit_cave"), # edit_cave needed by cave.html template for url matching re_path(r'^(?P<path>.*)/(?P<slug>[^/]+)_cave_edit/$', edit_cave, name="edit_cave"), # edit_cave needed by cave.html template for url matching
re_path(r'^(?P<path>.*)/(?P<caveslug>[^/]+):(?P<slug>[^:]+)_entrance_edit', edit_entrance, name = "editentrance"), #edit existing entrance re_path(r'^(?P<path>.*)/(?P<caveslug>[^/]+):(?P<entslug>[^:]+)_entrance_edit', edit_entrance, name = "editentrance"), #edit existing entrance
re_path(r'^(?P<path>.*)/(?P<caveslug>[^/]+)_entrance_new$', edit_entrance, name = "newentrance"), # new entrance for a cave re_path(r'^(?P<path>.*)/(?P<caveslug>[^/]+)_entrance_new$', edit_entrance, name = "newentrance"), # new entrance for a cave
re_path(r'^(.*)_edit$', editexpopage, name="editexpopage"), re_path(r'^(.*)_edit$', editexpopage, name="editexpopage"),