Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit a signal when a ticket is updated #1169

Merged
merged 16 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions helpdesk/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
from email.utils import getaddresses
from email_reply_parser import EmailReplyParser
from helpdesk import settings
from helpdesk import webhooks
from helpdesk.exceptions import DeleteIgnoredTicketException, IgnoreTicketException
from helpdesk.lib import process_attachments, safe_template_context
from helpdesk.models import FollowUp, IgnoreEmail, Queue, Ticket
from helpdesk.signals import update_ticket_done
import imaplib
import logging
import mimetypes
Expand Down Expand Up @@ -618,7 +618,8 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
else:
send_info_email(message_id, f, ticket, context, queue, new)
if not new:
webhooks.notify_followup_webhooks(f)
# emit signal with followup when the ticket is updated
update_ticket_done.send(sender="create_object_from_email_message", followup=f)
return ticket


Expand Down
4 changes: 4 additions & 0 deletions helpdesk/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import django.dispatch

# create a signal for ticket updates
update_ticket_done = django.dispatch.Signal()
27 changes: 9 additions & 18 deletions helpdesk/update_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
FollowUp,
Ticket,
TicketCC,
TicketChange,
)
from helpdesk.signals import update_ticket_done

User = get_user_model()

Expand Down Expand Up @@ -268,41 +268,33 @@ def update_ticket(
files = process_attachments(f, files) if files else []

if ticket_title and ticket_title != ticket.title:
c = TicketChange(
followup=f,
c = f.ticketchange_set.create(
field=_('Title'),
old_value=ticket.title,
new_value=ticket_title,
)
c.save()
ticket.title = ticket_title

if new_status != old_status:
c = TicketChange(
followup=f,
c = f.ticketchange_set.create(
field=_('Status'),
old_value=old_status_str,
new_value=ticket.get_status_display(),
)
c.save()

if ticket.assigned_to != old_owner:
c = TicketChange(
followup=f,
c = f.ticketchange_set.create(
field=_('Owner'),
old_value=old_owner,
new_value=ticket.assigned_to,
)
c.save()

if priority != ticket.priority:
c = TicketChange(
followup=f,
c = f.ticketchange_set.create(
field=_('Priority'),
old_value=ticket.priority,
new_value=priority,
)
c.save()
ticket.priority = priority

if queue != ticket.queue.id:
Expand All @@ -314,13 +306,11 @@ def update_ticket(
ticket.queue_id = queue

if due_date != ticket.due_date:
c = TicketChange(
followup=f,
c = f.ticketchange_set.create(
field=_('Due on'),
old_value=ticket.due_date,
new_value=due_date,
)
c.save()
ticket.due_date = due_date

for checklist in ticket.checklists.all():
Expand Down Expand Up @@ -397,8 +387,9 @@ def update_ticket(
))
ticket.save()

from helpdesk.webhooks import notify_followup_webhooks
notify_followup_webhooks(f)
# emit signal with followup when the ticket update is done
# internally used for webhooks
update_ticket_done.send(sender="update_ticket", followup=f)

# auto subscribe user if enabled
add_staff_subscription(user, ticket)
Expand Down
13 changes: 11 additions & 2 deletions helpdesk/webhooks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from . import settings

import requests
import requests.exceptions
import logging
from django.dispatch import receiver

from . import settings
from .signals import update_ticket_done

logger = logging.getLogger(__name__)

def notify_followup_webhooks(followup):
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
Expand All @@ -30,6 +33,12 @@ def notify_followup_webhooks(followup):
logger.error('Timeout while sending followup webhook to %s', url)


# listener is loaded via app.py HelpdeskConfig.ready()
@receiver(update_ticket_done)
def notify_followup_webhooks_receiver(sender, followup, **kwargs):
notify_followup_webhooks(followup)


def send_new_ticket_webhook(ticket):
urls = settings.HELPDESK_GET_NEW_TICKET_WEBHOOK_URLS()
if not urls:
Expand Down