From 4fa6c62d77efd84580a8cd150c6aed8fdcde4a00 Mon Sep 17 00:00:00 2001 From: Sverre <59171289+sverben@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:19:54 +0100 Subject: [PATCH] Feat: Add support for notifications (#54) --- LedenAdministratie/settings.py.example | 1 + LedenAdministratie/views.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/LedenAdministratie/settings.py.example b/LedenAdministratie/settings.py.example index eb0f307..d4cf8a0 100644 --- a/LedenAdministratie/settings.py.example +++ b/LedenAdministratie/settings.py.example @@ -191,3 +191,4 @@ TINYMCE_DEFAULT_CONFIG = { "alignright alignjustify | bullist numlist outdent indent | code table " "removeformat link | help", } +NOTIFICATION_ENDPOINT = None diff --git a/LedenAdministratie/views.py b/LedenAdministratie/views.py index 91683d1..7b63da5 100644 --- a/LedenAdministratie/views.py +++ b/LedenAdministratie/views.py @@ -444,6 +444,23 @@ def send_email(self, form, recipients): return Utils.send_email(message) + def send_notification(self, form, recipients): + if len(recipients) == 0: + return + if not settings.NOTIFICATION_ENDPOINT: + return + + requests.post( + settings.NOTIFICATION_ENDPOINT, + json={ + "title": "DJO Aankondigingen", + "description": form.cleaned_data["subject"], + "content": form.cleaned_data["body"], + "recipients": recipients, + }, + timeout=10, + ) + def form_valid(self, form): if "self" in form.cleaned_data["recipients"]: self.send_email(form, [self.request.user.email]) @@ -451,22 +468,27 @@ def form_valid(self, form): recipients = Member.objects.filter( Q(afmeld_datum__gt=date.today()) | Q(afmeld_datum=None) ) + to_user_list = [] for recipient in recipients: to_list = [] if recipient.is_begeleider() or recipient.is_aspirant(): if "begeleiders" in form.cleaned_data["recipients"]: to_list.append(recipient.email_address) + to_user_list.append(str(recipient.user.id)) elif recipient.is_ondersteuner(): if "ondersteuning" in form.cleaned_data["recipients"]: to_list.append(recipient.email_address) + to_user_list.append(str(recipient.user.id)) else: if "parents" in form.cleaned_data["recipients"]: for address in recipient.email_ouders.split(","): to_list.append(address) if "members" in form.cleaned_data["recipients"]: to_list.append(recipient.email_address) + to_user_list.append(str(recipient.user.id)) self.send_email(form, to_list) + self.send_notification(form, list(set(to_user_list))) return HttpResponseRedirect(reverse_lazy("email_log"))