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 (
|
2020-12-29 16:19:59 +00:00
|
|
|
send_ticket_applied_mail_to_assignees,
|
|
|
|
send_ticket_processed_mail_to_applicant
|
2019-11-07 10:06:58 +00:00
|
|
|
)
|
2020-12-29 16:19:59 +00:00
|
|
|
from . import const
|
2019-11-07 10:06:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@receiver(pre_save, sender=Ticket)
|
|
|
|
def on_ticket_pre_save(sender, instance=None, **kwargs):
|
2020-12-30 10:14:06 +00:00
|
|
|
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)
|
2020-12-30 21:47:27 +00:00
|
|
|
def on_ticket_post_save(sender, instance=None, **kwargs):
|
2020-12-29 16:19:59 +00:00
|
|
|
instance.create_action_comment()
|
2020-12-30 21:47:27 +00:00
|
|
|
if instance.action_open:
|
|
|
|
instance.create_applied_comment()
|
|
|
|
return
|
2020-12-30 10:14:06 +00:00
|
|
|
if instance.action_approve:
|
2020-12-29 16:19:59 +00:00
|
|
|
instance.create_permission()
|
|
|
|
instance.create_approved_comment()
|
2020-12-30 21:47:27 +00:00
|
|
|
logger.debug(
|
|
|
|
'Ticket () has processed, send mail to applicant: {}'.format(
|
|
|
|
instance.title, instance.applicant_display
|
|
|
|
)
|
|
|
|
)
|
2020-12-29 16:19:59 +00:00
|
|
|
send_ticket_processed_mail_to_applicant(instance)
|
2019-11-08 07:48:01 +00:00
|
|
|
|
|
|
|
|
2020-12-29 16:19:59 +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
|
2020-12-30 10:14:06 +00:00
|
|
|
logger.debug('Receives ticket and assignees changed signal, ticket: {}'.format(ticket.title))
|
|
|
|
ticket.set_assignees_display()
|
2020-12-29 16:19:59 +00:00
|
|
|
ticket.save()
|
|
|
|
assignees = model.objects.filter(pk__in=pk_set)
|
2020-12-30 10:14:06 +00:00
|
|
|
assignees_display = [str(assignee) for assignee in assignees]
|
|
|
|
logger.debug('Send applied email to assignees: {}'.format(assignees_display))
|
2020-12-29 16:19:59 +00:00
|
|
|
send_ticket_applied_mail_to_assignees(ticket, assignees)
|
2019-11-08 07:48:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_save, sender=Comment)
|
2020-12-29 16:19:59 +00:00
|
|
|
def on_comment_create(sender, instance=None, created=False, **kwargs):
|
2020-12-30 10:14:06 +00:00
|
|
|
instance.set_display_fields()
|