Skip to content

Commit

Permalink
feat: send club announcements emails
Browse files Browse the repository at this point in the history
  • Loading branch information
alanzhu0 committed Oct 5, 2024
1 parent df32926 commit f64049b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
6 changes: 5 additions & 1 deletion intranet/apps/announcements/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
}
Expand Down
14 changes: 11 additions & 3 deletions intranet/apps/announcements/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,13 @@ 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)
.union(get_user_model().objects.filter(user_type__in=["teacher", "counselor"], 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=obj.activity,
)
.union(get_user_model().objects.filter(user_type__in=["teacher", "counselor"], subscribed_activity_set=obj.activity))
)

else:
Expand Down Expand Up @@ -152,7 +157,10 @@ def announcement_posted_email(request, obj, send_all=False):
email_send_task.delay(
"announcements/emails/announcement_posted.txt", "announcements/emails/announcement_posted.html", data, subject, emails, bcc=True
)
messages.success(request, f"Sent email to {len(users_send)} users")
if request.user.is_announcements_admin:
messages.success(request, f"Sent email to {len(users_send)} users")
else:
messages.success(request, "Sent notification emails.")
else:
logger.info("Emailing announcements disabled")

Expand Down
3 changes: 2 additions & 1 deletion intranet/apps/announcements/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{ announcement.content }}
{% endautoescape %}

<p>Posted by {{ announcement.get_author }} on {{ announcement.added|date:"l, F j, Y"}} at {{ announcement.added|date:"P"}} to {% if announcement.groups.count == 0 %}everyone{% else %}{{ announcement.groups.all|join:", " }}{% endif %}
<p>Posted by {{ announcement.get_author }} on {{ announcement.added|date:"l, F j, Y"}} at {{ announcement.added|date:"P"}} to {% if announcement.is_club_announcement %}{{ announcement.activity.name }}{% else %}{% if announcement.groups.count == 0 %}everyone{% else %}{{ announcement.groups.all|join:", " }}{% endif %}{% endif %}
</p>

<p><a href="{{ info_link }}">View this announcement on Intranet</a></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{ announcement.content }}
{% endautoescape %}

Posted by {{ announcement.get_author }} on {{ announcement.added|date:"l, F j, Y"}} at {{ announcement.added|date:"P"}} to {% if announcement.groups.count == 0 %}everyone{% else %}{{ announcement.groups.all|join:", " }}{% endif %}
Posted by {{ announcement.get_author }} on {{ announcement.added|date:"l, F j, Y"}} at {{ announcement.added|date:"P"}} to {% if announcement.is_club_announcement %}{{ announcement.activity.name }}{% else %}{% if announcement.groups.count == 0 %}everyone{% else %}{{ announcement.groups.all|join:", " }}{% endif %}{% endif %}

View this announcement on Intranet: {{ info_link }}

Expand Down

0 comments on commit f64049b

Please sign in to comment.