gestioCOF/kfet/auth/models.py
Ludovic Stephan e92d50593c New models
2020-09-07 14:57:41 +02:00

45 lines
1.3 KiB
Python

from django.contrib.auth.models import Group, Permission
from django.db import models
from django.utils.crypto import get_random_string
from django.utils.translation import ugettext_lazy as _
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)
class GenericTeamToken(models.Model):
token = models.CharField(max_length=50, unique=True)
objects = GenericTeamTokenManager()
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`)
class Meta:
verbose_name = _("Groupe K-Fêt")
verbose_name_plural = _("Groupes K-Fêt")
class KFetPermissionManager(models.Manager):
def get_queryset(self):
return (
super()
.get_queryset()
.filter(content_type__app_label__in=["kfet", "kfetauth"])
)
class KFetPermission(Permission):
kfet = KFetPermissionManager()
class Meta:
proxy = True
verbose_name = _("Permission K-Fêt")
verbose_name_plural = _("Permissions K-Fêt")