From 0b43d82dda8e9be78b263fa07ccc8a08fd012f9a Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Fri, 7 Jul 2017 04:13:25 +0200 Subject: [PATCH] Change le mode de retrait des droits(en tableau) --- users/forms.py | 9 ++++- users/models.py | 2 +- users/templates/users/del_right.html | 58 ++++++++++++++++++++++++++++ users/views.py | 23 ++++++----- 4 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 users/templates/users/del_right.html diff --git a/users/forms.py b/users/forms.py index 33504c99..b9401846 100644 --- a/users/forms.py +++ b/users/forms.py @@ -258,7 +258,7 @@ class NewListRightForm(ListRightForm): self.fields['gid'].label = 'Gid, attention, cet attribut ne doit pas être modifié après création' class DelListRightForm(ModelForm): - listrights = forms.ModelMultipleChoiceField(queryset=ListRight.objects.all().select_related('user'), label="Droits actuels", widget=forms.CheckboxSelectMultiple) + listrights = forms.ModelMultipleChoiceField(queryset=ListRight.objects.all().select_related('user'), label="Droits actuels", widget=forms.CheckboxSelectMultiple) class Meta: exclude = ['listright','gid'] @@ -284,7 +284,12 @@ class RightForm(ModelForm): class DelRightForm(ModelForm): - rights = forms.ModelMultipleChoiceField(queryset=Right.objects.all(), label="Droits actuels", widget=forms.CheckboxSelectMultiple) + rights = forms.ModelMultipleChoiceField(queryset=Right.objects.all(), widget=forms.CheckboxSelectMultiple) + + def __init__(self, right, *args, **kwargs): + super(DelRightForm, self).__init__(*args, **kwargs) + self.fields['rights'].queryset = Right.objects.filter(right=right) + class Meta: model = Right diff --git a/users/models.py b/users/models.py index 0caa21ff..e0477a1d 100644 --- a/users/models.py +++ b/users/models.py @@ -461,7 +461,7 @@ class Right(models.Model): unique_together = ("user", "right") def __str__(self): - return str(self.user) + " - " + str(self.right) + return str(self.user) @receiver(post_save, sender=Right) def right_post_save(sender, **kwargs): diff --git a/users/templates/users/del_right.html b/users/templates/users/del_right.html new file mode 100644 index 00000000..740778a2 --- /dev/null +++ b/users/templates/users/del_right.html @@ -0,0 +1,58 @@ +{% extends "users/sidebar.html" %} +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load bootstrap3 %} + +{% block title %}Création et modification d'utilisateur{% endblock %} + + +{% block content %} + +

Gestion des droits

+ +
+ {% csrf_token %} + + + + {% for key, values in userform.items %} + + {% endfor %} + + + + {% for key, values in userform.items %} + {% bootstrap_form_errors values %} + + {% endfor %} + +
{{ key }}
{{ values.rights }}
+ {% bootstrap_button "Modifier" button_type="submit" icon="star" %} +
+ +
+
+
+{% endblock %} diff --git a/users/views.py b/users/views.py index 18f2241f..d11c63f3 100644 --- a/users/views.py +++ b/users/views.py @@ -297,16 +297,19 @@ def add_right(request, userid): @permission_required('bureau') def del_right(request): """ Supprimer un droit à un user, need droit bureau """ - user_right_list = DelRightForm(request.POST or None) - if user_right_list.is_valid(): - right_del = user_right_list.cleaned_data['rights'] - with transaction.atomic(), reversion.create_revision(): - reversion.set_user(request.user) - reversion.set_comment("Retrait des droit %s" % ','.join(str(deleted_right) for deleted_right in right_del)) - right_del.delete() - messages.success(request, "Droit retiré avec succès") - return redirect("/users/") - return form({'userform': user_right_list}, 'users/user.html', request) + user_right_list = dict() + for right in ListRight.objects.all(): + user_right_list[right]= DelRightForm(right, request.POST or None) + for keys, right_item in user_right_list.items(): + if right_item.is_valid(): + right_del = right_item.cleaned_data['rights'] + with transaction.atomic(), reversion.create_revision(): + reversion.set_user(request.user) + reversion.set_comment("Retrait des droit %s" % ','.join(str(deleted_right) for deleted_right in right_del)) + right_del.delete() + messages.success(request, "Droit retiré avec succès") + return redirect("/users/") + return form({'userform': user_right_list}, 'users/del_right.html', request) @login_required @permission_required('bofh')