From 10ff8a5aab1e4380989c589044e12c34ecbf70ce Mon Sep 17 00:00:00 2001 From: Martin Green Date: Sun, 30 Apr 2023 19:05:57 +0100 Subject: [PATCH] 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 --- core/views/caves.py | 68 +++++++++++++++++------------------ templates/editcave.html | 3 +- templates/new_image_form.html | 2 +- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/core/views/caves.py b/core/views/caves.py index e51fb47..f83ed1c 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -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, diff --git a/templates/editcave.html b/templates/editcave.html index 640412a..45f752b 100644 --- a/templates/editcave.html +++ b/templates/editcave.html @@ -8,7 +8,8 @@ {% include 'html_editor_pop_ups.html' %}

{{message}}

{% csrf_token %} - {{ form }}{{caveAndEntranceFormSet}}
+ {{ form }} + {{caveAndEntranceFormSet}}

diff --git a/templates/new_image_form.html b/templates/new_image_form.html index 1a2f636..c2d58fb 100644 --- a/templates/new_image_form.html +++ b/templates/new_image_form.html @@ -1,4 +1,4 @@ -
+ {% csrf_token %} {{ form.as_p }}