diff --git a/users/models.py b/users/models.py index 30acbf35..4867a3df 100644 --- a/users/models.py +++ b/users/models.py @@ -1,5 +1,6 @@ from django.db import models from django.forms import ModelForm +from django import forms class User(models.Model): STATE_ACTIVE = 0 @@ -23,7 +24,7 @@ class User(models.Model): state = models.IntegerField(choices=STATES, default=STATE_ACTIVE) def __str__(self): - return self.name + return self.pseudo class Right(models.Model): user = models.ForeignKey('User', on_delete=models.PROTECT) @@ -32,6 +33,9 @@ class Right(models.Model): class Meta: unique_together = ("user", "right") + def __str__(self): + return str(self.user) + " - " + str(self.right) + class School(models.Model): name = models.CharField(max_length=255) @@ -90,3 +94,10 @@ class RightForm(ModelForm): class Meta: model = Right fields = ['user', 'right'] + +class DelRightForm(ModelForm): + rights = forms.ModelMultipleChoiceField(queryset=Right.objects.all(), label="Droits actuels", widget=forms.CheckboxSelectMultiple) + + class Meta: + model = Right + exclude = ['user', 'right'] diff --git a/users/templates/users/sidebar.html b/users/templates/users/sidebar.html index 155ba0a4..edd57ccc 100644 --- a/users/templates/users/sidebar.html +++ b/users/templates/users/sidebar.html @@ -3,4 +3,6 @@ {% block sidebar %}

Créer un adhérent

Liste des adhérents

+

Ajouter un droit rezo

+

Retirer un droit rezo

{% endblock %} diff --git a/users/urls.py b/users/urls.py index 4a770010..31f97abd 100644 --- a/users/urls.py +++ b/users/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ url(r'^state/(?P[0-9]+)$', views.state, name='state'), url(r'^password/(?P[0-9]+)$', views.password, name='password'), url(r'^add_right/$', views.add_right, name='add-right'), + url(r'^del_right/$', views.del_right, name='del-right'), url(r'^$', views.index, name='index'), ] diff --git a/users/views.py b/users/views.py index dadc4329..b20c9a97 100644 --- a/users/views.py +++ b/users/views.py @@ -7,7 +7,7 @@ from django.core.context_processors import csrf from django.template import Context, RequestContext, loader from django.contrib import messages -from users.models import User, UserForm, InfoForm, PasswordForm, StateForm, RightForm +from users.models import User, Right, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm from users.forms import PassForm from re2o.login import makeSecret, hashNT @@ -77,6 +77,15 @@ def add_right(request): return redirect("/users/") return form({'userform': right}, 'users/user.html', request) +def del_right(request): + right = DelRightForm(request.POST or None) + if right.is_valid(): + right_del = right.cleaned_data['rights'] + right_del.delete() + messages.success(request, "Droit retiré avec succès") + return redirect("/users/") + return form({'userform': right}, 'users/user.html', request) + def index(request): users_list = User.objects.order_by('pk') return render(request, 'users/index.html', {'users_list': users_list})