From 1133ded42c0dde439b616179ea3e972cb461987b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Mon, 26 Sep 2022 22:10:16 +0200
Subject: [PATCH] Improve slot handling for wishes

This implements #144
Don't show slots, slot creation button, and availabilities for wishes on detail page
Don't create slots for wishes automatically (and don't ask for durations)
Automatically create one slot when wish changes to AK
Automatically remove all unscheduled slots when AK changes to wish
---
 AKSubmission/forms.py                         |   2 +-
 .../templates/AKSubmission/ak_detail.html     | 149 +++++++++---------
 AKSubmission/views.py                         |  22 ++-
 3 files changed, 94 insertions(+), 79 deletions(-)

diff --git a/AKSubmission/forms.py b/AKSubmission/forms.py
index e24e0a41..55959be4 100644
--- a/AKSubmission/forms.py
+++ b/AKSubmission/forms.py
@@ -151,7 +151,7 @@ class AKEditForm(AKForm):
         self.fields["tags_raw"].initial = "; ".join(str(tag) for tag in self.instance.tags.all())
 
 
-class AKWishForm(AKSubmissionForm):
+class AKWishForm(AKForm):
     class Meta(AKForm.Meta):
         exclude = ['owners', 'link', 'protocol_link']
 
diff --git a/AKSubmission/templates/AKSubmission/ak_detail.html b/AKSubmission/templates/AKSubmission/ak_detail.html
index 69324b63..e5052faf 100644
--- a/AKSubmission/templates/AKSubmission/ak_detail.html
+++ b/AKSubmission/templates/AKSubmission/ak_detail.html
@@ -235,95 +235,96 @@
 
     <p style="margin-top: 30px;margin-bottom: 30px;">{{ ak.description|linebreaks }}</p>
 
-
-    <table class="table">
-        <thead>
-        <tr>
-            {% if not ak.event.plan_hidden or user.is_staff %}
-                <th>{% trans "When?" %}</th>
-            {% endif %}
-            <th>{% trans "Duration" %}</th>
-            {% if not ak.event.plan_hidden or user.is_staff %}
-                <th>{% trans "Room" %}</th>
-            {% endif %}
-            <th></th>
-        </tr>
-        </thead>
-        <tbody>
-        {% for slot in ak.akslot_set.all %}
+    {% if not ak.wish %}
+        <table class="table">
+            <thead>
             <tr>
                 {% if not ak.event.plan_hidden or user.is_staff %}
-                    <td>{{ slot.time_simplified }}</td>
+                    <th>{% trans "When?" %}</th>
                 {% endif %}
-                <td>{{ slot.duration_simplified }}</td>
+                <th>{% trans "Duration" %}</th>
                 {% if not ak.event.plan_hidden or user.is_staff %}
-                    <td>
-                        {% if slot.room %}
-                            {% if "AKPlan"|check_app_installed %}
-                                <a href="{% url 'plan:plan_room' event_slug=ak.event.slug pk=slot.room.pk %}">{{ slot.room }}</a>
+                    <th>{% trans "Room" %}</th>
+                {% endif %}
+                <th></th>
+            </tr>
+            </thead>
+            <tbody>
+            {% for slot in ak.akslot_set.all %}
+                <tr>
+                    {% if not ak.event.plan_hidden or user.is_staff %}
+                        <td>{{ slot.time_simplified }}</td>
+                    {% endif %}
+                    <td>{{ slot.duration_simplified }}</td>
+                    {% if not ak.event.plan_hidden or user.is_staff %}
+                        <td>
+                            {% if slot.room %}
+                                {% if "AKPlan"|check_app_installed %}
+                                    <a href="{% url 'plan:plan_room' event_slug=ak.event.slug pk=slot.room.pk %}">{{ slot.room }}</a>
+                                {% else %}
+                                    {{ slot.room }}
+                                {% endif %}
                             {% else %}
-                                {{ slot.room }}
+                                -
                             {% endif %}
+                        </td>
+                    {% endif %}
+                    <td>
+                        {% if not slot.start %}
+                            <a href="{% url 'submit:akslot_edit' event_slug=ak.event.slug pk=slot.pk %}"
+                               data-toggle="tooltip" title="{% trans 'Edit' %}"
+                               class="btn btn-success">{% fa5_icon 'pencil-alt' 'fas' %}</a>
+                            <a href="{% url 'submit:akslot_delete' event_slug=ak.event.slug pk=slot.pk %}"
+                               data-toggle="tooltip" title="{% trans 'Delete' %}"
+                               class="btn btn-danger">{% fa5_icon 'times' 'fas' %}</a>
                         {% else %}
-                            -
+                            {% if "AKOnline"|check_app_installed and slot.room and slot.room.virtualroom and slot.room.virtualroom.url != '' %}
+                                <a class="btn btn-success" href="{{ slot.room.virtualroom.url }}">
+                                    {% fa5_icon 'external-link-alt' 'fas' %} {% trans "Go to virtual room" %}
+                                </a>
+                            {% endif %}
                         {% endif %}
-                    </td>
-                {% endif %}
-                <td>
-                    {% if not slot.start %}
-                        <a href="{% url 'submit:akslot_edit' event_slug=ak.event.slug pk=slot.pk %}"
-                           data-toggle="tooltip" title="{% trans 'Edit' %}"
-                           class="btn btn-success">{% fa5_icon 'pencil-alt' 'fas' %}</a>
-                        <a href="{% url 'submit:akslot_delete' event_slug=ak.event.slug pk=slot.pk %}"
-                           data-toggle="tooltip" title="{% trans 'Delete' %}"
-                           class="btn btn-danger">{% fa5_icon 'times' 'fas' %}</a>
-                    {% else %}
-                        {% if "AKOnline"|check_app_installed and slot.room and slot.room.virtualroom and slot.room.virtualroom.url != '' %}
-                            <a class="btn btn-success" href="{{ slot.room.virtualroom.url }}">
-                                {% fa5_icon 'external-link-alt' 'fas' %} {% trans "Go to virtual room" %}
-                            </a>
+                        {% if user.is_staff %}
+                            <a href="{% url 'admin:AKModel_akslot_change' slot.pk %}"
+                               data-toggle="tooltip" title="{% trans 'Schedule' %}"
+                               class="btn btn-outline-success">{% fa5_icon 'stream' 'fas' %}</a>
                         {% endif %}
-                    {% endif %}
-                    {% if user.is_staff %}
-                        <a href="{% url 'admin:AKModel_akslot_change' slot.pk %}"
-                           data-toggle="tooltip" title="{% trans 'Schedule' %}"
-                           class="btn btn-outline-success">{% fa5_icon 'stream' 'fas' %}</a>
-                    {% endif %}
-                </td>
-            </tr>
-        {% endfor %}
-        </tbody>
-    </table>
+                    </td>
+                </tr>
+            {% endfor %}
+            </tbody>
+        </table>
 
-    {% if ak.event.active %}
-        <div class="">
-            <a href="{% url 'submit:akslot_add' event_slug=ak.event.slug pk=ak.pk %}"
-               class="btn btn-success">{% fa5_icon 'plus' 'fas' %} {% trans "Add another slot" %}</a>
-        </div>
-    {% endif %}
+        {% if ak.event.active %}
+            <div class="">
+                <a href="{% url 'submit:akslot_add' event_slug=ak.event.slug pk=ak.pk %}"
+                   class="btn btn-success">{% fa5_icon 'plus' 'fas' %} {% trans "Add another slot" %}</a>
+            </div>
+        {% endif %}
 
 
-    {% if 'AKPlan'|check_app_installed %}
-        <div id='akSlotCalendar' style="margin-top: 50px;margin-bottom: 50px;"></div>
-    {% endif %}
+        {% if 'AKPlan'|check_app_installed %}
+            <div id='akSlotCalendar' style="margin-top: 50px;margin-bottom: 50px;"></div>
+        {% endif %}
 
 
-    <h4 style="margin-top: 30px;">{% trans "Possible Times" %}</h4>
-    <table class="table">
-        <thead>
-        <tr>
-            <th>{% trans "Start" %}</th>
-            <th>{% trans "End" %}</th>
-        </tr>
-        </thead>
-        <tbody>
-        {% for a in availabilities %}
+        <h4 style="margin-top: 30px;">{% trans "Possible Times" %}</h4>
+        <table class="table">
+            <thead>
             <tr>
-                <td>{{ a.start | timezone:event.timezone | date:"l H:i"  }}</td>
-                <td>{{ a.end | timezone:event.timezone | date:"l H:i" }}</td>
+                <th>{% trans "Start" %}</th>
+                <th>{% trans "End" %}</th>
             </tr>
-        {% endfor %}
-        </tbody>
-    </table>
+            </thead>
+            <tbody>
+            {% for a in availabilities %}
+                <tr>
+                    <td>{{ a.start | timezone:event.timezone | date:"l H:i"  }}</td>
+                    <td>{{ a.end | timezone:event.timezone | date:"l H:i" }}</td>
+                </tr>
+            {% endfor %}
+            </tbody>
+        </table>
+    {% endif %}
 
 {% endblock %}
diff --git a/AKSubmission/views.py b/AKSubmission/views.py
index 28e6b6d1..15d0811d 100644
--- a/AKSubmission/views.py
+++ b/AKSubmission/views.py
@@ -223,10 +223,11 @@ class AKAndAKWishSubmissionView(EventSlugMixin, EventInactiveRedirectMixin, Crea
             tag, was_created = AKTag.objects.get_or_create(name=tag_name)
             self.object.tags.add(tag)
 
-        # Generate slot(s)
-        for duration in form.cleaned_data["durations"]:
-            new_slot = AKSlot(ak=self.object, duration=duration, event=self.object.event)
-            new_slot.save()
+        # Generate slot(s) (but not for wishes)
+        if "durations" in form.cleaned_data:
+            for duration in form.cleaned_data["durations"]:
+                new_slot = AKSlot(ak=self.object, duration=duration, event=self.object.event)
+                new_slot.save()
 
         return super_form_valid
 
@@ -269,6 +270,8 @@ class AKEditView(EventSlugMixin, EventInactiveRedirectMixin, UpdateView):
             return redirect(reverse_lazy('submit:submission_overview',
                                          kwargs={'event_slug': form.cleaned_data["event"].slug}))
 
+        previous_owner_count = self.object.owners.count()
+
         super_form_valid = super().form_valid(form)
 
         # Detach existing tags
@@ -279,6 +282,17 @@ class AKEditView(EventSlugMixin, EventInactiveRedirectMixin, UpdateView):
             tag, was_created = AKTag.objects.get_or_create(name=tag_name)
             self.object.tags.add(tag)
 
+        # Did this AK change from wish to AK or vice versa?
+        new_owner_count = self.object.owners.count()
+        # Now AK:
+        if previous_owner_count == 0 and new_owner_count > 0 and self.object.akslot_set.count() == 0:
+            # Create one slot with default length
+            AKSlot.objects.create(ak=self.object, duration=self.object.event.default_slot, event=self.object.event)
+        # Now wish:
+        elif previous_owner_count > 0 and new_owner_count == 0:
+            # Delete all unscheduled slots
+            self.object.akslot_set.filter(start__isnull=True).delete()
+
         return super_form_valid
 
 
-- 
GitLab