diff --git a/helpdesk/signals.py b/helpdesk/signals.py new file mode 100644 index 00000000..0f3ed823 --- /dev/null +++ b/helpdesk/signals.py @@ -0,0 +1,4 @@ +import django.dispatch + +# create a signal for ticket updates +update_ticket_done = django.dispatch.Signal() \ No newline at end of file diff --git a/helpdesk/update_ticket.py b/helpdesk/update_ticket.py index ddd337ee..017b5b4f 100644 --- a/helpdesk/update_ticket.py +++ b/helpdesk/update_ticket.py @@ -4,7 +4,6 @@ from django.core.exceptions import ValidationError from django.contrib.auth import get_user_model from django.utils import timezone from django.utils.translation import gettext as _ -import django.dispatch from helpdesk.lib import safe_template_context from helpdesk import settings as helpdesk_settings @@ -18,11 +17,10 @@ from helpdesk.models import ( TicketCC, TicketChange, ) +from helpdesk.signals import update_ticket_done User = get_user_model() -update_ticket_done = django.dispatch.Signal() - def add_staff_subscription( user: User, ticket: Ticket @@ -391,11 +389,9 @@ def update_ticket( ticket.save() # emit signal with followup when the ticket update is done + # internally used for webhooks update_ticket_done.send(sender="update_ticket", followup=f) - from helpdesk.webhooks import notify_followup_webhooks - notify_followup_webhooks(f) - # auto subscribe user if enabled add_staff_subscription(user, ticket) return f diff --git a/helpdesk/webhooks.py b/helpdesk/webhooks.py index 3133a3fa..39ab2359 100644 --- a/helpdesk/webhooks.py +++ b/helpdesk/webhooks.py @@ -1,33 +1,38 @@ -from . import settings - import requests import requests.exceptions import logging +from . import settings +from .signals import update_ticket_done + logger = logging.getLogger(__name__) -def notify_followup_webhooks(followup): +# listener is loaded via app.py HelpdeskConfig.ready() +@receiver(update_ticket_done) +def notify_followup_webhooks(sender, followup, **kwargs): urls = settings.HELPDESK_GET_FOLLOWUP_WEBHOOK_URLS() if not urls: return - # Serialize the ticket associated with the followup - from .serializers import TicketSerializer - ticket = followup.ticket - ticket.set_custom_field_values() - serialized_ticket = TicketSerializer(ticket).data - # Prepare the data to send - data = { - 'ticket': serialized_ticket, - 'queue_slug': ticket.queue.slug, - 'followup_id': followup.id - } + if sender == "update_ticket": + # Serialize the ticket associated with the followup + from .serializers import TicketSerializer + ticket = followup.ticket + ticket.set_custom_field_values() + serialized_ticket = TicketSerializer(ticket).data - for url in urls: - try: - requests.post(url, json=data, timeout=settings.HELPDESK_WEBHOOK_TIMEOUT) - except requests.exceptions.Timeout: - logger.error('Timeout while sending followup webhook to %s', url) + # Prepare the data to send + data = { + 'ticket': serialized_ticket, + 'queue_slug': ticket.queue.slug, + 'followup_id': followup.id + } + + for url in urls: + try: + requests.post(url, json=data, timeout=settings.HELPDESK_WEBHOOK_TIMEOUT) + except requests.exceptions.Timeout: + logger.error('Timeout while sending followup webhook to %s', url) def send_new_ticket_webhook(ticket):