jumpserver/apps/tickets/models/ticket.py

142 lines
5.0 KiB
Python
Raw Normal View History

2019-11-07 10:06:58 +00:00
# -*- coding: utf-8 -*-
#
2019-10-25 03:27:50 +00:00
from django.db import models
2019-11-18 10:44:21 +00:00
from django.db.models import Q
2019-10-25 03:27:50 +00:00
from django.utils.translation import ugettext_lazy as _
from common.db.models import ChoiceSet
2019-10-25 03:27:50 +00:00
from common.mixins.models import CommonModelMixin
2019-11-15 10:55:35 +00:00
from common.fields.model import JsonDictTextField
2020-07-09 07:41:02 +00:00
from orgs.mixins.models import OrgModelMixin
2019-10-25 03:27:50 +00:00
2019-11-07 10:06:58 +00:00
__all__ = ['Ticket', 'Comment']
2020-07-09 07:41:02 +00:00
class Ticket(OrgModelMixin, CommonModelMixin):
class STATUS(ChoiceSet):
OPEN = 'open', _("Open")
CLOSED = 'closed', _("Closed")
class TYPE(ChoiceSet):
GENERAL = 'general', _("General")
LOGIN_CONFIRM = 'login_confirm', _("Login confirm")
REQUEST_ASSET_PERM = 'request_asset', _('Request asset permission')
class ACTION(ChoiceSet):
APPROVE = 'approve', _('Approve')
REJECT = 'reject', _('Reject')
user = models.ForeignKey('users.User', on_delete=models.SET_NULL, null=True, related_name='%(class)s_requested', verbose_name=_("User"))
2019-10-25 04:35:38 +00:00
user_display = models.CharField(max_length=128, verbose_name=_("User display name"))
2019-10-25 03:27:50 +00:00
title = models.CharField(max_length=256, verbose_name=_("Title"))
body = models.TextField(verbose_name=_("Body"))
2019-11-15 10:55:35 +00:00
meta = JsonDictTextField(verbose_name=_("Meta"), default='{}')
assignee = models.ForeignKey('users.User', on_delete=models.SET_NULL, null=True, related_name='%(class)s_handled', verbose_name=_("Assignee"))
assignee_display = models.CharField(max_length=128, blank=True, null=True, verbose_name=_("Assignee display name"), default='')
assignees = models.ManyToManyField('users.User', related_name='%(class)s_assigned', verbose_name=_("Assignees"))
2019-10-25 04:35:38 +00:00
assignees_display = models.CharField(max_length=128, verbose_name=_("Assignees display name"), blank=True)
type = models.CharField(max_length=16, choices=TYPE.choices, default=TYPE.GENERAL, verbose_name=_("Type"))
status = models.CharField(choices=STATUS.choices, max_length=16, default='open')
action = models.CharField(choices=ACTION.choices, max_length=16, default='', blank=True)
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
2019-10-25 11:20:28 +00:00
2020-07-09 07:41:02 +00:00
origin_objects = models.Manager()
2019-10-25 11:20:28 +00:00
def __str__(self):
return '{}: {}'.format(self.user_display, self.title)
2019-10-25 03:27:50 +00:00
2019-10-31 10:23:43 +00:00
@property
def body_as_html(self):
return self.body.replace('\n', '<br/>')
@property
def status_display(self):
return self.get_status_display()
2019-11-15 10:55:35 +00:00
@property
def type_display(self):
return self.get_type_display()
@property
def action_display(self):
return self.get_action_display()
2019-11-08 07:48:01 +00:00
def create_status_comment(self, status, user):
if status == self.STATUS.CLOSED:
2019-11-08 07:48:01 +00:00
action = _("Close")
else:
action = _("Open")
body = _('{} {} this ticket').format(self.user, action)
self.comments.create(user=user, body=body)
def perform_status(self, status, user, extra_comment=None):
self.create_comment(
self.STATUS.get(status),
user,
extra_comment
)
2019-11-08 07:48:01 +00:00
self.status = status
self.assignee = user
2019-11-08 07:48:01 +00:00
self.save()
def create_comment(self, action_display, user, extra_comment=None):
2019-11-18 03:54:12 +00:00
body = '{} {} {}'.format(user, action_display, _("this ticket"))
2020-07-09 07:41:02 +00:00
if extra_comment is not None:
body += extra_comment
2019-11-15 10:55:35 +00:00
self.comments.create(body=body, user=user, user_display=str(user))
2020-07-09 07:41:02 +00:00
def perform_action(self, action, user, extra_comment=None):
self.create_comment(
self.ACTION.get(action),
user,
extra_comment
)
2019-11-15 10:55:35 +00:00
self.action = action
self.status = self.STATUS.CLOSED
2019-11-15 10:55:35 +00:00
self.assignee = user
self.save()
2019-11-18 03:54:12 +00:00
def is_assignee(self, user):
return self.assignees.filter(id=user.id).exists()
def is_user(self, user):
return self.user == user
2019-11-18 10:44:21 +00:00
@classmethod
def get_related_tickets(cls, user, queryset=None):
if queryset is None:
queryset = cls.objects.all()
queryset = queryset.filter(
Q(assignees=user) | Q(user=user)
).distinct()
return queryset
@classmethod
def get_assigned_tickets(cls, user, queryset=None):
if queryset is None:
queryset = cls.objects.all()
queryset = queryset.filter(assignees=user)
return queryset
@classmethod
def get_my_tickets(cls, user, queryset=None):
if queryset is None:
queryset = cls.objects.all()
queryset = queryset.filter(user=user)
return queryset
2019-10-25 04:35:38 +00:00
class Meta:
ordering = ('-date_created',)
2019-10-25 04:35:38 +00:00
2019-11-07 10:06:58 +00:00
class Comment(CommonModelMixin):
2019-11-08 07:48:01 +00:00
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE, related_name='comments')
2019-11-07 10:06:58 +00:00
user = models.ForeignKey('users.User', on_delete=models.SET_NULL, null=True, verbose_name=_("User"), related_name='comments')
user_display = models.CharField(max_length=128, verbose_name=_("User display name"))
body = models.TextField(verbose_name=_("Body"))
class Meta:
ordering = ('date_created', )