From d35301d698e364e4c598a042e86a38daf5e69b07 Mon Sep 17 00:00:00 2001
From: Lucas Brandstaetter <lucas@brandstaetter.tech>
Date: Sun, 27 Oct 2024 01:52:00 +0200
Subject: [PATCH] Refactor ConferenceSelectionView

Move it to a separate file and add Docstring
---
 src/backoffice/forms.py                       |  2 +-
 .../locale/de/LC_MESSAGES/django.po           |  5 +-
 .../locale/en/LC_MESSAGES/django.po           | 10 +++-
 src/backoffice/templates/backoffice/base.html |  8 +--
 .../selection.html}                           |  4 +-
 src/backoffice/urls.py                        |  3 +-
 src/backoffice/views/conferences.py           | 50 +++++++++++++++++++
 src/backoffice/views/misc.py                  | 27 ++--------
 src/backoffice/views/mixins.py                |  2 +-
 9 files changed, 77 insertions(+), 34 deletions(-)
 rename src/backoffice/templates/backoffice/{conferenceselection.html => conferences/selection.html} (89%)
 create mode 100644 src/backoffice/views/conferences.py

diff --git a/src/backoffice/forms.py b/src/backoffice/forms.py
index 70c60d5d6..e4482cf68 100644
--- a/src/backoffice/forms.py
+++ b/src/backoffice/forms.py
@@ -36,7 +36,7 @@ from core.utils import str2timedelta
 logger = logging.getLogger(__name__)
 
 
-class ConferenceForm(forms.Form):
+class ConferenceSelectionForm(forms.Form):
     conference_slug = forms.SlugField()
 
 
diff --git a/src/backoffice/locale/de/LC_MESSAGES/django.po b/src/backoffice/locale/de/LC_MESSAGES/django.po
index 11c65a72b..3624aaca3 100644
--- a/src/backoffice/locale/de/LC_MESSAGES/django.po
+++ b/src/backoffice/locale/de/LC_MESSAGES/django.po
@@ -828,9 +828,12 @@ msgstr "Schedules"
 msgid "nav_workadventure"
 msgstr "WorkAdventure"
 
-msgid "conference_selection"
+msgid "Conference__Selection"
 msgstr "Konferenz auswählen"
 
+msgid "Conferences"
+msgstr "Konferenzen"
+
 msgid "nav_profile"
 msgstr "Profil"
 
diff --git a/src/backoffice/locale/en/LC_MESSAGES/django.po b/src/backoffice/locale/en/LC_MESSAGES/django.po
index 21c3fd49d..1ce5dc96d 100644
--- a/src/backoffice/locale/en/LC_MESSAGES/django.po
+++ b/src/backoffice/locale/en/LC_MESSAGES/django.po
@@ -827,9 +827,12 @@ msgstr "Schedules"
 msgid "nav_workadventure"
 msgstr "WorkAdventure"
 
-msgid "conference_selection"
+msgid "Conference__Selection"
 msgstr "Select Conference"
 
+msgid "Conferences"
+msgstr "Conferences"
+
 msgid "nav_profile"
 msgstr "Profile"
 
@@ -1965,3 +1968,8 @@ msgstr "sessions"
 
 msgid "wa_textures"
 msgstr "textures"
+
+#, fuzzy
+#~| msgid "Conference"
+#~ msgid "nav_conference"
+#~ msgstr "Conference"
diff --git a/src/backoffice/templates/backoffice/base.html b/src/backoffice/templates/backoffice/base.html
index 20517eae6..71b80a48e 100644
--- a/src/backoffice/templates/backoffice/base.html
+++ b/src/backoffice/templates/backoffice/base.html
@@ -71,10 +71,10 @@
           {% endif %}
         </ul>
         <ul class="navbar-nav">
-          {% if conferences|length > 1 or active_page == 'conference_selection' %}
-          <li class="nav-item{% if active_page == 'conference_selection' %} active{% endif %}">
-            <a class="nav-link" href="{% url 'backoffice:conference_selection' %}" title="{% trans 'conference_selection' %}">
-              {{ conference.slug }}
+          {% if conferences|length > 1 or active_page == 'conferences' %}
+          <li class="nav-item{% if active_page == 'conferences' %} active{% endif %}">
+            <a class="nav-link" href="{% url 'backoffice:conferences' %}" title="{% trans 'Conference__Selection' %}">
+              {% trans 'Conferences' %}
             </a>
           </li>
           {% endif %}
diff --git a/src/backoffice/templates/backoffice/conferenceselection.html b/src/backoffice/templates/backoffice/conferences/selection.html
similarity index 89%
rename from src/backoffice/templates/backoffice/conferenceselection.html
rename to src/backoffice/templates/backoffice/conferences/selection.html
index 9cad9c308..7ded69a53 100644
--- a/src/backoffice/templates/backoffice/conferenceselection.html
+++ b/src/backoffice/templates/backoffice/conferences/selection.html
@@ -7,10 +7,10 @@
         <div class="card-header">{% trans "Conferences__selection__header" %}</div>
         <div class="card-body">
           {% if conferences %}
-            <form action="{% url 'backoffice:conference_selection' %}" method="POST">
+            <form action="{% url 'backoffice:conferences' %}" method="POST">
               {% csrf_token %}
               <p>
-                {% trans Conferences__selection__help%}
+                {% trans Conferences__selection__help %}
               </p>
               <div class="mb-3">
                 <label class="form-label" for="conference">{% trans 'Conference' %}:</label>
diff --git a/src/backoffice/urls.py b/src/backoffice/urls.py
index 6931b8b45..748dfa12c 100644
--- a/src/backoffice/urls.py
+++ b/src/backoffice/urls.py
@@ -1,6 +1,7 @@
 from django.urls import path, re_path
 from django.views.generic import RedirectView
 
+from backoffice.views.conferences import ConferenceSelectionView
 from backoffice.views.map import (
     FloorCreateView,
     FloorListView,
@@ -47,7 +48,7 @@ urlpatterns = [
     path('login', auth.LoginView.as_view(), name='login'),
     path('logout', auth.LogoutView.as_view(), name='logout'),
     path('auth_debug', auth.AuthDebugView.as_view()),
-    path('conferences', misc.ConferenceSelectionView.as_view(), name='conference_selection'),
+    path('conferences', ConferenceSelectionView.as_view(), name='conferences'),
     path('wiki', wiki.WikiOverviewView.as_view(), name='wiki'),
     path('wiki/namespaces', wiki.NamespaceListView.as_view(), name='wiki-namespaces'),
     path('wiki/locks', wiki.LockListView.as_view(), name='wiki-locks'),
diff --git a/src/backoffice/views/conferences.py b/src/backoffice/views/conferences.py
new file mode 100644
index 000000000..facd80268
--- /dev/null
+++ b/src/backoffice/views/conferences.py
@@ -0,0 +1,50 @@
+from django.shortcuts import redirect
+from django.views.generic.edit import FormView
+
+from core.models import Conference
+
+from backoffice.forms import ConferenceSelectionForm
+from backoffice.views.mixins import LoginRequiredMixin
+
+
+class ConferenceSelectionView(LoginRequiredMixin, FormView):
+    """
+    A class-based view that will let you select the current :model:`core.Conference` for you session.
+
+    It will also be used if no conference is available and show an error message to the user.
+
+    **Context:**
+
+    ``conferences``
+        A list of all available :model:`core.Conference` for the current user.
+
+    **Template:**
+
+    :template:`backoffice/conferences/selection.html`
+
+    """
+
+    form_class = ConferenceSelectionForm
+    template_name = 'backoffice/conferences/selection.html'
+    active_page = 'conferences'
+
+    def form_valid(self, form: ConferenceSelectionForm):
+        """Sets the selected conference for the session if the form is valid.
+
+        Args:
+            form (ConferenceForm): A form containing the selected conference
+
+        Returns:
+            Response: A redirection to the backoffice index
+        """
+        conf_slug = form.cleaned_data['conference_slug']
+        conference = Conference.objects.accessible_by_user(self.request.user).get(slug=conf_slug)
+        self.request.session['conference'] = conference.slug
+        return redirect('backoffice:index')
+
+    def get_context_data(self, *args, **kwargs):
+        return {
+            **super().get_context_data(*args, **kwargs),
+            'conferences': Conference.objects.accessible_by_user(self.request.user),
+            'active_page': self.active_page,
+        }
diff --git a/src/backoffice/views/misc.py b/src/backoffice/views/misc.py
index 0728cf718..5ca163188 100644
--- a/src/backoffice/views/misc.py
+++ b/src/backoffice/views/misc.py
@@ -1,28 +1,9 @@
-from django.shortcuts import redirect, render
+from django.shortcuts import render
 from django.views.generic import View
-from django.views.generic.edit import FormView
 
-from core.models import Assembly, Conference
+from core.models.assemblies import Assembly
 
-from backoffice.forms import ConferenceForm
-from backoffice.views.mixins import ConferenceLoginRequiredMixin, LoginRequiredMixin
-
-
-class ConferenceSelectionView(LoginRequiredMixin, FormView):
-    form_class = ConferenceForm
-    template_name = 'backoffice/conferenceselection.html'
-
-    def form_valid(self, form):
-        conf_slug = form.cleaned_data['conference_slug']
-        conference = Conference.objects.accessible_by_user(self.request.user).get(slug=conf_slug)
-        self.request.session['conference'] = conference.slug
-        return redirect('backoffice:index')
-
-    def get_context_data(self, *args, **kwargs):
-        ctx = super().get_context_data(*args, **kwargs)
-        ctx['conferences'] = Conference.objects.accessible_by_user(self.request.user)
-        ctx['active_page'] = 'conference_selection'
-        return ctx
+from backoffice.views.mixins import ConferenceLoginRequiredMixin
 
 
 class IndexView(ConferenceLoginRequiredMixin, View):
@@ -30,7 +11,7 @@ class IndexView(ConferenceLoginRequiredMixin, View):
 
     def get(self, *args, **kwargs):
         if self.request.user.is_authenticated:
-            myassemblies = list(Assembly.objects.associated_to_user(conference=self.conference, user=self.request.user))
+            myassemblies = list(Assembly.objects.associated_with_user(conference=self.conference, user=self.request.user))
 
             # remove stored backlink for assembly pages from the session if it is set, so the backlink will go to the overview, which is the default
             self.request.session.pop('assembly_back', None)
diff --git a/src/backoffice/views/mixins.py b/src/backoffice/views/mixins.py
index b47660a1d..a56853bd8 100644
--- a/src/backoffice/views/mixins.py
+++ b/src/backoffice/views/mixins.py
@@ -80,7 +80,7 @@ class ConferenceRequiredMixin(PermissionRequiredMixin):
 
     def dispatch(self, request, *args, **kwargs):
         if self.require_conference and self.conference is None:
-            return redirect('backoffice:conference_selection')
+            return redirect('backoffice:conferences')
         if not self.has_permission():
             raise PermissionDenied('Insufficient privileges.')
         return super().dispatch(request, *args, **kwargs)
-- 
GitLab