import jinja2 from channels.db import database_sync_to_async 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', 'event_slug', 'event_name', 'username', 'user_nick', 'web_host'] # TODO customer_name, tracking_code def limit_length(s, length=50): if len(s) > length: return s[:(length - 3)] + "..." return s def ticket_url(ticket): eventslug = ticket.event.slug if ticket.event else "37C3" # TODO 37C3 should not be hardcoded return f"https://{PRIMARY_HOST}/{eventslug}/ticket/{ticket.id}/" 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) except MessageTemplate.DoesNotExist: return None 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)) 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)) 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)