2019-05-21 08:27:01 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.db import models
|
2019-08-28 03:43:55 +00:00
|
|
|
from django.db.models import Q
|
2019-05-21 08:27:01 +00:00
|
|
|
from django.utils import timezone
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2019-05-21 08:27:01 +00:00
|
|
|
|
2021-02-25 06:45:21 +00:00
|
|
|
from common.db.models import UnionQuerySet
|
2020-05-09 06:51:19 +00:00
|
|
|
from common.utils import date_expired_default, lazyproperty
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.models import OrgManager
|
2019-05-21 08:27:01 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2020-09-27 08:02:44 +00:00
|
|
|
'BasePermission', 'BasePermissionQuerySet'
|
2019-05-21 08:27:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class BasePermissionQuerySet(models.QuerySet):
|
|
|
|
def active(self):
|
|
|
|
return self.filter(is_active=True)
|
|
|
|
|
|
|
|
def valid(self):
|
|
|
|
return self.active().filter(date_start__lt=timezone.now()) \
|
|
|
|
.filter(date_expired__gt=timezone.now())
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
def inactive(self):
|
|
|
|
return self.filter(is_active=False)
|
|
|
|
|
|
|
|
def invalid(self):
|
2019-10-15 04:41:29 +00:00
|
|
|
now = timezone.now()
|
2021-08-31 06:13:05 +00:00
|
|
|
q = (Q(is_active=False) | Q(date_start__gt=now) | Q(date_expired__lt=now))
|
2019-08-28 03:43:55 +00:00
|
|
|
return self.filter(q)
|
|
|
|
|
2019-05-21 08:27:01 +00:00
|
|
|
|
|
|
|
class BasePermissionManager(OrgManager):
|
|
|
|
def valid(self):
|
|
|
|
return self.get_queryset().valid()
|
|
|
|
|
|
|
|
|
2019-05-21 09:01:47 +00:00
|
|
|
class BasePermission(OrgModelMixin):
|
2019-05-21 08:27:01 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2020-09-27 08:02:44 +00:00
|
|
|
users = models.ManyToManyField('users.User', blank=True, verbose_name=_("User"), related_name='%(class)ss')
|
2021-08-31 06:13:05 +00:00
|
|
|
user_groups = models.ManyToManyField(
|
|
|
|
'users.UserGroup', blank=True, verbose_name=_("User group"), related_name='%(class)ss')
|
2019-05-21 08:27:01 +00:00
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
|
|
|
|
date_start = models.DateTimeField(default=timezone.now, db_index=True, verbose_name=_("Date start"))
|
|
|
|
date_expired = models.DateTimeField(default=date_expired_default, db_index=True, verbose_name=_('Date expired'))
|
|
|
|
created_by = models.CharField(max_length=128, blank=True, verbose_name=_('Created by'))
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
|
|
|
|
comment = models.TextField(verbose_name=_('Comment'), blank=True)
|
2021-09-06 02:46:12 +00:00
|
|
|
from_ticket = models.BooleanField(default=False, verbose_name=_('From ticket'))
|
2019-05-21 08:27:01 +00:00
|
|
|
|
|
|
|
objects = BasePermissionManager.from_queryset(BasePermissionQuerySet)()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id_str(self):
|
|
|
|
return str(self.id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_expired(self):
|
|
|
|
if self.date_expired > timezone.now() > self.date_start:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_valid(self):
|
|
|
|
if not self.is_expired and self.is_active:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-05-28 09:00:42 +00:00
|
|
|
@property
|
|
|
|
def all_users(self):
|
|
|
|
from users.models import User
|
|
|
|
|
|
|
|
users_query = self._meta.get_field('users').related_query_name()
|
|
|
|
user_groups_query = self._meta.get_field('user_groups').related_query_name()
|
|
|
|
|
|
|
|
users_q = Q(**{
|
|
|
|
f'{users_query}': self
|
|
|
|
})
|
|
|
|
|
|
|
|
user_groups_q = Q(**{
|
|
|
|
f'groups__{user_groups_query}': self
|
|
|
|
})
|
|
|
|
|
|
|
|
return User.objects.filter(users_q | user_groups_q).distinct()
|
|
|
|
|
2019-05-21 08:27:01 +00:00
|
|
|
def get_all_users(self):
|
2019-12-05 07:09:25 +00:00
|
|
|
from users.models import User
|
2021-03-08 02:08:51 +00:00
|
|
|
user_ids = self.users.all().values_list('id', flat=True)
|
|
|
|
group_ids = self.user_groups.all().values_list('id', flat=True)
|
2021-02-25 06:45:21 +00:00
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
user_ids = list(user_ids)
|
|
|
|
group_ids = list(group_ids)
|
2021-02-25 06:45:21 +00:00
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
qs1 = User.objects.filter(id__in=user_ids).distinct()
|
|
|
|
qs2 = User.objects.filter(groups__id__in=group_ids).distinct()
|
2021-02-25 06:45:21 +00:00
|
|
|
|
|
|
|
qs = UnionQuerySet(qs1, qs2)
|
|
|
|
return qs
|
2020-05-09 06:51:19 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def users_amount(self):
|
|
|
|
return self.users.count()
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user_groups_amount(self):
|
|
|
|
return self.user_groups.count()
|