From b3877696ccaa92db183ff127a3537102ec37f8c6 Mon Sep 17 00:00:00 2001 From: Alan Zhu <2025azhu@tjhsst.edu> Date: Sat, 5 Oct 2024 02:15:34 -0400 Subject: [PATCH] feat: send club announcements emails --- intranet/apps/announcements/forms.py | 6 +++++- intranet/apps/announcements/notifications.py | 7 ++++++- intranet/apps/announcements/views.py | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/intranet/apps/announcements/forms.py b/intranet/apps/announcements/forms.py index 48a222e474..67460d82ec 100644 --- a/intranet/apps/announcements/forms.py +++ b/intranet/apps/announcements/forms.py @@ -44,16 +44,20 @@ def __init__(self, user, *args, **kwargs): else: self.fields["activity"].queryset = [] self.fields["activity"].required = True + self.fields[ + "notify_post" + ].help_text = "If this box is checked, students who have subscribed to your club's announcements will receive an email." if "instance" in kwargs: # Don't allow changing the activity once the announcement has been created self.fields["activity"].widget.attrs["disabled"] = True self.fields["activity"].initial = kwargs["instance"].activity expiration_date = forms.DateTimeInput() + notify_post = forms.BooleanField(required=False, initial=True) class Meta: model = Announcement - fields = ["activity", "title", "content", "expiration_date"] + fields = ["activity", "title", "content", "expiration_date", "notify_post"] help_texts = { "expiration_date": "By default, announcements expire after two weeks. Choose the shortest time necessary.", } diff --git a/intranet/apps/announcements/notifications.py b/intranet/apps/announcements/notifications.py index 4b7c66cf4d..6c25c7ac00 100644 --- a/intranet/apps/announcements/notifications.py +++ b/intranet/apps/announcements/notifications.py @@ -122,7 +122,12 @@ def announcement_posted_email(request, obj, send_all=False): subject = f"Club Announcement for {obj.activity.name}: {obj.title}" users = ( get_user_model() - .objects.filter(user_type="student", graduation_year__gte=get_senior_graduation_year(), subscribed_to_set__contains=obj.activity) + .objects.filter( + user_type="student", + graduation_year__gte=get_senior_graduation_year(), + receive_news_emails=True, + subscribed_activity_set__contains=obj.activity, + ) .union(get_user_model().objects.filter(user_type__in=["teacher", "counselor"], subscribed_to_set__contains=obj.activity)) ) diff --git a/intranet/apps/announcements/views.py b/intranet/apps/announcements/views.py index a2754e27d1..4412ac0299 100644 --- a/intranet/apps/announcements/views.py +++ b/intranet/apps/announcements/views.py @@ -157,9 +157,10 @@ def add_club_announcement_view(request): obj.user = request.user # SAFE HTML obj.content = safe_html(obj.content) - obj.save() + announcement_posted_hook(request, obj) + messages.success(request, "Successfully posted club announcement.") return redirect("club_announcements") else: