From 9b8c75eaff1d898bf488355c6855bf1e17850a1c Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 4 May 2024 03:26:32 +0200 Subject: [PATCH] stash --- core/notifications/defauls.py | 2 +- core/notifications/dispatch.py | 4 ++-- core/notifications/templates.py | 40 +++++++++++++++++++-------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/core/notifications/defauls.py b/core/notifications/defauls.py index 7af6eb3..812d93e 100644 --- a/core/notifications/defauls.py +++ b/core/notifications/defauls.py @@ -12,5 +12,5 @@ Your c3lf (Cloakroom + Lost&Found) Team''' new_issue_notification = '''New issue "{{ ticket_name | limit_length }}" [{{ ticket_uuid }}] created {{ ticket_url }}''' -reply_issue_notification = '''Reply to issue "{{ ticket_name }}" [{{ ticket_uuid }}] +reply_issue_notification = '''Reply to issue "{{ ticket_name }}" [{{ ticket_uuid }}] (was {{ previous_state_pretty }}) {{ ticket_url }}''' diff --git a/core/notifications/dispatch.py b/core/notifications/dispatch.py index fc8900e..752c342 100644 --- a/core/notifications/dispatch.py +++ b/core/notifications/dispatch.py @@ -68,11 +68,11 @@ class NotificationDispatcher: async def dispatch(self, ticket, event_id, new): message = await render_notification_new_ticket_async( ticket) if new else await render_notification_reply_ticket_async(ticket) - title = f"{ticket.name} [#{ticket.short_uuid()}]" + title = f"[#{ticket.short_uuid()}] {ticket.name}" print("Dispatching message:", message, "with event_id:", event_id) targets = await self.get_notification_targets() jobs = [] - # jobs.append(telegram_notify(message, TELEGRAM_GROUP_CHAT_ID)) + jobs.append(telegram_notify(message, TELEGRAM_GROUP_CHAT_ID)) for target in targets: if target.channel_type == 'telegram': print("Sending telegram notification to:", target.channel_target) diff --git a/core/notifications/templates.py b/core/notifications/templates.py index e658c2e..e8e66b3 100644 --- a/core/notifications/templates.py +++ b/core/notifications/templates.py @@ -4,10 +4,8 @@ from core.settings import PRIMARY_HOST from notifications.models import MessageTemplate -# auto_reply_title = f"Re: {{ ticket_name }} [#{{ ticket_uuid }}]" - - TEMLATE_VARS = ['ticket_name', 'ticket_uuid', 'ticket_id', 'ticket_url', + 'current_state', 'previous_state', 'current_state_pretty', 'previous_state_pretty', 'event_slug', 'event_name', 'username', 'user_nick', 'web_host'] # TODO customer_name, tracking_code @@ -26,10 +24,8 @@ def ticket_url(ticket): def render_template(template, **kwargs): try: - environment = jinja2.Environment() environment.filters['limit_length'] = limit_length - tmpl = MessageTemplate.objects.get(name=template) template = environment.from_string(tmpl.message) return template.render(**kwargs, web_host=PRIMARY_HOST) @@ -37,27 +33,37 @@ def render_template(template, **kwargs): return None +def get_ticket_vars(ticket): + states = list(ticket.state_changes.order_by('-timestamp')) + return { + 'ticket_name': ticket.name, + 'ticket_uuid': ticket.short_uuid(), + 'ticket_id': ticket.id, + 'ticket_url': ticket_url(ticket), + 'current_state': states[0].state if states else 'none', + 'previous_state': states[1].state if len(states) > 1 else 'none', + 'current_state_pretty': states[0].get_state_display() if states else 'none', + 'previous_state_pretty': states[1].get_state_display() if len(states) > 1 else 'none', + 'event_slug': ticket.event.slug if ticket.event else "37C3", # TODO 37C3 should not be hardcoded + 'event_name': ticket.event.name if ticket.event else "37C3", + } + + def render_auto_reply(ticket): - eventslug = ticket.event.slug if ticket.event else "37C3" # TODO 37C3 should not be hardcoded - return render_template('auto_reply', ticket_name=ticket.name, ticket_uuid=ticket.short_uuid(), - ticket_id=ticket.id, event_slug=eventslug, ticket_url=ticket_url(ticket)) + return render_template('auto_reply', **get_ticket_vars(ticket)) def render_notification_new_ticket(ticket): - eventslug = ticket.event.slug if ticket.event else "37C3" # TODO 37C3 should not be hardcoded - return render_template('new_issue_notification', ticket_name=ticket.name, ticket_uuid=ticket.short_uuid(), - ticket_id=ticket.id, event_slug=eventslug, ticket_url=ticket_url(ticket)) + return render_template('new_issue_notification', **get_ticket_vars(ticket)) + + +def render_notification_reply_ticket(ticket): + return render_template('reply_issue_notification', **get_ticket_vars(ticket)) async def render_notification_new_ticket_async(ticket): return await database_sync_to_async(render_notification_new_ticket)(ticket) -def render_notification_reply_ticket(ticket): - eventslug = ticket.event.slug if ticket.event else "37C3" # TODO 37C3 should not be hardcoded - return render_template('reply_issue_notification', ticket_name=ticket.name, ticket_uuid=ticket.short_uuid(), - ticket_id=ticket.id, event_slug=eventslug, ticket_url=ticket_url(ticket)) - - async def render_notification_reply_ticket_async(ticket): return await database_sync_to_async(render_notification_reply_ticket)(ticket)