2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-17 23:27:09 +00:00

fix error on first creating new ent

This commit is contained in:
2023-11-08 02:12:37 +02:00
parent b5cc66a576
commit 2215464cfa
4 changed files with 41 additions and 18 deletions

View File

@@ -561,46 +561,52 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
print(f"- POST {caveslug=} {entslug=} {entranceletter=} {path=}")
if entslug is None:
# we are creating a new entrance
entrance = entform.save(commit=False)
# entrance = ce.entrance # the one we created earlier?
if entranceletter:
slugname, letter = check_new_slugname_ok(cave.slug(), entranceletter)
else:
slugname, letter = check_new_slugname_ok(cave.slug(), "")
ce.entranceletter = letter
entrance = ce.entrance # the one we created earlier
entrance.slug = slugname
entrance.cached_primary_slug = slugname
entrance.filename = slugname + ".html"
else:
# an existing entrance ?
entrance.slug = entslug
entrance.cached_primary_slug = entslug
entrance.filename = entslug + ".html"
try:
entrance.save()
except:
print(f"- post {entrance.slug=} {entrance.tag_station=} {entrance.other_station=}")
except Exception as e:
# fails with uniqueness constraint failure. Which is on CaveAndEntrance, not just on entrance,
# which is confusing to a user who is just editing an Entrance.
# Can happen when user specifies an existing letter! (or none, when they should set one)
print(f"SAVE EXCEPTION FAIL {entrance=}")
print(f"CAVE {cave}")
print(f"CAVE {cave}\n{e}")
for ce in cave.entrances():
print(f"CAVE:{ce.cave} - ENT:{ce.entrance} - LETTER:'{ce.entranceletter}'")
raise
ce.entrance = entrance
# try not to do this:
# try not to invoke this:
# UNIQUE constraint failed: core_caveandentrance.cave_id, core_caveandentrance.entranceletter
ce.save()
entrance_file = entrance.file_output()
cave_file = cave.file_output()
print(f"- POST WRITE letter: '{ce}' {entrance=}")
if write_and_commit([entrance_file, cave_file], f"Online edit of entrance {entrance.slug}"):
try:
write_and_commit([entrance_file, cave_file], f"Online edit of entrance {entrance.slug}")
return HttpResponseRedirect("/" + cave.url)
else:
except Exception as e:
efilepath, econtent, eencoding = entrance_file
cfilepath, ccontent, cencoding = cave_file
message = f"- FAIL write_and_commit \n entr:'{efilepath}'\n cave:'{cfilepath}'"
message = f"- FAIL write_and_commit \n entr:'{efilepath}'\n cave:'{cfilepath}'\n\n{e}"
print(message)
return render(request, "errors/generic.html", {"message": message})
@@ -611,8 +617,12 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
if entrance:
# re-read entrance data from file.
filename = str(entrance.slug +".html")
ent = read_entrance(filename, ent=entrance)
print(f"ENTRANCE from file: entranceletter = '{ce.entranceletter}'")
try:
ent = read_entrance(filename, ent=entrance)
print(f"ENTRANCE from file: entranceletter = '{ce.entranceletter}'")
except:
# ent only in db not on file. Interesting, let's run with it using whatever we have in the db
print(f"ENTRANCE NOT read from file: entranceletter = '{ce.entranceletter}'")
entform = EntranceForm(instance=entrance)
if entslug is None: