|
| 1 | +from django.dispatch import receiver |
| 2 | +from django.urls import reverse |
| 3 | +from django.db.models.signals import post_save |
| 4 | +from django.apps import apps |
| 5 | +from django.conf import settings |
| 6 | +from .models import Application |
| 7 | +import logging |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | +@receiver(post_save, sender=Application) |
| 11 | +def notify_discord_channel(sender, **kwargs): |
| 12 | + if 'created' not in kwargs or not kwargs.get('created'): |
| 13 | + return |
| 14 | + try: |
| 15 | + from django_discord_connector.request import DiscordRequest |
| 16 | + channel = apps.get_app_config( |
| 17 | + 'applications').APPLICATIONS_NOTIFICATION_CHANNEL |
| 18 | + application = kwargs.get('instance') |
| 19 | + |
| 20 | + application_url = reverse('application-detail', kwargs={'pk': application.pk}) |
| 21 | + url = f"{settings.SITE_PROTOCOL}{settings.SITE_DOMAIN}{application_url}" |
| 22 | + |
| 23 | + message = { |
| 24 | + 'title': f"New Application - {application.template.name}", |
| 25 | + 'fields': [ |
| 26 | + { |
| 27 | + 'name' : "Description", |
| 28 | + 'value': f"There is a new application by {application.request_user}" |
| 29 | + }, |
| 30 | + { |
| 31 | + 'name' : "View Application", |
| 32 | + 'value': f"[Click here to view the application]({url})" |
| 33 | + }, |
| 34 | + ], |
| 35 | + } |
| 36 | + |
| 37 | + response = DiscordRequest.get_instance().send_channel_message( |
| 38 | + channel, |
| 39 | + embed=message |
| 40 | + ) |
| 41 | + |
| 42 | + except Exception as e: |
| 43 | + logger.error(f"Failed to notify Discord channel of new application. {e}") |
0 commit comments