2017-09-26 22:18:39 +02:00
|
|
|
from django.contrib.auth.models import (
|
|
|
|
Group as DjangoGroup, Permission as DjangoPermission,
|
|
|
|
)
|
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
|
2017-09-26 22:18:39 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-09-13 01:57:31 +02:00
|
|
|
|
2017-10-16 00:07:30 +02:00
|
|
|
from kfet.cms.models import CmsPermissionManager
|
|
|
|
|
2017-09-13 01:57:31 +02:00
|
|
|
|
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-26 22:18:39 +02:00
|
|
|
|
2017-09-25 17:16:19 +02:00
|
|
|
objects = GenericTeamTokenManager()
|
2017-10-12 11:07:16 +02:00
|
|
|
|
2017-10-15 20:48:15 +02:00
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
2017-09-26 22:18:39 +02:00
|
|
|
|
|
|
|
class Group(DjangoGroup):
|
|
|
|
|
|
|
|
def give_admin_access(self):
|
|
|
|
perms = Permission.kfetcore.all()
|
|
|
|
self.permissions.add(*perms)
|
|
|
|
|
|
|
|
def give_staff_access(self):
|
|
|
|
perms = Permission.kfetcore.filter(
|
|
|
|
codename__in=['is_team', 'perform_deposit'])
|
|
|
|
self.permissions.add(*perms)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Groupe")
|
|
|
|
verbose_name_plural = _("Groupes")
|
2017-10-15 20:48:15 +02:00
|
|
|
default_permissions = ('view', 'add', 'change')
|
2017-09-26 22:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
KFET_CORE_APP_LABELS = ['kfet', 'kfetauth']
|
|
|
|
|
|
|
|
|
|
|
|
class CorePermissionManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(
|
|
|
|
content_type__app_label__in=KFET_CORE_APP_LABELS)
|
|
|
|
|
|
|
|
|
|
|
|
class Permission(DjangoPermission):
|
|
|
|
kfetcore = CorePermissionManager()
|
2017-10-16 00:07:30 +02:00
|
|
|
kfetcms = CmsPermissionManager()
|
2017-09-26 22:18:39 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
verbose_name = _("Permission")
|
|
|
|
verbose_name_plural = _("Permissions")
|