mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-23 03:43:12 +00:00
can_create avec choix du model
This commit is contained in:
parent
e8db0f8cf2
commit
843d78521f
2 changed files with 15 additions and 8 deletions
|
@ -762,12 +762,12 @@ class User(AbstractBaseUser):
|
|||
num += 1
|
||||
return composed_pseudo(num)
|
||||
|
||||
def can_create(user, perms=('cableur',)):
|
||||
def can_create(user):
|
||||
options, _created = OptionalUser.objects.get_or_create()
|
||||
if options.all_can_create:
|
||||
return True
|
||||
else:
|
||||
return user.has_perms(perms)
|
||||
return user.has_perms(('cableur',))
|
||||
|
||||
def can_edit(self, user):
|
||||
if self.is_class_club and user.is_class_adherent:
|
||||
|
@ -911,6 +911,13 @@ class ServiceUser(AbstractBaseUser):
|
|||
def __str__(self):
|
||||
return self.pseudo
|
||||
|
||||
def can_create(user):
|
||||
options, _created = OptionalUser.objects.get_or_create()
|
||||
if options.all_can_create:
|
||||
return True
|
||||
else:
|
||||
return user.has_perms(('infra',))
|
||||
|
||||
|
||||
@receiver(post_save, sender=ServiceUser)
|
||||
def service_user_post_save(sender, **kwargs):
|
||||
|
|
|
@ -109,11 +109,11 @@ def password_change_action(u_form, user, request, req=False):
|
|||
kwargs={'userid':str(user.id)}
|
||||
))
|
||||
|
||||
def can_create(perms=('cableur',)):
|
||||
"""Décorateur qui vérifie si l'utilisateur peut créer un objet."""
|
||||
def can_create(model):
|
||||
"""Decorator to check if an user can create a model. """
|
||||
def decorator(view):
|
||||
def wrapper(request,*args, **kwargs):
|
||||
if not request.user.can_create(perms=perms):
|
||||
if not model.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)}
|
||||
|
@ -123,7 +123,7 @@ def can_create(perms=('cableur',)):
|
|||
return decorator
|
||||
|
||||
@login_required
|
||||
@can_create()
|
||||
@can_create(Adherent)
|
||||
def new_user(request):
|
||||
""" Vue de création d'un nouvel utilisateur,
|
||||
envoie un mail pour le mot de passe"""
|
||||
|
@ -145,7 +145,7 @@ def new_user(request):
|
|||
|
||||
|
||||
@login_required
|
||||
@can_create()
|
||||
@can_create(Club)
|
||||
def new_club(request):
|
||||
""" Vue de création d'un nouveau club,
|
||||
envoie un mail pour le mot de passe"""
|
||||
|
@ -310,7 +310,7 @@ def password(request, userid):
|
|||
|
||||
|
||||
@login_required
|
||||
@can_create(('infra',))
|
||||
@can_create(ServiceUser)
|
||||
def new_serviceuser(request):
|
||||
""" Vue de création d'un nouvel utilisateur service"""
|
||||
user = ServiceUserForm(request.POST or None)
|
||||
|
|
Loading…
Reference in a new issue