jumpserver/apps/tickets/signals_handler.py

61 lines
1.9 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 (
send_ticket_applied_mail_to_assignees,
send_ticket_processed_mail_to_applicant
2019-11-07 10:06:58 +00:00
)
from . import const
2019-11-07 10:06:58 +00:00
logger = get_logger(__name__)
@receiver(pre_save, sender=Ticket)
def on_ticket_pre_save(sender, instance=None, **kwargs):
if instance.has_processed:
instance.set_status_closed()
instance.set_display_fields()
2019-11-07 10:06:58 +00:00
2019-11-15 10:55:35 +00:00
@receiver(post_save, sender=Ticket)
def on_ticket_post_save(sender, instance=None, **kwargs):
instance.create_action_comment()
if instance.action_open:
instance.create_applied_comment()
return
if instance.action_approve:
instance.create_permission()
instance.create_approved_comment()
logger.debug(
'Ticket () has processed, send mail to applicant: {}'.format(
instance.title, instance.applicant_display
)
)
send_ticket_processed_mail_to_applicant(instance)
2019-11-08 07:48:01 +00:00
@receiver(m2m_changed, sender=Ticket.assignees.through)
def on_ticket_assignees_changed(sender, instance=None, action=None, reverse=False, model=None, pk_set=None, **kwargs):
if reverse:
return
if action != 'post_add':
return
ticket = instance
logger.debug('Receives ticket and assignees changed signal, ticket: {}'.format(ticket.title))
ticket.set_assignees_display()
ticket.save()
assignees = model.objects.filter(pk__in=pk_set)
assignees_display = [str(assignee) for assignee in assignees]
logger.debug('Send applied email to assignees: {}'.format(assignees_display))
send_ticket_applied_mail_to_assignees(ticket, assignees)
2019-11-08 07:48:01 +00:00
@receiver(pre_save, sender=Comment)
def on_comment_create(sender, instance=None, created=False, **kwargs):
instance.set_display_fields()