From 72ef788acb8347d305ad313776a58228be34acba Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Fri, 7 Jan 2022 16:31:56 +0100 Subject: [PATCH] =?UTF-8?q?Rajoute=20la=20cr=C3=A9ation=20de=20groupes=20d?= =?UTF-8?q?e=20mani=C3=A8re=20plus=20simple=20pour=20les=20admins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wiki_groups/templates/wiki_groups/admin.html | 5 ++- .../templates/wiki_groups/managed.html | 20 +++++++++++- wiki_groups/views.py | 32 +++++++++++++++++-- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/wiki_groups/templates/wiki_groups/admin.html b/wiki_groups/templates/wiki_groups/admin.html index 590fad7..5c1a49a 100644 --- a/wiki_groups/templates/wiki_groups/admin.html +++ b/wiki_groups/templates/wiki_groups/admin.html @@ -4,7 +4,10 @@ {% block wiki_site_title %}Groupes administrés - WikiENS{% endblock %} {% block wiki_contents %} -

Gestion du groupe « {{ wikigroup }} »

+

+ Gestion du groupe « {{ wikigroup }} » + Retour +


diff --git a/wiki_groups/templates/wiki_groups/managed.html b/wiki_groups/templates/wiki_groups/managed.html index 2a4b3e8..c23cc1c 100644 --- a/wiki_groups/templates/wiki_groups/managed.html +++ b/wiki_groups/templates/wiki_groups/managed.html @@ -6,6 +6,25 @@

Liste des groupes administrés


+{% if request.user.is_staff %} +
+ {% csrf_token %} +
+ +
+ +
+ {% if form.group.errors %} + {% for error in form.group.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ Entrer le nom du groupe à créer +
+
+{% endif %} + {% if wikigroup_list %}
{% for group in wikigroup_list %} @@ -14,5 +33,4 @@
{% endif %} - {% endblock %} diff --git a/wiki_groups/views.py b/wiki_groups/views.py index db751b9..7f9aada 100644 --- a/wiki_groups/views.py +++ b/wiki_groups/views.py @@ -63,10 +63,17 @@ class StaffMixin(UserPassesTestMixin): return self.request.user.is_staff -class ManagedGroupsView(LoginRequiredMixin, ListView): +class ManagedGroupsView(LoginRequiredMixin, BaseFormView, ListView): + """Displays the list of managed groups and allows creating new groups""" + model = WikiGroup template_name = "wiki_groups/managed.html" + form_class = CreateGroupForm + + def get_success_url(self): + return reverse("wiki_groups:admin-group", args=[self.group_pk]) + def get_queryset(self): if self.request.user.is_staff: return WikiGroup.objects.select_related("django_group") @@ -84,6 +91,24 @@ class ManagedGroupsView(LoginRequiredMixin, ListView): return ctx + def post(self, request, *args, **kwargs): + form = self.get_form() + + # Only the staff can create new groups + if self.user.is_staff and form.is_valid(): + return self.form_valid(form) + + return self.form_invalid(form) + + def form_valid(self, form): + self.group_pk = form.cleaned_data["group"].pk + return super().form_valid(form) + + def form_invalid(self, form): + self.object_list = self.get_queryset() + + return super().form_invalid(form) + class WikiGroupView(WikiGroupMixin, DetailView): template_name = "wiki_groups/admin.html" @@ -214,7 +239,10 @@ class CreateGroupView(WikiGroupMixin, BaseFormView): new_group = form.cleaned_data["group"] self.object.includes_groups.add(new_group) - new_group.managers.add(self.request.user) + + # On ne met pas les admins dans les managers explicites + if not self.request.user.is_staff: + new_group.managers.add(self.request.user) return super().form_valid(form) -- 2.47.0