From 0303710d2609f1d06ff3d344a6768f61b77a73f2 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Fri, 9 Mar 2018 00:53:38 +0100 Subject: [PATCH] nouvelles vues pour users --- users/forms.py | 56 +++++ users/models.py | 4 + users/templates/users/school.html | 45 +--- users/urls.py | 80 +++---- users/views.py | 332 ++++++++++++++---------------- 5 files changed, 246 insertions(+), 271 deletions(-) create mode 100644 users/forms.py diff --git a/users/forms.py b/users/forms.py new file mode 100644 index 0000000..8d42501 --- /dev/null +++ b/users/forms.py @@ -0,0 +1,56 @@ +from django import forms +from django.contrib.auth.models import User + +from .models import School + + +class CreateSchool(forms.ModelForm): + class Meta: + model = User + fields = '__all__' + + +class CreateUser(forms.ModelForm): + class Meta: + model = User + fields = '__all__' + + +class EditPhone(forms.ModelForm): + class Meta: + model = School + fields = ['phone'] + + +class EditLogo(forms.ModelForm): + class Meta: + model = School + fields = ['logo'] + + +class EditMail(forms.ModelForm): + class Meta: + model = User + fields = ['email'] + + +class EditJury1(forms.ModelForm): + class Meta: + model = School + fields = [ + 'first_name_j1', + 'last_name_j1', + 'phone_j1', + 'mail_j1' + ] + + +class EditJury2(forms.ModelForm): + class Meta: + model = School + fields = [ + 'first_name_j2', + 'last_name_j2', + 'phone_j2', + 'mail_j2' + ] diff --git a/users/models.py b/users/models.py index 16f50dc..253225c 100644 --- a/users/models.py +++ b/users/models.py @@ -36,6 +36,10 @@ class School(models.Model): null=True, blank=False, ) + validated = models.BooleanField( + verbose_name="Inscription validé.", + default=False + ) first_name_j1 = models.CharField( max_length=255, verbose_name="Prénom juré n°1" diff --git a/users/templates/users/school.html b/users/templates/users/school.html index e76fd7f..1a3de49 100644 --- a/users/templates/users/school.html +++ b/users/templates/users/school.html @@ -2,50 +2,17 @@ {% load bootstrap4 %} {% block content %} -

{{object.name}}

+

{{school.name}}

{% if manager_right in perms %} Numéro de téléphone : -{% if object.school.phone %}{{object.school.phone}} - - - Éditer - +{% if school.phone %}{{school.phone}} {%else%} Non indiqué{%endif%}
{%endif%} -{% if manager_right in perms %} -

Membres

- - - - - - - - - {% for member in members %} - - - - - - - {% endfor %} - -
NomPrénomPseudoAdministrer
{{member.last_name}}{{member.first_name}}{{member.username}} - {% if member in manager_group.user_set.all %} - - - Enlever le privilège Administrateur - - {% else %} - - - Promouvoir administrateur - - {% endif %} -
-{% endif %} + + + Éditer +

Contenus

{% if manager_right in perms %} diff --git a/users/urls.py b/users/urls.py index 79a06a2..2fcf4f3 100644 --- a/users/urls.py +++ b/users/urls.py @@ -1,79 +1,45 @@ -from django.urls import path -from .views import ( - CreateUser, - CreateSchool, - EditSchoolName, - EditSchoolPhone, - DeleteSchool, - Login, - Logout, - PasswordChange, - Profile, - School, - promote_user, - degrade_user -) +from django.urls import path, include +from . import views app_name = 'users' urlpatterns = [ path( 'user/new', - CreateUser.as_view(), + views.create_user, name='new-user' ), path( - 'login', - Login.as_view(), - name='login' - ), - path( - 'logout', - Logout.as_view(), - name='logout', - ), - path( - 'change_password', - PasswordChange.as_view(), - name='change-password' - ), - path( - 'user/', - Profile.as_view(), - name='profile', + 'user/', + include('django.contrib.auth.urls') ), path( 'school/new', - CreateSchool.as_view(), + views.create_school, name='new-school' ), path( 'school/', - School.as_view(), - name='school' - ), - path( - 'school//degrade/', - degrade_user, - name='degrade-user' - ), - path( - 'school//promote/', - promote_user, - name='promote-user' - ), - path( - 'school//edit_name', - EditSchoolName.as_view(), - name='edit-school-name' - ), + views.school, + name='school', + ) path( 'school//edit_phone', - EditSchoolPhone.as_view(), + views.edit_phone, name='edit-school-phone' ), path( - 'school//delete', - DeleteSchool.as_view(), - name='delete-school' + 'school//edit_mail', + views.edit_mail, + name='edit-school-mail' + ), + path( + 'school//jury_1', + views.edit_jury_1, + name='edit-jury-1' + ), + path( + 'school//jury_2', + views.edit_jury_2, + name='edit-jury-2' ), ] diff --git a/users/views.py b/users/views.py index 145b406..ffe6d23 100644 --- a/users/views.py +++ b/users/views.py @@ -1,199 +1,181 @@ -from django.contrib.auth.models import User, Group -from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin -from django.views.generic import CreateView, UpdateView, DeleteView, DetailView -from django.contrib.auth.views import LoginView, LogoutView, PasswordChangeView, login_required -from django.contrib.auth.hashers import make_password -from django.contrib.messages.views import SuccessMessageMixin from django.contrib import messages -from django.urls import reverse, reverse_lazy -from django.shortcuts import get_object_or_404, redirect +from django.urls import reverse +from django.shortcuts import get_object_or_404, redirect, render +from django.contrib.admin.views.decorators import staff_member_required + +from content.models import Category from .models import School -from content.models import Content +from . import forms -class CreateUser(CreateView): - model = User - fields = [ - 'first_name', - 'last_name', - 'email', - 'username', - 'password', - ] - template_name = 'edit.html' - - def get_success_url(self): - return reverse( - 'users:create-userprofile', - kwargs={'pk': self.object.pk} - ) - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['title'] = "Inscription" - context['validate'] = "S'inscrire" - return context - - def form_valid(self, form): - r = super().form_valid(form) - self.object.set_password(form.cleaned_data['password']) - self.object.save() - return r - -class Profile(LoginRequiredMixin, UpdateView): - model = User - template_name = 'users/profile.html' - fields = [ - 'username', - 'first_name', - 'last_name', - 'email' - ] - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['title'] = self.object.username - context['validate'] = "Modifier" - return context - - def get_success_url(self): - return reverse( - 'users:profile', - kwargs={'pk': self.object.pk} - ) +@staff_member_required +def create_user(request): + user_form = forms.CreateUser(request.POST or None) + if user_form.is_valid(): + u = user_form.save() + messages.success(request, "L'utilisateur {} {} a bien été créé.".format( + u.first_name, u.last_name)) + return redirect(reverse('settings:index')) + return render(request, 'edit.html', { + 'form': user_form, + 'title': "Création d'un utilisateur", + 'validate': "Créer" + }) -class CreateSchool(LoginRequiredMixin, PermissionRequiredMixin, CreateView): - permission_required = 'users.add_schoolprofile' - model = School - fields = ['name'] - template_name = 'edit.html' - success_url = reverse_lazy('settings:index') - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['title'] = "Création de l'école" - context['validate'] = "Créer" - return context - - def form_valid(self, form): - response = super(CreateSchool, self).form_valid(form) - profile = School() - profile.group = form.instance - profile.save() - return response +@staff_member_required +def create_school(request): + school_form = forms.CreateSchool(request.POST or None) + if school_form.is_valid(): + s = school_form.save() + messages.success(request, "L'école {} a bien été créée.".format(s.name)) + return redirect(redirect('settings:index')) + return render(request, 'edit.html', { + 'form': school_form, + 'title': "Création d'une école", + 'validate': "Créer" + }) -class EditSchoolName(LoginRequiredMixin, PermissionRequiredMixin, UpdateView): - model = School - fields = ['name'] - template_name = 'edit.html' - - def get_success_url(self): - return reverse('users:edit-school-phone', kwargs={'pk':self.object.school.pk}) - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['title'] = "Édition de l'école" - context['validate'] = "Modifier" - return context - - def has_permission(self): - return self.request.user.has_perm('users.manage_'+str(self.kwargs['pk'])) - - def form_valid(self, *args, **kwargs): - r = super().form_valid(*args, **kwargs) - self.object.school.save() - return r +def school(request, pk): + school = get_object_or_404(School, pk=pk) + can = request.user.is_staff or request.user == school.admin + if not can: + messages.error(request, 'Vous ne pouvez pas accéder à cette page') + return redirect(reverse('home')) + return render(request, 'users/school.html', {'school':school}) -class EditSchoolPhone(LoginRequiredMixin, PermissionRequiredMixin, UpdateView): - model = School - fields = ['phone'] - template_name = 'edit.html' - - def get_success_url(self): - return reverse('users:school', kwargs={'pk':self.object.group.pk}) - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['title'] = "Édition de l'école" - context['validate'] = "Modifier" - return context - - def has_permission(self): - return self.request.user.has_perm('users.manage_'+str(self.kwargs['pk'])) +def edit_phone(request, pk): + school = get_object_or_404(School, pk=pk) + can = request.user.is_staff or request.user == school.admin + if not can: + messages.error(request, 'Vous ne pouvez pas accéder à cette page') + return redirect(reverse('settings:index')) + school_form = forms.EditPhone(request.POST or None, instance=school) + if school_form.is_valid(): + s = school_form.save() + messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) + return redirect(redirect('settings:index')) + return render(request, 'edit.html', { + 'form': school_form, + 'title': "Édition du numéro de téléphone", + 'validate': "Modifier" + }) -class DeleteSchool(LoginRequiredMixin, PermissionRequiredMixin, DeleteView): - model = Group - permission_required = 'users.delete_schoolprofile' +def edit_logo(request, pk): + school = get_object_or_404(School, pk=pk) + can = request.user.is_staff or request.user == school.admin + if not can: + messages.error(request, 'Vous ne pouvez pas accéder à cette page') + return redirect(reverse('settings:index')) + school_form = forms.EditLogo(request.POST or None, instance=school) + if school_form.is_valid(): + s = school_form.save() + messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) + return redirect(redirect('settings:index')) + return render(request, 'edit.html', { + 'form': school_form, + 'title': "Édition du logo", + 'validate': "Modifier" + }) -class School(LoginRequiredMixin, PermissionRequiredMixin, DetailView): - model = Group - template_name = "users/school.html" - - def get_context_data(self, **kwargs): - context = super().get_context_data() - context['contents'] = Content.objects.filter(school_owner=self.object.school) - context['school'] = True - context['members'] = User.objects.filter(userprofile__school=self.object.school) - context['manager_right'] = 'users.manage_' + str(self.object.pk) - context['manager_group'],_ = Group.objects.get_or_create(name=str(self.object.pk)+'_admins') - return context - - def has_permission(self): - return self.request.user.has_perm('users.view_'+str(self.kwargs['pk'])) +def edit_mail(request, pk): + school = get_object_or_404(School, pk=pk) + can = request.user.is_staff or request.user == school.admin + if not can: + messages.error(request, 'Vous ne pouvez pas accéder à cette page') + return redirect(reverse('settings:index')) + user_form = forms.EditMail(request.POST or None, instance=school.admin) + if user_form.is_valid(): + s = user_form.save() + messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) + return redirect(redirect('settings:index')) + return render(request, 'edit.html', { + 'form': user_form, + 'title': "Édition du numéro du mail", + 'validate': "Modifier" + }) -class Logout(SuccessMessageMixin, LogoutView): - success_message = "Vous vous êtes bien déconnecté." +def edit_jury_1(request, pk): + school = get_object_or_404(School, pk=pk) + can = request.user.is_staff or request.user == school.admin + if not can: + messages.error(request, 'Vous ne pouvez pas accéder à cette page') + return redirect(reverse('settings:index')) + school_form = forms.EditJury1(request.POST or None, instance=school) + if school_form.is_valid(): + s = school_form.save() + messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) + return redirect(redirect('settings:index')) + return render(request, 'edit.html', { + 'form': school_form, + 'title': "Édition du jury 1", + 'validate': "Modifier" + }) -class Login(SuccessMessageMixin, LoginView): - template_name = "edit.html" - success_message = "Bienvenue !" - extra_context = { - 'title' : "Connexion", - 'validate' : "Se connecter", - } +def edit_jury_2(request, pk): + school = get_object_or_404(School, pk=pk) + can = request.user.is_staff or request.user == school.admin + if not can: + messages.error(request, 'Vous ne pouvez pas accéder à cette page') + return redirect(reverse('settings:index')) + school_form = forms.EditJury2(request.POST or None, instance=school) + if school_form.is_valid(): + s = school_form.save() + messages.success(request, "L'école {} a bien été modifiée.".format(s.name)) + return redirect(redirect('settings:index')) + return render(request, 'edit.html', { + 'form': school_form, + 'title': "Édition du jury 2", + 'validate': "Modifier" + }) -class PasswordChange(SuccessMessageMixin, PasswordChangeView): - template_name = "edit.html" - success_url = reverse_lazy("home") - success_message = "Le mot de passe a été changé." - extra_context = { - 'title' : "Changer le mot de passe", - 'validate' : "Changer", - } +def validate(request, pk): + school = get_object_or_404(School, pk=pk) + can = request.user.is_staff or request.user == school.admin + if not can: + messages.error(request, 'Vous ne pouvez pas accéder à cette page') + return redirect(reverse('settings:index')) + jury_1_ok = any([ + school.first_name_j1 is not None, + school.last_name_j1 is not None, + school.phone_j1 is not None, + school.mail_j1 is not None + ]) + jury_2_ok = any([ + school.first_name_j2 is not None, + school.last_name_j2 is not None, + school.phone_j2 is not None, + school.mail_j2 is not None + ]) + logo_ok = school.logo is not None + phone_ok = school.phone is not None + mail_ok = school.admin.email is not None + if not jury_1_ok: + messages.warning(request, 'Pas de jury n°1 défini.') + if not jury_2_ok: + messages.warning(request, 'Pas de jury n°2 défini.') + if not logo_ok: + messages.warning(request, 'Pas de logo défini.') + if not phone_ok: + messages.error(request, 'Pas de téléphone défini.') + if not mail_ok: + messages.error(request, 'Pas de mail défini.') + for category in Category.objects.all(): + if not category.content_set.filter(school_owner=school): + messages.warning(request, 'Pas de contenu dans la catégorie {}.'.format(category.name)) -@login_required -def promote_user(request, school_pk, user_pk): - school = get_object_or_404(Group, pk=school_pk) - user = get_object_or_404(User, pk=user_pk) - if request.user.has_perm('manage_'+str(school.pk)): - admins,_ = Group.objects.get_or_create(name=str(school.pk)+'_admins') - user.groups.add(admins) - user.save() - messages.success(request, user.username + ' a été ajouté aux administrateurs de ' + school.name) - return redirect(reverse('users:school', kwargs={'pk':school.pk})) - messages.error(request, "Vous n'aves pas ce droit.") - return redirect('home') + if phone_ok and mail_ok: + school.validated = True + school.save() + messages.success(request, 'Inscription validée.') - -@login_required -def degrade_user(request, school_pk, user_pk): - school = get_object_or_404(Group, pk=school_pk) - user = get_object_or_404(User, pk=user_pk) - if request.user.has_perm('manage_'+str(school.pk)): - admins,_ = Group.objects.get_or_create(name=str(school.pk)+'_admins') - user.groups.remove(admins) - user.save() - messages.success(request, user.username + ' a été enlevé des administrateurs de ' + school.name) - return redirect(reverse('users:school', kwargs={'pk':school.pk})) - messages.error(request, "Vous n'aves pas ce droit.") - return redirect('home') + return redirect(reverse('settings:index'))