jumpserver/apps/tickets/models/ticket.py

141 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.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):
2019-11-07 10:06:58 +00:00
STATUS_OPEN = 'open'
STATUS_CLOSED = 'closed'
2019-10-25 03:27:50 +00:00
STATUS_CHOICES = (
2019-11-07 10:06:58 +00:00
(STATUS_OPEN, _("Open")),
(STATUS_CLOSED, _("Closed"))
2019-10-25 03:27:50 +00:00
)
2019-11-07 10:06:58 +00:00
TYPE_GENERAL = 'general'
TYPE_LOGIN_CONFIRM = 'login_confirm'
2020-07-06 03:14:20 +00:00
TYPE_REQUEST_ASSET_PERM = 'request_asset'
2019-10-25 03:27:50 +00:00
TYPE_CHOICES = (
2019-11-07 10:06:58 +00:00
(TYPE_GENERAL, _("General")),
2020-07-06 03:14:20 +00:00
(TYPE_LOGIN_CONFIRM, _("Login confirm")),
(TYPE_REQUEST_ASSET_PERM, _('Request asset permission'))
2019-10-25 03:27:50 +00:00
)
2019-11-15 10:55:35 +00:00
ACTION_APPROVE = 'approve'
ACTION_REJECT = 'reject'
ACTION_CHOICES = (
(ACTION_APPROVE, _('Approve')),
(ACTION_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"))
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)
2019-11-08 07:48:01 +00:00
type = models.CharField(max_length=16, choices=TYPE_CHOICES, default=TYPE_GENERAL, verbose_name=_("Type"))
2019-11-07 10:06:58 +00:00
status = models.CharField(choices=STATUS_CHOICES, max_length=16, default='open')
2019-11-15 10:55:35 +00:00
action = models.CharField(choices=ACTION_CHOICES, max_length=16, default='', blank=True)
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:
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):
if self.status == status:
return
self.status = status
self.save()
2020-07-09 07:41:02 +00:00
def create_action_comment(self, action, user, extra_comment=None):
2019-11-15 10:55:35 +00:00
action_display = dict(self.ACTION_CHOICES).get(action)
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_action_comment(action, user, extra_comment)
2019-11-15 10:55:35 +00:00
self.action = action
self.status = self.STATUS_CLOSED
self.assignee = user
self.assignees_display = str(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', )