From 57f74f380a5738be689081fa0841376e8cc20145 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 21 Nov 2017 05:47:05 +0100 Subject: [PATCH] Fonction can_create sur un user --- .../0024_optionaluser_all_can_create.py | 20 +++++++++++++++++++ preferences/models.py | 4 ++++ .../preferences/display_preferences.html | 4 ++++ users/models.py | 11 ++++++++-- users/templates/users/sidebar.html | 11 +++++----- users/views.py | 7 ++++++- 6 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 preferences/migrations/0024_optionaluser_all_can_create.py diff --git a/preferences/migrations/0024_optionaluser_all_can_create.py b/preferences/migrations/0024_optionaluser_all_can_create.py new file mode 100644 index 00000000..3f7cf56e --- /dev/null +++ b/preferences/migrations/0024_optionaluser_all_can_create.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-11-21 04:42 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('preferences', '0023_auto_20171015_2033'), + ] + + operations = [ + migrations.AddField( + model_name='optionaluser', + name='all_can_create', + field=models.BooleanField(default=False, help_text="Tous les users peuvent en créer d'autres"), + ), + ] diff --git a/preferences/models.py b/preferences/models.py index dc1412e7..6a793a24 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -42,6 +42,10 @@ class OptionalUser(models.Model): default=0 ) gpg_fingerprint = models.BooleanField(default=True) + all_can_create = models.BooleanField( + default=False, + help_text="Tous les users peuvent en créer d'autres", + ) def clean(self): """Creation du mode de paiement par solde""" diff --git a/preferences/templates/preferences/display_preferences.html b/preferences/templates/preferences/display_preferences.html index 9a2abe08..4858e414 100644 --- a/preferences/templates/preferences/display_preferences.html +++ b/preferences/templates/preferences/display_preferences.html @@ -52,6 +52,10 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ useroptions.solde_negatif }} {% endif %} + + Creations d'users par tous + {{ useroptions.all_can_create }} +

Préférences machines

{% if is_bureau %} diff --git a/users/models.py b/users/models.py index 110531d3..ac767bd3 100644 --- a/users/models.py +++ b/users/models.py @@ -758,16 +758,23 @@ class User(AbstractBaseUser): num += 1 return composed_pseudo(num) + def can_create(user): + options, _created = OptionalUser.objects.get_or_create() + if options.all_can_create: + return True + else: + return user.has_perms(('cableur',)) + def can_edit(self, user): if self.is_class_club and user.is_class_adherent: - return self == user or user.has_perms(('cableur',))or\ + return self == user or user.has_perms(('cableur',)) or\ user.adherent in self.club.administrators.all() else: return self == user or user.has_perms(('cableur',)) def can_view(self, user): if self.is_class_club and user.is_class_adherent: - return self == user or user.has_perms(('cableur',))or\ + return self == user or user.has_perms(('cableur',)) or\ user.adherent in self.club.administrators.all() or\ user.adherent in self.club.members.all() else: diff --git a/users/templates/users/sidebar.html b/users/templates/users/sidebar.html index 7c5f05f5..d6b31acd 100644 --- a/users/templates/users/sidebar.html +++ b/users/templates/users/sidebar.html @@ -26,21 +26,20 @@ with this program; if not, write to the Free Software Foundation, Inc., {% block sidebar %} {% if is_cableur %} - - - Créer un adhérent - Créer un club/association {% endif %} - {% if is_cableur %} + + + Créer un adhérent + Clubs et assos - + {% if is_cableur %} Adherents diff --git a/users/views.py b/users/views.py index 6250db75..e0c71157 100644 --- a/users/views.py +++ b/users/views.py @@ -111,10 +111,15 @@ def password_change_action(u_form, user, request, req=False): @login_required -@permission_required('cableur') def new_user(request): """ Vue de création d'un nouvel utilisateur, envoie un mail pour le mot de passe""" + if not User.can_create(request.user): + messages.error(request, "Vous ne pouvez pas accéder à ce menu") + return redirect(reverse( + 'users:profil', + kwargs={'userid':str(request.user.id)} + )) user = AdherentForm(request.POST or None) if user.is_valid(): user = user.save(commit=False)