2019-07-03 14:28:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2022-12-07 09:24:30 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2019-07-03 14:28:20 +00:00
|
|
|
from django.db import models
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-07-03 14:28:20 +00:00
|
|
|
|
2022-08-11 07:45:03 +00:00
|
|
|
from common.db.models import JMSBaseModel
|
2022-12-07 09:24:30 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
|
|
|
from ..models import Organization
|
2019-07-03 14:28:20 +00:00
|
|
|
from ..utils import (
|
2021-03-03 03:20:40 +00:00
|
|
|
set_current_org, get_current_org, current_org, filter_org_queryset
|
2019-07-03 14:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
__all__ = [
|
2022-08-11 07:45:03 +00:00
|
|
|
'OrgManager', 'OrgModelMixin', 'JMSOrgBaseModel'
|
2019-07-03 14:28:20 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class OrgManager(models.Manager):
|
2020-03-12 08:24:38 +00:00
|
|
|
def all_group_by_org(self):
|
|
|
|
from ..models import Organization
|
|
|
|
orgs = list(Organization.objects.all())
|
2022-11-01 03:52:51 +00:00
|
|
|
org_queryset = {}
|
2020-03-12 08:24:38 +00:00
|
|
|
for org in orgs:
|
2021-03-02 06:57:48 +00:00
|
|
|
org_id = org.id
|
|
|
|
queryset = super(OrgManager, self).get_queryset().filter(org_id=org_id)
|
2022-11-01 03:52:51 +00:00
|
|
|
org_queryset[org] = queryset
|
|
|
|
return org_queryset
|
2019-07-03 14:28:20 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super(OrgManager, self).get_queryset()
|
2019-10-16 05:31:42 +00:00
|
|
|
return filter_org_queryset(queryset)
|
2019-07-03 14:28:20 +00:00
|
|
|
|
|
|
|
def set_current_org(self, org):
|
|
|
|
if isinstance(org, str):
|
|
|
|
org = Organization.get_instance(org)
|
|
|
|
set_current_org(org)
|
|
|
|
return self
|
|
|
|
|
2022-07-13 02:43:20 +00:00
|
|
|
def bulk_create(self, objs, batch_size=None, ignore_conflicts=False):
|
|
|
|
org = get_current_org()
|
|
|
|
for obj in objs:
|
2022-07-13 03:00:12 +00:00
|
|
|
if org.is_root():
|
2022-08-15 08:02:39 +00:00
|
|
|
if not obj.org_id:
|
2022-11-01 03:52:51 +00:00
|
|
|
raise ValidationError('Please save in a org')
|
2022-07-13 03:00:12 +00:00
|
|
|
else:
|
|
|
|
obj.org_id = org.id
|
2022-07-13 02:43:20 +00:00
|
|
|
return super().bulk_create(objs, batch_size, ignore_conflicts)
|
|
|
|
|
|
|
|
|
2019-07-03 14:28:20 +00:00
|
|
|
class OrgModelMixin(models.Model):
|
2022-07-13 03:00:12 +00:00
|
|
|
org_id = models.CharField(
|
2022-11-01 03:52:51 +00:00
|
|
|
max_length=36, blank=True, default='',
|
|
|
|
verbose_name=_("Organization"), db_index=True
|
2022-07-13 03:00:12 +00:00
|
|
|
)
|
2019-07-03 14:28:20 +00:00
|
|
|
objects = OrgManager()
|
|
|
|
sep = '@'
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2022-11-01 08:18:46 +00:00
|
|
|
locking_org = getattr(self, 'LOCKING_ORG', None)
|
2022-11-01 03:52:51 +00:00
|
|
|
if locking_org:
|
|
|
|
org = Organization.get_instance(locking_org)
|
|
|
|
else:
|
|
|
|
org = get_current_org()
|
2021-08-04 03:00:11 +00:00
|
|
|
# 这里不可以优化成, 因为 root 组织下可以设置组织 id 来保存
|
|
|
|
# if org.is_root() and not self.org_id:
|
|
|
|
# raise ...
|
2021-03-02 06:57:48 +00:00
|
|
|
if org.is_root():
|
|
|
|
if not self.org_id:
|
2022-11-01 03:52:51 +00:00
|
|
|
raise ValidationError('Please save in a org')
|
2021-03-02 06:57:48 +00:00
|
|
|
else:
|
2019-08-28 03:43:55 +00:00
|
|
|
self.org_id = org.id
|
2019-07-03 14:28:20 +00:00
|
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
2022-12-07 09:24:30 +00:00
|
|
|
@lazyproperty
|
2019-07-03 14:28:20 +00:00
|
|
|
def org(self):
|
2021-03-03 03:20:40 +00:00
|
|
|
return Organization.get_instance(self.org_id)
|
2019-07-03 14:28:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def org_name(self):
|
2021-03-03 03:20:40 +00:00
|
|
|
return self.org.name
|
2019-07-03 14:28:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def fullname(self, attr=None):
|
|
|
|
name = ''
|
|
|
|
if attr and hasattr(self, attr):
|
|
|
|
name = getattr(self, attr)
|
|
|
|
elif hasattr(self, 'name'):
|
|
|
|
name = self.name
|
2021-03-02 06:57:48 +00:00
|
|
|
return name + self.sep + self.org_name
|
2019-07-03 14:28:20 +00:00
|
|
|
|
|
|
|
def validate_unique(self, exclude=None):
|
|
|
|
"""
|
|
|
|
Check unique constraints on the model and raise ValidationError if any
|
|
|
|
failed.
|
|
|
|
Form 提交时会使用这个检验
|
|
|
|
"""
|
2021-03-02 06:57:48 +00:00
|
|
|
self.org_id = current_org.id
|
2019-07-03 14:28:20 +00:00
|
|
|
if exclude and 'org_id' in exclude:
|
|
|
|
exclude.remove('org_id')
|
|
|
|
unique_checks, date_checks = self._get_unique_checks(exclude=exclude)
|
|
|
|
|
|
|
|
errors = self._perform_unique_checks(unique_checks)
|
|
|
|
date_errors = self._perform_date_checks(date_checks)
|
|
|
|
|
|
|
|
for k, v in date_errors.items():
|
|
|
|
errors.setdefault(k, []).extend(v)
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2022-08-11 07:45:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JMSOrgBaseModel(JMSBaseModel, OrgModelMixin):
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|