jumpserver/apps/tickets/signals_handler.py

50 lines
1.6 KiB
Python
Raw Normal View History

2019-11-07 10:06:58 +00:00
# -*- coding: utf-8 -*-
#
from django.dispatch import receiver
2019-11-08 07:48:01 +00:00
from django.db.models.signals import m2m_changed, post_save, pre_save
2019-11-07 10:06:58 +00:00
from common.utils import get_logger
2019-11-15 10:55:35 +00:00
from .models import Ticket, Comment
2019-11-07 10:06:58 +00:00
from .utils import (
2019-11-15 10:55:35 +00:00
send_new_ticket_mail_to_assignees,
send_ticket_action_mail_to_user
2019-11-07 10:06:58 +00:00
)
logger = get_logger(__name__)
2019-11-15 10:55:35 +00:00
@receiver(m2m_changed, sender=Ticket.assignees.through)
2019-11-18 07:18:47 +00:00
def on_ticket_assignees_set(sender, instance=None, action=None,
reverse=False, model=None,
pk_set=None, **kwargs):
2019-11-07 10:06:58 +00:00
if action == 'post_add':
logger.debug('New ticket create, send mail: {}'.format(instance.id))
assignees = model.objects.filter(pk__in=pk_set)
2019-11-15 10:55:35 +00:00
send_new_ticket_mail_to_assignees(instance, assignees)
2019-11-08 07:48:01 +00:00
if action.startswith('post') and not reverse:
instance.assignees_display = ', '.join([
str(u) for u in instance.assignees.all()
])
instance.save()
2019-11-07 10:06:58 +00:00
2019-11-15 10:55:35 +00:00
@receiver(post_save, sender=Ticket)
2019-11-18 07:18:47 +00:00
def on_ticket_status_change(sender, instance=None, created=False, **kwargs):
2019-11-08 07:48:01 +00:00
if created or instance.status == "open":
2019-11-07 10:06:58 +00:00
return
logger.debug('Ticket changed, send mail: {}'.format(instance.id))
2019-11-15 10:55:35 +00:00
send_ticket_action_mail_to_user(instance)
2019-11-08 07:48:01 +00:00
2019-11-15 10:55:35 +00:00
@receiver(pre_save, sender=Ticket)
2019-11-08 07:48:01 +00:00
def on_ticket_create(sender, instance=None, **kwargs):
instance.user_display = str(instance.user)
if instance.assignee:
instance.assignee_display = str(instance.assignee)
@receiver(pre_save, sender=Comment)
def on_comment_create(sender, instance=None, **kwargs):
instance.user_display = str(instance.user)