From fb4909fc36d3d33233feca43eb30df2c9fa1e349 Mon Sep 17 00:00:00 2001
From: imperosol <thgirod@hotmail.com>
Date: Sat, 1 Mar 2025 12:47:08 +0100
Subject: [PATCH] merge `ClubEditView` and `ClubEditPropView`

---
 club/forms.py                                 | 13 +++-
 club/templates/club/edit_club.jinja           | 54 ++++++++++++++
 club/urls.py                                  |  2 -
 club/views.py                                 | 73 ++++++++-----------
 .../commands/generate_galaxy_test_data.py     |  3 +-
 5 files changed, 95 insertions(+), 50 deletions(-)
 create mode 100644 club/templates/club/edit_club.jinja

diff --git a/club/forms.py b/club/forms.py
index e63627eb..ab8f5757 100644
--- a/club/forms.py
+++ b/club/forms.py
@@ -34,13 +34,20 @@ from counter.models import Counter
 
 
 class ClubEditForm(forms.ModelForm):
+    error_css_class = "error"
+    required_css_class = "required"
+
     class Meta:
         model = Club
         fields = ["address", "logo", "short_description"]
+        widgets = {"short_description": forms.Textarea()}
 
-    def __init__(self, *args, **kwargs):
-        super().__init__(*args, **kwargs)
-        self.fields["short_description"].widget = forms.Textarea()
+
+class ClubAdminEditForm(ClubEditForm):
+    admin_fields = ["name", "parent", "is_active"]
+
+    class Meta(ClubEditForm.Meta):
+        fields = ["name", "parent", "is_active", *ClubEditForm.Meta.fields]
 
 
 class MailingForm(forms.Form):
diff --git a/club/templates/club/edit_club.jinja b/club/templates/club/edit_club.jinja
new file mode 100644
index 00000000..2964fb64
--- /dev/null
+++ b/club/templates/club/edit_club.jinja
@@ -0,0 +1,54 @@
+{% extends "core/base.jinja" %}
+
+{% block title %}
+  {% trans name=object %}Edit {{ name }}{% endtrans %}
+{% endblock %}
+
+{% block content %}
+  <h2>{% trans name=object %}Edit {{ name }}{% endtrans %}</h2>
+
+  <form action="" method="post" enctype="multipart/form-data">
+    {% csrf_token %}
+
+    {{ form.non_field_errors() }}
+
+    {% if form.admin_fields %}
+      {# If the user is admin, display the admin fields,
+         and explicitly separate them from the non-admin ones,
+         with some help text.
+         Non-admin users will only see the regular form fields,
+         so they don't need thoses explanations #}
+      <h3>{% trans %}Club properties{% endtrans %}</h3>
+      <p class="helptext">
+        {% trans trimmed %}
+          The following form fields are linked to the core properties of a club.
+          Only admin users can see and edit them.
+        {% endtrans %}
+      </p>
+      <fieldset class="required margin-bottom">
+        {% for field_name in form.admin_fields %}
+          {% set field = form.pop(field_name) %}
+          <div class="form-group">
+            {{ field.errors }}
+            {{ field.label_tag() }}
+            {{ field }}
+          </div>
+          {# Remove the the admin fields from the form.
+             The remaining non-admin fields will be rendered
+             at once with a simple {{ form.as_p() }} #}
+          {% set _ = form.fields.pop(field_name) %}
+        {% endfor %}
+      </fieldset>
+
+      <h3>{% trans %}Club informations{% endtrans %}</h3>
+      <p class="helptext">
+        {% trans trimmed %}
+          The following form fields are linked to the basic description of a club.
+          All board members of this club can see and edit them.
+        {% endtrans %}
+      </p>
+    {% endif %}
+    {{ form.as_p() }}
+    <p><input type="submit" value="{% trans %}Save{% endtrans %}" /></p>
+  </form>
+{% endblock content %}
diff --git a/club/urls.py b/club/urls.py
index 5dcd492a..664d93a6 100644
--- a/club/urls.py
+++ b/club/urls.py
@@ -26,7 +26,6 @@ from django.urls import path
 
 from club.views import (
     ClubCreateView,
-    ClubEditPropView,
     ClubEditView,
     ClubListView,
     ClubMailingView,
@@ -70,7 +69,6 @@ urlpatterns = [
     path(
         "<int:club_id>/sellings/csv/", ClubSellingCSVView.as_view(), name="sellings_csv"
     ),
-    path("<int:club_id>/prop/", ClubEditPropView.as_view(), name="club_prop"),
     path("<int:club_id>/tools/", ClubToolsView.as_view(), name="tools"),
     path("<int:club_id>/mailing/", ClubMailingView.as_view(), name="mailing"),
     path(
diff --git a/club/views.py b/club/views.py
index e03df328..0c32cfad 100644
--- a/club/views.py
+++ b/club/views.py
@@ -56,12 +56,7 @@ from com.views import (
     PosterEditBaseView,
     PosterListBaseView,
 )
-from core.auth.mixins import (
-    CanCreateMixin,
-    CanEditMixin,
-    CanEditPropMixin,
-    CanViewMixin,
-)
+from core.auth.mixins import CanCreateMixin, CanEditMixin, CanViewMixin
 from core.models import PageRev
 from core.views import DetailFormView, PageEditViewBase
 from core.views.mixins import TabedViewMixin
@@ -113,21 +108,23 @@ class ClubTabsMixin(TabedViewMixin):
                 }
             )
         if self.request.user.can_edit(self.object):
-            tab_list.append(
-                {
-                    "url": reverse("club:tools", kwargs={"club_id": self.object.id}),
-                    "slug": "tools",
-                    "name": _("Tools"),
-                }
-            )
-            tab_list.append(
-                {
-                    "url": reverse(
-                        "club:club_edit", kwargs={"club_id": self.object.id}
-                    ),
-                    "slug": "edit",
-                    "name": _("Edit"),
-                }
+            tab_list.extend(
+                [
+                    {
+                        "url": reverse(
+                            "club:tools", kwargs={"club_id": self.object.id}
+                        ),
+                        "slug": "tools",
+                        "name": _("Tools"),
+                    },
+                    {
+                        "url": reverse(
+                            "club:club_edit", kwargs={"club_id": self.object.id}
+                        ),
+                        "slug": "edit",
+                        "name": _("Edit"),
+                    },
+                ]
             )
             if self.object.page and self.request.user.can_edit(self.object.page):
                 tab_list.append(
@@ -165,16 +162,6 @@ class ClubTabsMixin(TabedViewMixin):
                     },
                 ]
             )
-        if self.request.user.is_owner(self.object):
-            tab_list.append(
-                {
-                    "url": reverse(
-                        "club:club_prop", kwargs={"club_id": self.object.id}
-                    ),
-                    "slug": "props",
-                    "name": _("Props"),
-                }
-            )
         return tab_list
 
 
@@ -461,23 +448,23 @@ class ClubSellingCSVView(ClubSellingView):
 
 
 class ClubEditView(ClubTabsMixin, CanEditMixin, UpdateView):
-    """Edit a Club's main informations (for the club's members)."""
+    """Edit a Club.
+
+    Regular club board members will be able to edit the main infos
+    (like the logo and the description).
+    Admins will also be able to edit the club properties
+    (like the name and the parent club).
+    """
 
     model = Club
     pk_url_kwarg = "club_id"
-    form_class = ClubEditForm
-    template_name = "core/edit.jinja"
+    template_name = "club/edit_club.jinja"
     current_tab = "edit"
 
-
-class ClubEditPropView(ClubTabsMixin, CanEditPropMixin, UpdateView):
-    """Edit the properties of a Club object (for the Sith admins)."""
-
-    model = Club
-    pk_url_kwarg = "club_id"
-    fields = ["name", "parent", "is_active"]
-    template_name = "core/edit.jinja"
-    current_tab = "props"
+    def get_form_class(self):
+        if self.object.is_owned_by(self.request.user):
+            return ClubAdminEditForm
+        return ClubEditForm
 
 
 class ClubCreateView(PermissionRequiredMixin, CreateView):
diff --git a/galaxy/management/commands/generate_galaxy_test_data.py b/galaxy/management/commands/generate_galaxy_test_data.py
index ada43a36..2f800db0 100644
--- a/galaxy/management/commands/generate_galaxy_test_data.py
+++ b/galaxy/management/commands/generate_galaxy_test_data.py
@@ -130,8 +130,7 @@ class Command(BaseCommand):
         self.clubs = Club.objects.bulk_create(
             [
                 Club(
-                    unix_name=f"galaxy-club-{i}",
-                    name=f"club-{i}",
+                    name=f"galaxy-club-{i}",
                     board_group=Group.objects.create(name=f"board {i}"),
                     members_group=Group.objects.create(name=f"members {i}"),
                 )