From 76a955b1b86cfe35cabf64561c63de9b034c5a7f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Wed, 13 May 2020 00:40:06 +0200
Subject: [PATCH] Unify AK list and submission overview

Use tabbed-view for ak list, too
Reduce code duplication between submission overview and ak list
Cleanup filtering in view
Display message when (filtered) AK list is empty
Remove submission button from dashboard when event is inactive
Closes #70
---
 .../templates/AKDashboard/dashboard_row.html  |   2 +-
 .../templates/AKSubmission/ak_list.html       |  91 +++++++-------
 .../templates/AKSubmission/ak_overview.html   |  30 +++++
 .../{ak_list_table.html => ak_table.html}     |   4 +
 .../AKSubmission/submission_overview.html     |  25 +---
 AKSubmission/urls.py                          |   2 +-
 AKSubmission/views.py                         | 115 +++++++++++-------
 7 files changed, 149 insertions(+), 120 deletions(-)
 create mode 100644 AKSubmission/templates/AKSubmission/ak_overview.html
 rename AKSubmission/templates/AKSubmission/{ak_list_table.html => ak_table.html} (94%)

diff --git a/AKDashboard/templates/AKDashboard/dashboard_row.html b/AKDashboard/templates/AKDashboard/dashboard_row.html
index 36b32af5..4b3e40c5 100644
--- a/AKDashboard/templates/AKDashboard/dashboard_row.html
+++ b/AKDashboard/templates/AKDashboard/dashboard_row.html
@@ -40,7 +40,7 @@
                 </a>
             {% endif %}
         {% endif %}
-        {% if 'AKSubmission'|check_app_installed %}
+        {% if 'AKSubmission'|check_app_installed and event.active %}
             <a class="dashboard-box btn btn-primary"
                href="{% url 'submit:submission_overview' event_slug=event.slug %}">
                 <div class="col-sm-12 col-md-3 col-lg-2 dashboard-button">
diff --git a/AKSubmission/templates/AKSubmission/ak_list.html b/AKSubmission/templates/AKSubmission/ak_list.html
index 3e4098bc..ac774bd7 100644
--- a/AKSubmission/templates/AKSubmission/ak_list.html
+++ b/AKSubmission/templates/AKSubmission/ak_list.html
@@ -1,49 +1,46 @@
-{% extends 'AKSubmission/submission_base.html' %}
-
 {% load i18n %}
-{% load fontawesome_5 %}
-
-{% load tags_AKSubmission %}
-
-{% block title %}{% trans "AKs" %}: {{ event.name }} - {% trans "AKs" %}{% endblock %}
-
-{% block imports %}
-    <style>
-        /* Prevent wrapping of buttons in AK table */
-        .table td:nth-child(5) {
-            white-space: nowrap;
-        }
-    </style>
-{% endblock %}
-
-{% block breadcrumbs %}
-    {% include "AKSubmission/submission_breadcrumbs.html" %}
-    <li class="breadcrumb-item"><a
-            href="{% url 'submit:submission_overview' event_slug=event.slug %}">{% trans "AK Submission" %}</a></li>
-    <li class="breadcrumb-item active">{% trans "AK List" %}</li>
-{% endblock %}
-
-{% block content %}
-    <h1>{{ event.name }}: {% trans "AK List" %}</h1>
-
-    {% if categories.count > 0 %}
-        <b>{% trans 'Categories' %}:</b>
-        {% category_list categories event.slug %}
-    {% endif %}
-
-    <br><br>
-
-    {% if tracks.count > 0 %}
-        <b>{% trans 'Tracks' %}:</b>
-        {% track_list tracks event.slug %}
-    {% endif %}
-
-    <br><br>
-
-    {% if filter_condition_string != "" %}
-        <h2 class="text-secondary">{{ filter_condition_string }}</h2>
-    {% endif %}
-
-    {% include "AKSubmission/ak_list_table.html" %}
 
-{% endblock %}
+<div class="float-right">
+    <ul class="nav nav-pills">
+        <li class="nav-item">
+            <a class="nav-link" href="{% url 'submit:ak_list' event_slug=event.slug %}">{% trans "All AKs" %}</a>
+        </li>
+        {% if event.aktrack_set.count > 0 %}
+            <li class="nav-item dropdown">
+                <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true"
+                   aria-expanded="false">{% trans "Tracks" %}</a>
+                <div class="dropdown-menu" style="">
+                    {% for track in event.aktrack_set.all %}
+                        <a class="dropdown-item"
+                           href="{% url 'submit:ak_list_by_track' event_slug=event.slug track_pk=track.pk %}">
+                            {{ track }}</a>
+                    {% endfor %}
+                </div>
+            </li>
+        {% endif %}
+    </ul>
+</div>
+
+<h2>{{ table_title }}</h2>
+
+<noscript>
+    {% include "AKSubmission/ak_table.html" %}
+</noscript>
+
+<ul class="nav nav-tabs" style="margin-bottom:15px">
+    {% for category, _ in categories_with_aks %}
+        <li class="nav-item">
+            <a class="nav-link {% if category.name == active_category %}active{% endif %}" data-toggle="tab"
+               href="#category_{{ category.pk }}">{{ category.name }}</a>
+        </li>
+    {% endfor %}
+</ul>
+
+<div id="akListTabbed" class="tab-content">
+    {% for category, AKs in categories_with_aks %}
+        <div class="tab-pane fade {% if category.name == active_category %}show active{% endif %}" id="category_{{ category.pk }}">
+            <p><b>{{ category.name }}:</b> {{ category.description }}</p>
+            {% include "AKSubmission/ak_table.html" %}
+        </div>
+    {% endfor %}
+</div>
diff --git a/AKSubmission/templates/AKSubmission/ak_overview.html b/AKSubmission/templates/AKSubmission/ak_overview.html
new file mode 100644
index 00000000..45342420
--- /dev/null
+++ b/AKSubmission/templates/AKSubmission/ak_overview.html
@@ -0,0 +1,30 @@
+{% extends 'AKSubmission/submission_base.html' %}
+
+{% load i18n %}
+{% load fontawesome_5 %}
+
+{% load tags_AKSubmission %}
+
+{% block title %}{% trans "AKs" %}: {{ event.name }} - {% trans "AKs" %}{% endblock %}
+
+{% block imports %}
+    <style>
+        /* Prevent wrapping of buttons in AK table */
+        .table td:nth-child(5) {
+            white-space: nowrap;
+        }
+    </style>
+{% endblock %}
+
+{% block breadcrumbs %}
+    {% include "AKSubmission/submission_breadcrumbs.html" %}
+    <li class="breadcrumb-item"><a
+            href="{% url 'submit:submission_overview' event_slug=event.slug %}">{% trans "AK Submission" %}</a></li>
+    <li class="breadcrumb-item active">{% trans "AK List" %}</li>
+{% endblock %}
+
+{% block content %}
+    <h1>{% trans "AKs" %}: {{ event.name }}</h1>
+
+    {% include "AKSubmission/ak_list.html" %}
+{% endblock %}
diff --git a/AKSubmission/templates/AKSubmission/ak_list_table.html b/AKSubmission/templates/AKSubmission/ak_table.html
similarity index 94%
rename from AKSubmission/templates/AKSubmission/ak_list_table.html
rename to AKSubmission/templates/AKSubmission/ak_table.html
index 0f227396..485c982e 100644
--- a/AKSubmission/templates/AKSubmission/ak_list_table.html
+++ b/AKSubmission/templates/AKSubmission/ak_table.html
@@ -58,6 +58,10 @@
         <tr>
             <td colspan="5" class="small">{{ ak.description }}</td>
         </tr>
+    {% empty %}
+        <tr>
+            <td colspan="5" class="small">{% trans "There are no AKs in this category yet" %}</td>
+        </tr>
     {% endfor %}
     </tbody>
 </table>
diff --git a/AKSubmission/templates/AKSubmission/submission_overview.html b/AKSubmission/templates/AKSubmission/submission_overview.html
index 5c4ab1d0..dd09c528 100644
--- a/AKSubmission/templates/AKSubmission/submission_overview.html
+++ b/AKSubmission/templates/AKSubmission/submission_overview.html
@@ -63,28 +63,5 @@
         </div>
     {% endif %}
 
-    <h2>{% trans "Current AKs" %}</h2>
-
-    <noscript>
-        {% include "AKSubmission/ak_list_table.html" %}
-    </noscript>
-
-    <ul class="nav nav-tabs" style="margin-bottom:15px">
-        {% for category, _ in categories_with_aks %}
-            <li class="nav-item">
-                <a class="nav-link {% if forloop.first %}active{% endif %}" data-toggle="tab"
-                   href="#category_{{ category.pk }}">{{ category.name }}</a>
-            </li>
-        {% endfor %}
-    </ul>
-
-    <div id="akListTabbed" class="tab-content">
-        {% for category, AKs in categories_with_aks %}
-            <div class="tab-pane fade {% if forloop.first %}show active{% endif %}" id="category_{{ category.pk }}">
-                <p><b>{{ category.name }}:</b> {{ category.description }}</p>
-                {% include "AKSubmission/ak_list_table.html" %}
-            </div>
-        {% endfor %}
-    </div>
-
+    {% include "AKSubmission/ak_list.html" %}
 {% endblock %}
diff --git a/AKSubmission/urls.py b/AKSubmission/urls.py
index 3528b45d..982db8a7 100644
--- a/AKSubmission/urls.py
+++ b/AKSubmission/urls.py
@@ -16,7 +16,7 @@ urlpatterns = [
             path('ak/<int:pk>/add_slot/', views.AKSlotAddView.as_view(), name='akslot_add'),
             path('akslot/<int:pk>/edit/', views.AKSlotEditView.as_view(), name='akslot_edit'),
             path('akslot/<int:pk>/delete/', views.AKSlotDeleteView.as_view(), name='akslot_delete'),
-            path('aks/', views.AKListView.as_view(), name='ak_list'),
+            path('aks/', views.AKOverviewView.as_view(), name='ak_list'),
             path('aks/category/<int:category_pk>/', views.AKListByCategoryView.as_view(), name='ak_list_by_category'),
             path('aks/tag/<int:tag_pk>/', views.AKListByTagView.as_view(), name='ak_list_by_tag'),
             path('aks/track/<int:track_pk>/', views.AKListByTrackView.as_view(), name='ak_list_by_track'),
diff --git a/AKSubmission/views.py b/AKSubmission/views.py
index 78c2fcde..662d038d 100644
--- a/AKSubmission/views.py
+++ b/AKSubmission/views.py
@@ -13,10 +13,20 @@ from AKModel.views import FilterByEventSlugMixin
 from AKSubmission.forms import AKWishForm, AKOwnerForm, AKEditForm, AKSubmissionForm, AKDurationForm
 
 
-class SubmissionOverviewView(FilterByEventSlugMixin, ListView):
+class AKOverviewView(FilterByEventSlugMixin, ListView):
     model = AKCategory
     context_object_name = "categories"
-    template_name = "AKSubmission/submission_overview.html"
+    template_name = "AKSubmission/ak_overview.html"
+    wishes_as_category = False
+
+    def filter_aks(self, context, category):
+        return category.ak_set.all()
+
+    def get_active_category_name(self, context):
+        return context["categories_with_aks"][0][0].name
+
+    def get_table_title(self, context):
+        return _("All AKs")
 
     def get_context_data(self, *, object_list=None, **kwargs):
         context = super().get_context_data(object_list=object_list, **kwargs)
@@ -27,17 +37,35 @@ class SubmissionOverviewView(FilterByEventSlugMixin, ListView):
 
         for category in context["categories"]:
             aks_for_category = []
-            for ak in category.ak_set.all():
-                if settings.WISHES_AS_CATEGORY and ak.wish:
+            for ak in self.filter_aks(context, category):
+                if self.wishes_as_category and ak.wish:
                     ak_wishes.append(ak)
                 else:
                     aks_for_category.append(ak)
             categories_with_aks.append((category, aks_for_category))
 
-        if settings.WISHES_AS_CATEGORY:
+        if self.wishes_as_category:
             categories_with_aks.append(
                 ({"name": _("Wishes"), "pk": "wish", "description": _("AKs one would like to have")}, ak_wishes))
+
         context["categories_with_aks"] = categories_with_aks
+        context["active_category"] = self.get_active_category_name(context)
+        context['table_title'] = self.get_table_title(context)
+
+        return context
+
+
+class SubmissionOverviewView(AKOverviewView):
+    model = AKCategory
+    context_object_name = "categories"
+    template_name = "AKSubmission/submission_overview.html"
+    wishes_as_category = settings.WISHES_AS_CATEGORY
+
+    def get_table_title(self, context):
+        return _("Currently planned AKs")
+
+    def get_context_data(self, *, object_list=None, **kwargs):
+        context = super().get_context_data(object_list=object_list, **kwargs)
 
         # Get list of existing owners for event (for AK submission start)
         context["existingOwners"] = AKOwner.objects.filter(event=self.event)
@@ -45,6 +73,39 @@ class SubmissionOverviewView(FilterByEventSlugMixin, ListView):
         return context
 
 
+class AKListByCategoryView(AKOverviewView):
+    def dispatch(self, request, *args, **kwargs):
+        self.category = get_object_or_404(AKCategory, pk=kwargs['category_pk'])
+        return super().dispatch(request, *args, **kwargs)
+
+    def get_active_category_name(self, context):
+        return self.category.name
+
+
+class AKListByTagView(AKOverviewView):
+    def dispatch(self, request, *args, **kwargs):
+        self.tag = get_object_or_404(AKTag, pk=kwargs['tag_pk'])
+        return super().dispatch(request, *args, **kwargs)
+
+    def filter_aks(self, context, category):
+        return self.tag.ak_set.filter(event=self.event, category=category)
+
+    def get_table_title(self, context):
+        return f"{_('AKs with Tag')} = {self.tag.name}"
+
+
+class AKListByTrackView(AKOverviewView):
+    def dispatch(self, request, *args, **kwargs):
+        self.track = get_object_or_404(AKTrack, pk=kwargs['track_pk'])
+        return super().dispatch(request, *args, **kwargs)
+
+    def filter_aks(self, context, category):
+        return category.ak_set.filter(track=self.track)
+
+    def get_table_title(self, context):
+        return f"{_('AKs with Track')} = {self.track.name}"
+
+
 class AKDetailView(EventSlugMixin, DetailView):
     model = AK
     context_object_name = "ak"
@@ -60,56 +121,16 @@ class AKHistoryView(EventSlugMixin, DetailView):
 class AKListView(FilterByEventSlugMixin, ListView):
     model = AK
     context_object_name = "AKs"
-    template_name = "AKSubmission/ak_list.html"
-    filter_condition_string = ""
+    template_name = "AKSubmission/ak_overview.html"
+    table_title = ""
 
     def get_context_data(self, *, object_list=None, **kwargs):
         context = super().get_context_data(object_list=object_list, **kwargs)
         context['categories'] = AKCategory.objects.filter(event=self.event)
         context['tracks'] = AKTrack.objects.filter(event=self.event)
-        context['filter_condition_string'] = self.filter_condition_string
         return context
 
 
-class AKListByCategoryView(AKListView):
-    category = None
-
-    def get_queryset(self):
-        # Find category based on event slug
-        try:
-            self.category = AKCategory.objects.get(pk=self.kwargs['category_pk'])
-            self.filter_condition_string = f"{_('Category')} = {self.category.name}"
-        except AKCategory.DoesNotExist:
-            raise Http404
-        return super().get_queryset().filter(category=self.category)
-
-
-class AKListByTagView(AKListView):
-    tag = None
-
-    def get_queryset(self):
-        # Find tag based on event slug
-        try:
-            self.tag = AKTag.objects.get(pk=self.kwargs['tag_pk'])
-            self.filter_condition_string = f"{_('Tag')} = {self.tag.name}"
-        except AKTag.DoesNotExist:
-            raise Http404
-        return super().get_queryset().filter(tags=self.tag)
-
-
-class AKListByTrackView(AKListView):
-    track = None
-
-    def get_queryset(self):
-        # Find track based on event slug
-        try:
-            self.track = AKTrack.objects.get(pk=self.kwargs['track_pk'])
-            self.filter_condition_string = f"{_('Track')} = {self.track.name}"
-        except AKTrack.DoesNotExist:
-            raise Http404
-        return super().get_queryset().filter(track=self.track)
-
-
 class EventInactiveRedirectMixin:
     def get_error_message(self):
         return _("Event inactive. Cannot create or update.")
-- 
GitLab