From 8978f21cdcc1489ca060c8a7e713b29c0e7bb3ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Sat, 13 Aug 2022 20:39:29 +0200
Subject: [PATCH] Improve history handling

Only store AK in database once all attributes are correctly set.
Previously, each AK was automatically edited directly after saving to add the link, which created two history entries for that AK.
This resolves #141.
Additionally, only display slots as (re-)scheduled when they are indeed scheduled (i.e., have a starting time stamp).
---
 AKDashboard/views.py  | 2 +-
 AKSubmission/views.py | 7 +++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/AKDashboard/views.py b/AKDashboard/views.py
index 9d9518d8..afabaf7d 100644
--- a/AKDashboard/views.py
+++ b/AKDashboard/views.py
@@ -54,7 +54,7 @@ class DashboardEventView(DetailView):
             # Changes in plan
             if apps.is_installed("AKPlan"):
                 if not context['event'].plan_hidden:
-                    last_changed_slots = AKSlot.objects.filter(event=context['event']).order_by('-updated')[
+                    last_changed_slots = AKSlot.objects.filter(event=context['event'], start__isnull=False).order_by('-updated')[
                                          :int(settings.DASHBOARD_RECENT_MAX)]
                     for changed_slot in last_changed_slots:
                         recent_changes.append({'icon': ('clock', 'far'),
diff --git a/AKSubmission/views.py b/AKSubmission/views.py
index 18972682..46c6fa55 100644
--- a/AKSubmission/views.py
+++ b/AKSubmission/views.py
@@ -215,7 +215,8 @@ class AKAndAKWishSubmissionView(EventSlugMixin, EventInactiveRedirectMixin, Crea
             return redirect(reverse_lazy('submit:submission_overview',
                                          kwargs={'event_slug': form.cleaned_data["event"].slug}))
 
-        super_form_valid = super().form_valid(form)
+        # Generate object but don't store it in the database yet
+        self.object = form.save(commit=False)
 
         # Generate wiki link
         if form.cleaned_data["event"].base_url:
@@ -225,7 +226,9 @@ class AKAndAKWishSubmissionView(EventSlugMixin, EventInactiveRedirectMixin, Crea
             if len(link) > 200:
                 messages.add_message(self.request, messages.WARNING,
                     _("Due to technical reasons, the link you entered was truncated to a length of 200 characters"))
-        self.object.save()
+
+        # Try to save AK and get redirect URL
+        super_form_valid = super().form_valid(form)
 
         # Set tags (and generate them if necessary)
         for tag_name in form.cleaned_data["tag_names"]:
-- 
GitLab