2020-08-03 19:04:36 +02:00
|
|
|
from django.contrib.auth.models import Group, Permission
|
2017-09-13 01:57:31 +02:00
|
|
|
from django.db import models
|
2017-09-25 17:16:19 +02:00
|
|
|
from django.utils.crypto import get_random_string
|
2020-08-03 19:04:36 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-09-25 17:16:19 +02:00
|
|
|
|
2020-08-03 20:35:56 +02:00
|
|
|
KFET_APP_LABELS = ["kfet", "kfetauth"]
|
|
|
|
|
2017-09-25 17:16:19 +02:00
|
|
|
|
|
|
|
class GenericTeamTokenManager(models.Manager):
|
|
|
|
def create_token(self):
|
|
|
|
token = get_random_string(50)
|
|
|
|
while self.filter(token=token).exists():
|
|
|
|
token = get_random_string(50)
|
|
|
|
return self.create(token=token)
|
2017-09-13 01:57:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class GenericTeamToken(models.Model):
|
|
|
|
token = models.CharField(max_length=50, unique=True)
|
2017-09-25 17:16:19 +02:00
|
|
|
|
|
|
|
objects = GenericTeamTokenManager()
|
2020-08-03 19:04:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class KFetPermissionManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return (
|
2020-08-03 20:35:56 +02:00
|
|
|
super().get_queryset().filter(content_type__app_label__in=KFET_APP_LABELS)
|
2020-08-03 19:04:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class KFetPermission(Permission):
|
2020-08-03 20:35:56 +02:00
|
|
|
objects = KFetPermissionManager()
|
2020-08-03 19:04:36 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
verbose_name = _("Permission K-Fêt")
|
|
|
|
verbose_name_plural = _("Permissions K-Fêt")
|
2020-08-03 20:35:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class KFetGroup(Group):
|
|
|
|
# Même si on n'ajoute que des méthodes, on fait un héritage complet pour
|
|
|
|
# mieux distinguer les groupes K-Fêt via l'ORM (i.e. faire `KFetGroup.objects.all`)
|
|
|
|
|
|
|
|
def give_admin_access(self):
|
|
|
|
perms = KFetPermission.objects.all()
|
|
|
|
self.permissions.add(*perms)
|
|
|
|
|
|
|
|
def give_staff_access(self):
|
|
|
|
perms = KFetPermission.objects.filter(
|
|
|
|
codename__in=["is_team", "perform_deposit", "add_account", "add_transfer"]
|
|
|
|
)
|
|
|
|
self.permissions.add(*perms)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Groupe K-Fêt")
|
|
|
|
verbose_name_plural = _("Groupes K-Fêt")
|