2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-22 07:11:52 +00: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()
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

View File

@ -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: <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],

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"/>
</head>
<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>
<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"/>
</head>
<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>
<entrance>
<non_public>{{ entrance.non_public }}</non_public>{% for slug in entrance.entranceslug_set.all %}
<slug>{{ slug.slug|default_if_none:""|safe }}</slug>{% endfor %}
<non_public>{{ entrance.non_public }}</non_public>
<slug>{{ entrance.slug|safe }}</slug> <!-- one entrance and one slug per entrance_data file -->
<name>{{ entrance.name|default_if_none:""|safe }}</name>
<entrance_description>{{ entrance.entrance_description|default_if_none:""|safe }}</entrance_description>
<explorers>{{ entrance.explorers|default_if_none:""|safe }}</explorers>

View File

@ -143,7 +143,7 @@ trogglepatterns = [
# 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<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'^(.*)_edit$', editexpopage, name="editexpopage"),