Fixed errors when creating new caves and entrances via the website. Moved slug logic to forms (previously checked in). This allows slug uniqueness to be validated in the form

This commit is contained in:
Martin Green 2023-04-30 19:05:57 +01:00
parent 941100a8a3
commit 10ff8a5aab
3 changed files with 36 additions and 37 deletions

View File

@ -352,23 +352,12 @@ def edit_cave(request, path="", slug=None):
if form.is_valid() and ceFormSet.is_valid():
# print(f'! POST is valid. {cave}')
cave = form.save(commit=False)
if slug is None:
for a in form.cleaned_data["area"]:
if a.kat_area():
myArea = a.kat_area()
if form.cleaned_data["kataster_number"]:
myslug = f"{myArea}-{form.cleaned_data['kataster_number']}"
else:
myslug = f"{myArea}-{form.cleaned_data['unofficial_number']}"
else:
myslug = slug
# Converting a PENDING cave to a real cave by saving this form
myslug = myslug.replace("-PENDING-", "-")
cave.filename = myslug + ".html"
cave.filename = form.cleaned_data["cave_slug"] + ".html"
cave.save()
form.save_m2m()
if slug is None:
cs = CaveSlug(cave=cave, slug=myslug, primary=True)
cs = CaveSlug(cave=cave, slug=form.cleaned_data["cave_slug"], primary=True)
cs.save()
ceinsts = ceFormSet.save(commit=False)
for ceinst in ceinsts:
@ -386,16 +375,18 @@ def edit_cave(request, path="", slug=None):
return render(request, "errors/generic.html", {"message": message})
return HttpResponseRedirect("/" + cave.url)
else:
message = f"! POST data is INVALID {cave}"
print(message)
else:
# re-read cave data from file.
filename = str(cave.slug() +".html")
read_cave(filename, cave=cave)
if slug is not None:
# re-read cave data from file.
filename = str(cave.slug() +".html")
read_cave(filename, cave=cave)
form = CaveForm(instance=cave)
ceFormSet = CaveAndEntranceFormSet(queryset=cave.caveandentrance_set.all())
form = CaveForm(instance=cave, initial={'cave_slug': cave.slug()})
ceFormSet = CaveAndEntranceFormSet(queryset=cave.caveandentrance_set.all())
else:
form = CaveForm()
ceFormSet = CaveAndEntranceFormSet(queryset=CaveAndEntrance.objects.none())
return render(
request,
@ -426,18 +417,20 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
except:
return render(request, "errors/badslug.html", {"badslug": f"for cave {caveslug} - from edit_entrance()"})
try:
entrance = Entrance.objects.get(slug=entslug)
except:
return render(request, "errors/badslug.html", {"badslug": f"for entrance {slug} - from edit_entrance()"})
if entslug:
try:
entrance = Entrance.objects.get(slug=entslug)
except:
return render(request, "errors/badslug.html", {"badslug": f"for entrance {entslug} - from edit_entrance()"})
else:
entrance = None
if entslug:
# print(f"{caveslug=} {entslug=} {path=}")
caveAndEntrance = CaveAndEntrance.objects.get(entrance=entrance, cave=cave)
entlettereditable = False
else:
entrance = Entrance()
caveAndEntrance = CaveAndEntrance(cave=cave, entrance=entrance)
caveAndEntrance = CaveAndEntrance(cave=cave, entrance=Entrance())
entlettereditable = True
if request.POST:
@ -461,19 +454,24 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
cave_file = cave.file_output()
entrance.save()
write_and_commit([entrance_file, cave_file], f"Online edit of entrance {entrance.slug}")
entrance_letter.entrance = entrance
if entslug is None:
entrance_letter.save()
return HttpResponseRedirect("/" + cave.url)
else:
# re-read entrance data from file.
filename = str(entrance.slug +".html")
read_entrance(filename, ent=entrance)
if entrance:
# re-read entrance data from file.
filename = str(entrance.slug +".html")
read_entrance(filename, ent=entrance)
form = EntranceForm(instance=entrance)
if entslug is None:
entletter = EntranceLetterForm()
form = EntranceForm(instance=entrance)
if entslug is None:
entletter = EntranceLetterForm()
else:
entletter = caveAndEntrance.entrance_letter
else:
entletter = caveAndEntrance.entrance_letter
form = EntranceForm()
entletter = EntranceLetterForm()
return render(
request,

View File

@ -8,7 +8,8 @@
{% include 'html_editor_pop_ups.html' %}
<h2>{{message}}</h2>
<form action="" method="post">{% csrf_token %}
<table>{{ form }}<!-- begin caveAndEntranceFormSet-->{{caveAndEntranceFormSet}}<!--end caveAndEntranceFormSet--></table>
<table>{{ form }}
<!-- begin caveAndEntranceFormSet-->{{caveAndEntranceFormSet}}<!--end caveAndEntranceFormSet--></table>
<p><input type="submit" value="Submit" /></p>
</form>

View File

@ -1,4 +1,4 @@
<form id="new_image_form" action="{% url 'new_image_form' path %}" method="post" enctype="multipart/form-data">
<form id="new_image_form" action="{% url 'new_image_form' path %}" method="post" enctype="multipart/form-data" class="dropzone">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">