Rationalise email notifications handling.

Add support for non-public submitter notification
This commit is contained in:
Christopher Broderick
2025-07-27 00:29:14 +01:00
parent c443d8f286
commit ac153518e3

View File

@@ -125,64 +125,84 @@ def get_and_set_ticket_status(
return old_status_str, old_status return old_status_str, old_status
def update_messages_sent_to_by_public_and_status( def process_email_notifications_for_ticket_update(
public: bool, public: bool,
ticket: Ticket, ticket: Ticket,
follow_up: FollowUp, follow_up: FollowUp,
context: str, context: dict,
messages_sent_to: typing.Set[str], messages_sent_to: typing.Set[str],
files: typing.List[typing.Tuple[str, str]], files: typing.List[typing.Tuple[str, str]],
) -> Ticket: reassigned: bool = False,
"""Sets the status of the ticket""" ):
"""
Sends email notifications when the ticket is updated in any way.
"""
template_prefix = get_email_template_prefix(reassigned, follow_up)
roles = {}
if public and ( if public and (
follow_up.comment follow_up.comment
or (follow_up.new_status in (Ticket.RESOLVED_STATUS, Ticket.CLOSED_STATUS)) or (follow_up.new_status in (Ticket.RESOLVED_STATUS, Ticket.CLOSED_STATUS))
): ):
if follow_up.new_status == Ticket.RESOLVED_STATUS: # For public tickets we do not want to send the assigned template for backwards compatibility
template = "resolved_" # TODO: possibly make the template prefix modification configurable
elif follow_up.new_status == Ticket.CLOSED_STATUS: pub_template_prefix = "updated_" if template_prefix == "assigned_" else template_prefix
template = "closed_" roles.update({
else: "submitter": (pub_template_prefix + "submitter", context),
template = "updated_" "ticket_cc": (pub_template_prefix + "cc", context),
})
roles = { else:
"submitter": (template + "submitter", context), if helpdesk_settings.HELPDESK_SEND_EMAIL_NOTIFICATION_FOR_INTERNAL_TICKET_UPDATES:
"ticket_cc": (template + "cc", context), roles.update({
} "submitter": (template_prefix + "submitter", context),
if ( })
ticket.assigned_to
and ticket.assigned_to.usersettings_helpdesk.email_on_ticket_change if roles:
):
roles["assigned_to"] = (template + "cc", context)
messages_sent_to.update( messages_sent_to.update(
ticket.send( ticket.send(
roles, dont_send_to=messages_sent_to, fail_silently=True, files=files roles,
dont_send_to=messages_sent_to,
fail_silently=True,
files=files,
) )
) )
return ticket if ticket.assigned_to and (
ticket.assigned_to.usersettings_helpdesk.email_on_ticket_change
or (
reassigned
and ticket.assigned_to.usersettings_helpdesk.email_on_ticket_assign
)
):
messages_sent_to.update(
ticket.send(
{"assigned_to": (template_prefix + "owner", context)},
dont_send_to=messages_sent_to,
fail_silently=True,
files=files,
)
)
messages_sent_to.update(
ticket.send(
{"ticket_cc": (template_prefix + "cc", context)},
dont_send_to=messages_sent_to,
fail_silently=True,
files=files,
)
)
def get_template_staff_and_template_cc(
def get_email_template_prefix(
reassigned, follow_up: FollowUp reassigned, follow_up: FollowUp
) -> typing.Tuple[str, str]: ) -> str:
if reassigned: if reassigned:
template_staff = "assigned_owner" return "assigned_"
elif follow_up.new_status == Ticket.RESOLVED_STATUS: elif follow_up.new_status == Ticket.RESOLVED_STATUS:
template_staff = "resolved_owner" return "resolved_"
elif follow_up.new_status == Ticket.CLOSED_STATUS: elif follow_up.new_status == Ticket.CLOSED_STATUS:
template_staff = "closed_owner" return "closed_"
else: else:
template_staff = "updated_owner" return "updated_"
if reassigned:
template_cc = "assigned_cc"
elif follow_up.new_status == Ticket.RESOLVED_STATUS:
template_cc = "resolved_cc"
elif follow_up.new_status == Ticket.CLOSED_STATUS:
template_cc = "closed_cc"
else:
template_cc = "updated_cc"
return template_staff, template_cc
def update_ticket( def update_ticket(
@@ -364,34 +384,8 @@ def update_ticket(
messages_sent_to.add(user.email) messages_sent_to.add(user.email)
except AttributeError: except AttributeError:
pass pass
ticket = update_messages_sent_to_by_public_and_status( process_email_notifications_for_ticket_update(
public, ticket, f, context, messages_sent_to, files public, ticket, f, context, messages_sent_to, files, reassigned=reassigned
)
template_staff, template_cc = get_template_staff_and_template_cc(reassigned, f)
if ticket.assigned_to and (
ticket.assigned_to.usersettings_helpdesk.email_on_ticket_change
or (
reassigned
and ticket.assigned_to.usersettings_helpdesk.email_on_ticket_assign
)
):
messages_sent_to.update(
ticket.send(
{"assigned_to": (template_staff, context)},
dont_send_to=messages_sent_to,
fail_silently=True,
files=files,
)
)
messages_sent_to.update(
ticket.send(
{"ticket_cc": (template_cc, context)},
dont_send_to=messages_sent_to,
fail_silently=True,
files=files,
)
) )
ticket.save() ticket.save()