2020-12-29 16:19:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import json
|
|
|
|
import uuid
|
|
|
|
from datetime import datetime
|
|
|
|
from django.db import models
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from common.mixins.models import CommonModelMixin
|
|
|
|
from orgs.mixins.models import OrgModelMixin
|
|
|
|
from orgs.utils import tmp_to_root_org, tmp_to_org
|
2021-01-13 09:49:03 +00:00
|
|
|
from tickets.const import TicketTypeChoices, TicketActionChoices, TicketStatusChoices
|
|
|
|
from tickets.signals import post_change_ticket_action
|
|
|
|
from tickets.handler import get_ticket_handler
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2020-12-30 21:07:11 +00:00
|
|
|
__all__ = ['Ticket', 'ModelJSONFieldEncoder']
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ModelJSONFieldEncoder(json.JSONEncoder):
|
|
|
|
""" 解决一些类型的字段不能序列化的问题 """
|
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, datetime):
|
|
|
|
return obj.strftime(settings.DATETIME_DISPLAY_FORMAT)
|
|
|
|
if isinstance(obj, uuid.UUID):
|
|
|
|
return str(obj)
|
2020-12-30 10:14:06 +00:00
|
|
|
if isinstance(obj, type(_("ugettext_lazy"))):
|
|
|
|
return str(obj)
|
2020-12-29 16:19:59 +00:00
|
|
|
else:
|
|
|
|
return super().default(obj)
|
|
|
|
|
|
|
|
|
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-01-13 09:49:03 +00:00
|
|
|
max_length=64, choices=TicketTypeChoices.choices,
|
|
|
|
default=TicketTypeChoices.general.value, 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"))
|
2020-12-29 16:19:59 +00:00
|
|
|
action = models.CharField(
|
2021-01-13 09:49:03 +00:00
|
|
|
choices=TicketActionChoices.choices, max_length=16,
|
|
|
|
default=TicketActionChoices.open.value, verbose_name=_("Action")
|
2020-12-29 16:19:59 +00:00
|
|
|
)
|
|
|
|
status = models.CharField(
|
2021-01-13 09:49:03 +00:00
|
|
|
max_length=16, choices=TicketStatusChoices.choices,
|
|
|
|
default=TicketStatusChoices.open.value, verbose_name=_("Status")
|
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")
|
|
|
|
)
|
|
|
|
applicant_display = models.CharField(
|
2021-01-13 09:49:03 +00:00
|
|
|
max_length=256, default='', verbose_name=_("Applicant display")
|
2020-12-29 16:19:59 +00:00
|
|
|
)
|
|
|
|
# 处理人
|
|
|
|
processor = models.ForeignKey(
|
|
|
|
'users.User', related_name='processed_tickets', on_delete=models.SET_NULL, null=True,
|
|
|
|
verbose_name=_("Processor")
|
|
|
|
)
|
|
|
|
processor_display = models.CharField(
|
2021-01-13 09:49:03 +00:00
|
|
|
max_length=256, blank=True, null=True, default='', verbose_name=_("Processor display")
|
2020-12-29 16:19:59 +00:00
|
|
|
)
|
|
|
|
# 受理人列表
|
|
|
|
assignees = models.ManyToManyField(
|
|
|
|
'users.User', related_name='assigned_tickets', verbose_name=_("Assignees")
|
|
|
|
)
|
2021-01-01 23:25:23 +00:00
|
|
|
assignees_display = models.JSONField(
|
|
|
|
encoder=ModelJSONFieldEncoder, default=list, verbose_name=_('Assignees display')
|
2020-12-29 16:19:59 +00:00
|
|
|
)
|
|
|
|
# 评论
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
|
|
|
|
|
|
|
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-01-13 09:49:03 +00:00
|
|
|
return self.type == TicketTypeChoices.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-01-13 09:49:03 +00:00
|
|
|
return self.type == TicketTypeChoices.apply_application.value
|
2020-12-30 10:14:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type_login_confirm(self):
|
2021-01-13 09:49:03 +00:00
|
|
|
return self.type == TicketTypeChoices.login_confirm.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
# status
|
|
|
|
@property
|
|
|
|
def status_closed(self):
|
2021-01-13 09:49:03 +00:00
|
|
|
return self.status == TicketStatusChoices.closed.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def status_open(self):
|
2021-01-13 09:49:03 +00:00
|
|
|
return self.status == TicketStatusChoices.open.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2020-12-30 10:14:06 +00:00
|
|
|
def set_status_closed(self):
|
2021-01-13 09:49:03 +00:00
|
|
|
self.status = TicketStatusChoices.closed.value
|
2020-12-30 10:14:06 +00:00
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
# action
|
|
|
|
@property
|
2020-12-30 10:14:06 +00:00
|
|
|
def action_open(self):
|
2021-01-13 09:49:03 +00:00
|
|
|
return self.action == TicketActionChoices.open.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@property
|
2020-12-30 10:14:06 +00:00
|
|
|
def action_approve(self):
|
2021-01-13 09:49:03 +00:00
|
|
|
return self.action == TicketActionChoices.approve.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@property
|
2020-12-30 10:14:06 +00:00
|
|
|
def action_reject(self):
|
2021-01-13 09:49:03 +00:00
|
|
|
return self.action == TicketActionChoices.reject.value
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@property
|
2020-12-30 10:14:06 +00:00
|
|
|
def action_close(self):
|
2021-01-13 09:49:03 +00:00
|
|
|
return self.action == TicketActionChoices.close.value
|
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
|
|
|
|
self._change_action(action=TicketActionChoices.open.value)
|
2020-12-30 10:14:06 +00:00
|
|
|
|
2021-01-13 09:49:03 +00:00
|
|
|
def approve(self, processor):
|
|
|
|
self.processor = processor
|
|
|
|
self._change_action(action=TicketActionChoices.approve.value)
|
2020-12-30 10:14:06 +00:00
|
|
|
|
2021-01-13 09:49:03 +00:00
|
|
|
def reject(self, processor):
|
|
|
|
self.processor = processor
|
|
|
|
self._change_action(action=TicketActionChoices.reject.value)
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
def close(self, processor):
|
|
|
|
self.processor = processor
|
2021-01-13 09:49:03 +00:00
|
|
|
self._change_action(action=TicketActionChoices.close.value)
|
|
|
|
|
|
|
|
def _change_action(self, action):
|
|
|
|
self.action = 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-01-01 23:47:43 +00:00
|
|
|
# ticket
|
2020-12-30 10:14:06 +00:00
|
|
|
def has_assignee(self, assignee):
|
|
|
|
return self.assignees.filter(id=assignee.id).exists()
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@classmethod
|
|
|
|
def get_user_related_tickets(cls, user):
|
2021-01-19 11:41:05 +00:00
|
|
|
queries = Q(applicant=user) | Q(assignees=user)
|
|
|
|
tickets = cls.all().filter(queries).distinct()
|
|
|
|
return tickets
|
2020-12-29 16:19:59 +00:00
|
|
|
|
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
|