2021-08-25 11:02:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2021-12-15 11:49:00 +00:00
|
|
|
from users.models import User
|
2021-08-25 11:02:50 +00:00
|
|
|
from common.mixins.models import CommonModelMixin
|
2022-06-23 05:52:28 +00:00
|
|
|
|
2021-08-25 11:02:50 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2022-02-10 05:14:17 +00:00
|
|
|
from orgs.models import Organization
|
|
|
|
from orgs.utils import tmp_to_org, get_current_org_id
|
2022-06-23 05:52:28 +00:00
|
|
|
from ..const import TicketType, TicketLevel, TicketApprovalStrategy
|
2021-08-25 11:02:50 +00:00
|
|
|
|
|
|
|
__all__ = ['TicketFlow', 'ApprovalRule']
|
|
|
|
|
|
|
|
|
|
|
|
class ApprovalRule(CommonModelMixin):
|
|
|
|
level = models.SmallIntegerField(
|
2022-06-23 05:52:28 +00:00
|
|
|
default=TicketLevel.one, choices=TicketLevel.choices,
|
2021-08-25 11:02:50 +00:00
|
|
|
verbose_name=_('Approve level')
|
|
|
|
)
|
|
|
|
strategy = models.CharField(
|
2021-08-26 10:29:02 +00:00
|
|
|
max_length=64, default=TicketApprovalStrategy.super_admin,
|
2021-08-25 11:02:50 +00:00
|
|
|
choices=TicketApprovalStrategy.choices,
|
|
|
|
verbose_name=_('Approve strategy')
|
|
|
|
)
|
|
|
|
# 受理人列表
|
|
|
|
assignees = models.ManyToManyField(
|
|
|
|
'users.User', related_name='assigned_ticket_flow_approval_rule',
|
|
|
|
verbose_name=_("Assignees")
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Ticket flow approval rule')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}({})'.format(self.id, self.level)
|
|
|
|
|
2021-12-15 11:49:00 +00:00
|
|
|
def get_assignees(self, org_id=None):
|
|
|
|
assignees = []
|
|
|
|
org_id = org_id if org_id else get_current_org_id()
|
|
|
|
with tmp_to_org(org_id):
|
|
|
|
if self.strategy == TicketApprovalStrategy.super_admin:
|
|
|
|
assignees = User.get_super_admins()
|
|
|
|
elif self.strategy == TicketApprovalStrategy.org_admin:
|
|
|
|
assignees = User.get_org_admins()
|
|
|
|
elif self.strategy == TicketApprovalStrategy.super_org_admin:
|
|
|
|
assignees = User.get_super_and_org_admins()
|
|
|
|
elif self.strategy == TicketApprovalStrategy.custom_user:
|
|
|
|
assignees = self.assignees.all()
|
|
|
|
return assignees
|
2021-08-25 11:02:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TicketFlow(CommonModelMixin, OrgModelMixin):
|
|
|
|
type = models.CharField(
|
|
|
|
max_length=64, choices=TicketType.choices,
|
|
|
|
default=TicketType.general, verbose_name=_("Type")
|
|
|
|
)
|
|
|
|
approval_level = models.SmallIntegerField(
|
2022-06-23 05:52:28 +00:00
|
|
|
default=TicketLevel.one,
|
|
|
|
choices=TicketLevel.choices,
|
2021-09-06 09:18:07 +00:00
|
|
|
verbose_name=_('Approve level')
|
2021-08-25 11:02:50 +00:00
|
|
|
)
|
|
|
|
rules = models.ManyToManyField(ApprovalRule, related_name='ticket_flows')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Ticket flow')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}'.format(self.type)
|
|
|
|
|
|
|
|
@classmethod
|
2021-12-11 04:50:30 +00:00
|
|
|
def get_org_related_flows(cls, org_id=None):
|
|
|
|
if org_id:
|
|
|
|
with tmp_to_org(org_id):
|
|
|
|
flows = cls.objects.all()
|
|
|
|
else:
|
|
|
|
flows = cls.objects.all()
|
2021-08-25 11:02:50 +00:00
|
|
|
cur_flow_types = flows.values_list('type', flat=True)
|
2022-02-10 05:14:17 +00:00
|
|
|
root_id = Organization.ROOT_ID
|
|
|
|
with tmp_to_org(root_id):
|
|
|
|
diff_global_flows = cls.objects.exclude(type__in=cur_flow_types).filter(org_id=root_id)
|
2021-08-25 11:02:50 +00:00
|
|
|
return flows | diff_global_flows
|