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.contrib.auth import get_user_model
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
import django.dispatch
from helpdesk.lib import safe_template_context from helpdesk.lib import safe_template_context
from helpdesk import settings as helpdesk_settings from helpdesk import settings as helpdesk_settings
@ -18,11 +17,10 @@ from helpdesk.models import (
TicketCC, TicketCC,
TicketChange, TicketChange,
) )
from helpdesk.signals import update_ticket_done
User = get_user_model() User = get_user_model()
update_ticket_done = django.dispatch.Signal()
def add_staff_subscription( def add_staff_subscription(
user: User, user: User,
ticket: Ticket ticket: Ticket
@ -391,11 +389,9 @@ def update_ticket(
ticket.save() ticket.save()
# emit signal with followup when the ticket update is done # emit signal with followup when the ticket update is done
# internally used for webhooks
update_ticket_done.send(sender="update_ticket", followup=f) 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 # auto subscribe user if enabled
add_staff_subscription(user, ticket) add_staff_subscription(user, ticket)
return f return f

View File

@ -1,33 +1,38 @@
from . import settings
import requests import requests
import requests.exceptions import requests.exceptions
import logging import logging
from . import settings
from .signals import update_ticket_done
logger = logging.getLogger(__name__) 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() urls = settings.HELPDESK_GET_FOLLOWUP_WEBHOOK_URLS()
if not urls: if not urls:
return 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 if sender == "update_ticket":
data = { # Serialize the ticket associated with the followup
'ticket': serialized_ticket, from .serializers import TicketSerializer
'queue_slug': ticket.queue.slug, ticket = followup.ticket
'followup_id': followup.id ticket.set_custom_field_values()
} serialized_ticket = TicketSerializer(ticket).data
for url in urls: # Prepare the data to send
try: data = {
requests.post(url, json=data, timeout=settings.HELPDESK_WEBHOOK_TIMEOUT) 'ticket': serialized_ticket,
except requests.exceptions.Timeout: 'queue_slug': ticket.queue.slug,
logger.error('Timeout while sending followup webhook to %s', url) '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): def send_new_ticket_webhook(ticket):