kpsul/kfet/auth/models.py

46 lines
1.3 KiB
Python
Raw Normal View History

2020-08-03 19:04:36 +02:00
from django.contrib.auth.models import Group, Permission
from django.db import models
from django.utils.crypto import get_random_string
2020-08-03 19:04:36 +02:00
from django.utils.translation import ugettext_lazy as _
2020-08-03 20:35:56 +02:00
KFET_APP_LABELS = ["kfet", "kfetauth"]
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()
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):
# On fait un héritage complet pour
2020-08-03 20:35:56 +02:00
# 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")