From f2b2a390918a9bcb4688880bd76eac8b40f64f1d Mon Sep 17 00:00:00 2001 From: chirac Date: Sat, 2 Jul 2016 02:42:04 +0200 Subject: [PATCH] =?UTF-8?q?Redirections=20une=20fois=20une=20modification?= =?UTF-8?q?=20effectu=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- users/admin.py | 10 +++++++++- users/models.py | 23 +++++++++++++++++++++++ users/views.py | 22 ++++++++++++++-------- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/users/admin.py b/users/admin.py index 6e6adefd..1336498a 100644 --- a/users/admin.py +++ b/users/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from .models import User, School +from .models import User, School, Right, ListRight class UserAdmin(admin.ModelAdmin): list_display = ('name','surname','pseudo','email', 'school', 'state') @@ -8,5 +8,13 @@ class UserAdmin(admin.ModelAdmin): class SchoolAdmin(admin.ModelAdmin): list_display = ('name',) +class ListRightAdmin(admin.ModelAdmin): + list_display = ('listright',) + +class RightAdmin(admin.ModelAdmin): + list_display = ('user', 'right') + admin.site.register(User, UserAdmin) admin.site.register(School, SchoolAdmin) +admin.site.register(Right, RightAdmin) +admin.site.register(ListRight, ListRightAdmin) diff --git a/users/models.py b/users/models.py index c7f52ed3..30acbf35 100644 --- a/users/models.py +++ b/users/models.py @@ -25,12 +25,25 @@ class User(models.Model): def __str__(self): return self.name +class Right(models.Model): + user = models.ForeignKey('User', on_delete=models.PROTECT) + right = models.ForeignKey('ListRight', on_delete=models.PROTECT) + + class Meta: + unique_together = ("user", "right") + class School(models.Model): name = models.CharField(max_length=255) def __str__(self): return self.name +class ListRight(models.Model): + listright = models.CharField(max_length=255) + + def __str__(self): + return self.listright + class UserForm(ModelForm): def __init__(self, *args, **kwargs): super(InfoForm, self).__init__(*args, **kwargs) @@ -67,3 +80,13 @@ class SchoolForm(ModelForm): class Meta: model = School fields = ['name'] + +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'] diff --git a/users/views.py b/users/views.py index 8c6f0b61..dadc4329 100644 --- a/users/views.py +++ b/users/views.py @@ -18,14 +18,12 @@ def form(ctx, template, request): return render_to_response(template, c, context_instance=RequestContext(request)) def new_user(request): - if request.method == 'POST': - user = InfoForm(request.POST) - if user.is_valid(): - user.save() - return form({'userform': user}, 'users/user.html', request) - else: - user = InfoForm() - return form({'userform': user}, 'users/user.html', request) + user = InfoForm(request.POST or None) + if user.is_valid(): + user.save() + messages.success(request, "L'utilisateur a été crée") + return redirect("/users/") + return form({'userform': user}, 'users/user.html', request) def edit_info(request, userid): try: @@ -36,6 +34,8 @@ def edit_info(request, userid): user = InfoForm(request.POST or None, instance=user) if user.is_valid(): user.save() + messages.success(request, "L'user a bien été modifié") + return redirect("/users/") return form({'userform': user}, 'users/user.html', request) def state(request, userid): @@ -47,6 +47,8 @@ def state(request, userid): user = StateForm(request.POST or None, instance=user) if user.is_valid(): user.save() + messages.success(request, "Etat changé avec succès") + return redirect("/users/") return form({'userform': user}, 'users/user.html', request) def password(request, userid): @@ -63,12 +65,16 @@ def password(request, userid): user.pwd_ssha = makeSecret(user_form.cleaned_data['passwd1']) user.pwd_ntlm = hashNT(user_form.cleaned_data['passwd1']) user.save() + messages.success(request, "Le mot de passe a changé") + return redirect("/users/") return form({'userform': user_form}, 'users/user.html', request) def add_right(request): right = RightForm(request.POST or None) if right.is_valid(): right.save() + messages.success(request, "Droit ajouté") + return redirect("/users/") return form({'userform': right}, 'users/user.html', request) def index(request):