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,15 +1,20 @@
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
if sender == "update_ticket":
# Serialize the ticket associated with the followup # Serialize the ticket associated with the followup
from .serializers import TicketSerializer from .serializers import TicketSerializer
ticket = followup.ticket ticket = followup.ticket