From 1c49c17801dba7f6f5c990bf516531ea2b607997 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Mon, 4 Jul 2016 22:37:04 +0200 Subject: [PATCH] Modifie la gestion des droits, depuis le profil maintenant --- users/models.py | 3 +-- users/templates/users/profil.html | 7 +++++++ users/templates/users/sidebar.html | 1 - users/urls.py | 2 +- users/views.py | 20 ++++++++++++++++---- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/users/models.py b/users/models.py index 68807056..22076e7b 100644 --- a/users/models.py +++ b/users/models.py @@ -131,12 +131,11 @@ class SchoolForm(ModelForm): class RightForm(ModelForm): def __init__(self, *args, **kwargs): super(RightForm, self).__init__(*args, **kwargs) - self.fields['user'].label = 'Utilisateur' self.fields['right'].label = 'Droit' class Meta: model = Right - fields = ['user', 'right'] + fields = ['right'] class DelRightForm(ModelForm): rights = forms.ModelMultipleChoiceField(queryset=Right.objects.all(), label="Droits actuels", widget=forms.CheckboxSelectMultiple) diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index d25933c2..48bcdb64 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -8,6 +8,7 @@ Editer Changer le mot de passe Changer le statut + Ajouter un droit

@@ -71,6 +72,12 @@ {% else %} {% endif %} + + {% if list_droits %} + + {% else %} + + {% endif %}
DésactivéeDroits{% for droit in list_droits %}{{ droit.right }} - {% endfor %}Aucun

Machines :

Ajouter une machine

diff --git a/users/templates/users/sidebar.html b/users/templates/users/sidebar.html index 3d262a3b..a976d4ac 100644 --- a/users/templates/users/sidebar.html +++ b/users/templates/users/sidebar.html @@ -6,6 +6,5 @@

Liste des adhérents

Ajouter un bannissement

Gérer les bannissements

-

Ajouter un droit rezo

Retirer un droit rezo

{% endblock %} diff --git a/users/urls.py b/users/urls.py index 87da16c6..e4a92a27 100644 --- a/users/urls.py +++ b/users/urls.py @@ -11,7 +11,7 @@ urlpatterns = [ url(r'^edit_ban/(?P[0-9]+)$', views.edit_ban, name='edit-ban'), url(r'^add_whitelist/(?P[0-9]+)$', views.add_whitelist, name='add-whitelist'), url(r'^edit_whitelist/(?P[0-9]+)$', views.edit_whitelist, name='edit-whitelist'), - url(r'^add_right/$', views.add_right, name='add-right'), + url(r'^add_right/(?P[0-9]+)$', views.add_right, name='add-right'), url(r'^del_right/$', views.del_right, name='del-right'), url(r'^profil/$', views.profil, name='profil'), url(r'^$', views.index, name='index'), diff --git a/users/views.py b/users/views.py index 3140e088..d29e0621 100644 --- a/users/views.py +++ b/users/views.py @@ -7,6 +7,7 @@ from django.core.context_processors import csrf from django.template import Context, RequestContext, loader from django.contrib import messages from django.db.models import Max +from django.db import IntegrityError from django.utils import timezone from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm, Whitelist, WhitelistForm @@ -128,11 +129,21 @@ def password(request, userid): return redirect("/users/") return form({'userform': user_form}, 'users/user.html', request) -def add_right(request): +def add_right(request, userid): + try: + user = User.objects.get(pk=userid) + except User.DoesNotExist: + messages.error(request, u"Utilisateur inexistant" ) + return redirect("/users/") right = RightForm(request.POST or None) if right.is_valid(): - right.save() - messages.success(request, "Droit ajouté") + right = right.save(commit=False) + right.user = user + try: + right.save() + messages.success(request, "Droit ajouté") + except IntegrityError: + pass return redirect("/users/") return form({'userform': right}, 'users/user.html', request) @@ -226,7 +237,8 @@ def profil(request): end_bans=end_ban(users) if(is_whitelisted(users)): end_whitelists=end_whitelist(users) - return render(request, 'users/profil.html', {'user': users, 'machine_list' :machines, 'facture_list':factures, 'ban_list':bans, 'white_list':whitelists,'end_ban':end_bans,'end_whitelist':end_whitelists, 'end_adhesion':end_adhesion(users), 'actif':has_access(users)}) + list_droits = Right.objects.filter(user=users) + return render(request, 'users/profil.html', {'user': users, 'machine_list' :machines, 'facture_list':factures, 'ban_list':bans, 'white_list':whitelists,'end_ban':end_bans,'end_whitelist':end_whitelists, 'end_adhesion':end_adhesion(users), 'actif':has_access(users), 'list_droits': list_droits}) return redirect("/users/") return redirect("/users/")