2020-12-29 16:19:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.db import models
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-01-10 12:14:13 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
from django.db.utils import IntegrityError
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2022-01-10 12:14:13 +00:00
|
|
|
from common.exceptions import JMSException
|
|
|
|
from common.utils.timezone import as_current_tz
|
2020-12-29 16:19:59 +00:00
|
|
|
from common.mixins.models import CommonModelMixin
|
2021-08-25 11:02:50 +00:00
|
|
|
from common.db.encoder import ModelJSONFieldEncoder
|
2020-12-29 16:19:59 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
|
|
|
from orgs.utils import tmp_to_root_org, tmp_to_org
|
2021-08-25 11:02:50 +00:00
|
|
|
from tickets.const import TicketType, TicketStatus, TicketState, TicketApprovalLevel, ProcessStatus, TicketAction
|
2021-01-13 09:49:03 +00:00
|
|
|
from tickets.signals import post_change_ticket_action
|
|
|
|
from tickets.handler import get_ticket_handler
|
2021-08-25 11:02:50 +00:00
|
|
|
from tickets.errors import AlreadyClosed
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2021-12-29 12:07:56 +00:00
|
|
|
__all__ = ['Ticket', 'TicketStep', 'TicketAssignee']
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
|
2021-08-25 11:02:50 +00:00
|
|
|
class TicketStep(CommonModelMixin):
|
|
|
|
ticket = models.ForeignKey(
|
|
|
|
'Ticket', related_name='ticket_steps', on_delete=models.CASCADE, verbose_name='Ticket'
|
|
|
|
)
|
|
|
|
level = models.SmallIntegerField(
|
|
|
|
default=TicketApprovalLevel.one, choices=TicketApprovalLevel.choices,
|
|
|
|
verbose_name=_('Approve level')
|
|
|
|
)
|
|
|
|
state = models.CharField(choices=ProcessStatus.choices, max_length=64, default=ProcessStatus.notified)
|
|
|
|
|
|
|
|
|
|
|
|
class TicketAssignee(CommonModelMixin):
|
|
|
|
assignee = models.ForeignKey(
|
|
|
|
'users.User', related_name='ticket_assignees', on_delete=models.CASCADE, verbose_name='Assignee'
|
|
|
|
)
|
|
|
|
state = models.CharField(choices=ProcessStatus.choices, max_length=64, default=ProcessStatus.notified)
|
|
|
|
step = models.ForeignKey('tickets.TicketStep', related_name='ticket_assignees', on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Ticket assignee')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{0.assignee.name}({0.assignee.username})_{0.step}'.format(self)
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
|
2021-01-13 09:49:03 +00:00
|
|
|
class Ticket(CommonModelMixin, OrgModelMixin):
|
2020-12-29 16:19:59 +00:00
|
|
|
title = models.CharField(max_length=256, verbose_name=_("Title"))
|
|
|
|
type = models.CharField(
|
2021-08-25 11:02:50 +00:00
|
|
|
max_length=64, choices=TicketType.choices,
|
|
|
|
default=TicketType.general, verbose_name=_("Type")
|
2020-12-29 16:19:59 +00:00
|
|
|
)
|
2020-12-30 21:07:11 +00:00
|
|
|
meta = models.JSONField(encoder=ModelJSONFieldEncoder, default=dict, verbose_name=_("Meta"))
|
2021-08-25 11:02:50 +00:00
|
|
|
state = models.CharField(
|
|
|
|
max_length=16, choices=TicketState.choices,
|
|
|
|
default=TicketState.open, verbose_name=_("State")
|
2020-12-29 16:19:59 +00:00
|
|
|
)
|
|
|
|
status = models.CharField(
|
2021-08-25 11:02:50 +00:00
|
|
|
max_length=16, choices=TicketStatus.choices,
|
|
|
|
default=TicketStatus.open, verbose_name=_("Status")
|
|
|
|
)
|
|
|
|
approval_step = models.SmallIntegerField(
|
|
|
|
default=TicketApprovalLevel.one, choices=TicketApprovalLevel.choices,
|
|
|
|
verbose_name=_('Approval step')
|
2020-12-29 16:19:59 +00:00
|
|
|
)
|
|
|
|
# 申请人
|
|
|
|
applicant = models.ForeignKey(
|
|
|
|
'users.User', related_name='applied_tickets', on_delete=models.SET_NULL, null=True,
|
|
|
|
verbose_name=_("Applicant")
|
|
|
|
)
|
2021-08-25 11:02:50 +00:00
|
|
|
applicant_display = models.CharField(max_length=256, default='', verbose_name=_("Applicant display"))
|
|
|
|
process_map = models.JSONField(encoder=ModelJSONFieldEncoder, default=list, verbose_name=_("Process"))
|
2020-12-29 16:19:59 +00:00
|
|
|
# 评论
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
2021-08-25 11:02:50 +00:00
|
|
|
flow = models.ForeignKey(
|
|
|
|
'TicketFlow', related_name='tickets', on_delete=models.SET_NULL, null=True,
|
|
|
|
verbose_name=_("TicketFlow")
|
|
|
|
)
|
2022-01-10 12:14:13 +00:00
|
|
|
serial_num = models.CharField(max_length=256, unique=True, null=True, verbose_name=_('Serial number'))
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('-date_created',)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}({})'.format(self.title, self.applicant_display)
|
|
|
|
|
2020-12-30 10:14:06 +00:00
|
|
|
# type
|
|
|
|
@property
|
|
|
|
def type_apply_asset(self):
|
2021-08-25 11:02:50 +00:00
|
|
|
return self.type == TicketType.apply_asset.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2020-12-30 10:14:06 +00:00
|
|
|
@property
|
|
|
|
def type_apply_application(self):
|
2021-08-25 11:02:50 +00:00
|
|
|
return self.type == TicketType.apply_application.value
|
2020-12-30 10:14:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type_login_confirm(self):
|
2021-08-25 11:02:50 +00:00
|
|
|
return self.type == TicketType.login_confirm.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
# status
|
2021-08-25 11:02:50 +00:00
|
|
|
@property
|
|
|
|
def status_open(self):
|
|
|
|
return self.status == TicketStatus.open.value
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@property
|
|
|
|
def status_closed(self):
|
2021-08-25 11:02:50 +00:00
|
|
|
return self.status == TicketStatus.closed.value
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@property
|
2021-08-25 11:02:50 +00:00
|
|
|
def state_open(self):
|
|
|
|
return self.state == TicketState.open.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2021-08-25 11:02:50 +00:00
|
|
|
@property
|
|
|
|
def state_approve(self):
|
|
|
|
return self.state == TicketState.approved.value
|
2020-12-30 10:14:06 +00:00
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@property
|
2021-08-25 11:02:50 +00:00
|
|
|
def state_reject(self):
|
|
|
|
return self.state == TicketState.rejected.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-25 11:02:50 +00:00
|
|
|
def state_close(self):
|
|
|
|
return self.state == TicketState.closed.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-25 11:02:50 +00:00
|
|
|
def current_node(self):
|
|
|
|
return self.ticket_steps.filter(level=self.approval_step)
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-25 11:02:50 +00:00
|
|
|
def processor(self):
|
|
|
|
processor = self.current_node.first().ticket_assignees.exclude(state=ProcessStatus.notified).first()
|
|
|
|
return processor.assignee if processor else None
|
|
|
|
|
|
|
|
def set_state_approve(self):
|
|
|
|
self.state = TicketState.approved
|
|
|
|
|
|
|
|
def set_state_reject(self):
|
|
|
|
self.state = TicketState.rejected
|
|
|
|
|
|
|
|
def set_state_closed(self):
|
|
|
|
self.state = TicketState.closed
|
|
|
|
|
|
|
|
def set_status_closed(self):
|
|
|
|
self.status = TicketStatus.closed
|
|
|
|
|
|
|
|
def create_related_node(self):
|
2021-12-15 11:49:00 +00:00
|
|
|
org_id = self.flow.org_id
|
2021-08-25 11:02:50 +00:00
|
|
|
approval_rule = self.get_current_ticket_flow_approve()
|
|
|
|
ticket_step = TicketStep.objects.create(ticket=self, level=self.approval_step)
|
|
|
|
ticket_assignees = []
|
2021-12-15 11:49:00 +00:00
|
|
|
assignees = approval_rule.get_assignees(org_id=org_id)
|
2021-08-25 11:02:50 +00:00
|
|
|
for assignee in assignees:
|
|
|
|
ticket_assignees.append(TicketAssignee(step=ticket_step, assignee=assignee))
|
|
|
|
TicketAssignee.objects.bulk_create(ticket_assignees)
|
|
|
|
|
|
|
|
def create_process_map(self):
|
2021-12-15 11:49:00 +00:00
|
|
|
org_id = self.flow.org_id
|
2021-08-25 11:02:50 +00:00
|
|
|
approval_rules = self.flow.rules.order_by('level')
|
|
|
|
nodes = list()
|
|
|
|
for node in approval_rules:
|
2021-12-15 11:49:00 +00:00
|
|
|
assignees = node.get_assignees(org_id=org_id)
|
|
|
|
assignee_ids = [assignee.id for assignee in assignees]
|
|
|
|
assignees_display = [str(assignee) for assignee in assignees]
|
2021-08-25 11:02:50 +00:00
|
|
|
nodes.append(
|
|
|
|
{
|
|
|
|
'approval_level': node.level,
|
|
|
|
'state': ProcessStatus.notified,
|
2021-12-15 11:49:00 +00:00
|
|
|
'assignees': assignee_ids,
|
|
|
|
'assignees_display': assignees_display
|
2021-08-25 11:02:50 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
# TODO 兼容不存在流的工单
|
|
|
|
def create_process_map_and_node(self, assignees):
|
|
|
|
self.process_map = [{
|
|
|
|
'approval_level': 1,
|
|
|
|
'state': 'notified',
|
|
|
|
'assignees': [assignee.id for assignee in assignees],
|
|
|
|
'assignees_display': [str(assignee) for assignee in assignees]
|
|
|
|
}, ]
|
|
|
|
self.save()
|
|
|
|
ticket_step = TicketStep.objects.create(ticket=self, level=1)
|
|
|
|
ticket_assignees = []
|
|
|
|
for assignee in assignees:
|
|
|
|
ticket_assignees.append(TicketAssignee(step=ticket_step, assignee=assignee))
|
|
|
|
TicketAssignee.objects.bulk_create(ticket_assignees)
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2021-01-13 09:49:03 +00:00
|
|
|
# action changed
|
|
|
|
def open(self, applicant):
|
|
|
|
self.applicant = applicant
|
2021-08-25 11:02:50 +00:00
|
|
|
self._change_action(TicketAction.open)
|
|
|
|
|
|
|
|
def update_current_step_state_and_assignee(self, processor, state):
|
|
|
|
if self.status_closed:
|
|
|
|
raise AlreadyClosed
|
|
|
|
self.state = state
|
|
|
|
current_node = self.current_node
|
|
|
|
current_node.update(state=state)
|
|
|
|
current_node.first().ticket_assignees.filter(assignee=processor).update(state=state)
|
2020-12-30 10:14:06 +00:00
|
|
|
|
2021-01-13 09:49:03 +00:00
|
|
|
def approve(self, processor):
|
2021-08-25 11:02:50 +00:00
|
|
|
self.update_current_step_state_and_assignee(processor, TicketState.approved)
|
|
|
|
self._change_action(TicketAction.approve)
|
2020-12-30 10:14:06 +00:00
|
|
|
|
2021-01-13 09:49:03 +00:00
|
|
|
def reject(self, processor):
|
2021-08-25 11:02:50 +00:00
|
|
|
self.update_current_step_state_and_assignee(processor, TicketState.rejected)
|
|
|
|
self._change_action(TicketAction.reject)
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
def close(self, processor):
|
2021-08-25 11:02:50 +00:00
|
|
|
self.update_current_step_state_and_assignee(processor, TicketState.closed)
|
|
|
|
self._change_action(TicketAction.close)
|
2021-01-13 09:49:03 +00:00
|
|
|
|
|
|
|
def _change_action(self, action):
|
2020-12-29 16:19:59 +00:00
|
|
|
self.save()
|
2021-01-13 09:49:03 +00:00
|
|
|
post_change_ticket_action.send(sender=self.__class__, ticket=self, action=action)
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2021-09-17 07:40:44 +00:00
|
|
|
def has_current_assignee(self, assignee):
|
2021-08-25 11:02:50 +00:00
|
|
|
return self.ticket_steps.filter(ticket_assignees__assignee=assignee, level=self.approval_step).exists()
|
2020-12-30 10:14:06 +00:00
|
|
|
|
2021-09-17 07:40:44 +00:00
|
|
|
def has_all_assignee(self, assignee):
|
|
|
|
return self.ticket_steps.filter(ticket_assignees__assignee=assignee).exists()
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@classmethod
|
|
|
|
def get_user_related_tickets(cls, user):
|
2021-08-25 11:02:50 +00:00
|
|
|
queries = Q(applicant=user) | Q(ticket_steps__ticket_assignees__assignee=user)
|
2021-01-19 11:41:05 +00:00
|
|
|
tickets = cls.all().filter(queries).distinct()
|
|
|
|
return tickets
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2021-08-25 11:02:50 +00:00
|
|
|
def get_current_ticket_flow_approve(self):
|
|
|
|
return self.flow.rules.filter(level=self.approval_step).first()
|
|
|
|
|
2021-01-13 09:49:03 +00:00
|
|
|
@classmethod
|
|
|
|
def all(cls):
|
|
|
|
with tmp_to_root_org():
|
|
|
|
return Ticket.objects.all()
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2021-01-13 09:49:03 +00:00
|
|
|
""" 确保保存的org_id的是自身的值 """
|
2020-12-29 16:19:59 +00:00
|
|
|
with tmp_to_org(self.org_id):
|
|
|
|
return super().save(*args, **kwargs)
|
2021-01-13 09:49:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def handler(self):
|
|
|
|
return get_ticket_handler(ticket=self)
|
|
|
|
|
|
|
|
# body
|
|
|
|
@property
|
|
|
|
def body(self):
|
|
|
|
_body = self.handler.get_body()
|
|
|
|
return _body
|
2022-01-10 12:14:13 +00:00
|
|
|
|
|
|
|
def get_serial_num_date(self):
|
|
|
|
date_created = as_current_tz(self.date_created)
|
|
|
|
date = date_created.strftime('%Y%m%d')
|
|
|
|
return date
|
|
|
|
|
|
|
|
def get_last_serail_num(self):
|
|
|
|
date_created = as_current_tz(self.date_created)
|
|
|
|
date_prefix = date_created.strftime('%Y%m%d')
|
|
|
|
|
|
|
|
ticket = Ticket.objects.select_for_update().filter(
|
|
|
|
serial_num__startswith=date_prefix
|
|
|
|
).order_by('-date_created').first()
|
|
|
|
|
|
|
|
if ticket:
|
|
|
|
# 202212010001
|
|
|
|
num_str = ticket.serial_num[8:]
|
|
|
|
num = int(num_str)
|
|
|
|
return num
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_next_serail_num(self):
|
|
|
|
num = self.get_last_serail_num()
|
|
|
|
if num is None:
|
|
|
|
num = 0
|
|
|
|
return '%04d' % (num + 1)
|
|
|
|
|
|
|
|
def construct_serial_num(self):
|
|
|
|
date_prefix = self.get_serial_num_date()
|
|
|
|
num_suffix = self.get_next_serail_num()
|
|
|
|
return date_prefix + num_suffix
|
|
|
|
|
|
|
|
def update_serial_num_if_need(self):
|
|
|
|
if self.serial_num:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.serial_num = self.construct_serial_num()
|
|
|
|
self.save(update_fields=('serial_num',))
|
|
|
|
except IntegrityError as e:
|
|
|
|
if e.args[0] == 1062:
|
|
|
|
# 虽然做了 `select_for_update` 但是每天的第一条工单仍可能造成冲突
|
|
|
|
# 但概率小,这里只报错,用户重新提交即可
|
|
|
|
raise JMSException(detail=_('Please try again'), code='please_try_again')
|
|
|
|
|
|
|
|
raise e
|