Skip to content

Commit

Permalink
Feat: Add support for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
sverben committed Jan 21, 2024
1 parent c5dca78 commit 862beee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions LedenAdministratie/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class EmailSendForm(forms.Form):
choices=VALID_RECIPIENTS, widget=forms.CheckboxSelectMultiple
)
subject = forms.CharField(max_length=255)
description = forms.CharField(max_length=255)
body = forms.CharField(widget=TinyMCE(mce_attrs={"cols": 80, "height": 500}))
attachment = forms.FileField(required=False)

Expand Down
1 change: 1 addition & 0 deletions LedenAdministratie/settings.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,4 @@ TINYMCE_DEFAULT_CONFIG = {
"alignright alignjustify | bullist numlist outdent indent | code table "
"removeformat link | help",
}
NOTIFICATION_ENDPOINT = None
19 changes: 19 additions & 0 deletions LedenAdministratie/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,29 +444,48 @@ def send_email(self, form, recipients):

return Utils.send_email(message)

def send_notification(self, form, recipients):
if len(recipients) == 0:
return None
if not settings.NOTIFICATION_ENDPOINT:
return None

requests.post(settings.NOTIFICATION_ENDPOINT, json={
"title": form.cleaned_data["subject"],
"description": form.cleaned_data["description"],
"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])

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"))

Expand Down
3 changes: 3 additions & 0 deletions templates/email_send.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

<label for="id_subject">Onderwerp</label>
{{ form.subject|add_class:'form-control' }}

<label for="id_description">Beschrijving</label>
{{ form.description|add_class:'form-control' }}
</div>

<div class="form-row col-10">
Expand Down

0 comments on commit 862beee

Please sign in to comment.