From 566954b6bca80be219675ae79e8dd3918e630689 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Sun, 14 Aug 2022 01:23:50 +0200
Subject: [PATCH] Move link creation from view to form

This makes the usage of save(commit=False) obsolete that could cause issues for the creation of Availabilities (due to atomicy of transactions).
---
 AKSubmission/forms.py | 10 ++++++++++
 AKSubmission/views.py | 12 ------------
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/AKSubmission/forms.py b/AKSubmission/forms.py
index 41cebfca..e971ed57 100644
--- a/AKSubmission/forms.py
+++ b/AKSubmission/forms.py
@@ -2,6 +2,7 @@ import itertools
 import re
 
 from django import forms
+from django.contrib import messages
 from django.core.exceptions import ValidationError
 from django.utils.translation import ugettext_lazy as _
 
@@ -100,6 +101,15 @@ class AKForm(AvailabilitiesFormMixin, forms.ModelForm):
                 short_name = '{}-{}'.format(short_name[:-(digits + 1)], i)
             cleaned_data["short_name"] = short_name
 
+        # Generate wiki link
+        if self.cleaned_data["event"].base_url:
+            link = self.cleaned_data["event"].base_url + self.cleaned_data["name"].replace(" ", "_")
+            # Truncate links longer than 200 characters (default length of URL fields in django)
+            self.cleaned_data["link"] = link[:200]
+            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"))
+
         # Get tag names from raw tags
         cleaned_data["tag_names"] = [name.strip().lower() for name
                                      in self.split_string.split(cleaned_data["tags_raw"])
diff --git a/AKSubmission/views.py b/AKSubmission/views.py
index 46c6fa55..a59f0ee1 100644
--- a/AKSubmission/views.py
+++ b/AKSubmission/views.py
@@ -215,18 +215,6 @@ class AKAndAKWishSubmissionView(EventSlugMixin, EventInactiveRedirectMixin, Crea
             return redirect(reverse_lazy('submit:submission_overview',
                                          kwargs={'event_slug': form.cleaned_data["event"].slug}))
 
-        # 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:
-            link = form.cleaned_data["event"].base_url + form.cleaned_data["name"].replace(" ", "_")
-            # Truncate links longer than 200 characters (default length of URL fields in django)
-            self.object.link = link[:200]
-            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"))
-
         # Try to save AK and get redirect URL
         super_form_valid = super().form_valid(form)
 
-- 
GitLab