Make update_ticket_done signal trigger notify_followup_webhooks

This commit is contained in:
Sam Splunks 2024-04-16 08:23:06 +00:00
parent 2499f81d4d
commit ebb9a4d50d
3 changed files with 30 additions and 25 deletions

4
helpdesk/signals.py Normal file
View File

@ -0,0 +1,4 @@
import django.dispatch
# create a signal for ticket updates
update_ticket_done = django.dispatch.Signal()

View File

@ -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

View File

@ -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):