From 67adbd277c1af6cf2e65223097f7c49e5998482c Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 4 May 2022 23:02:39 +0000 Subject: [PATCH] Send an email to people interested in a course --- apiserver/apiserver/api/emails/interest.html | 19 ++++++++++++++ apiserver/apiserver/api/emails/interest.txt | 11 +++++++++ apiserver/apiserver/api/utils_email.py | 26 ++++++++++++++++++++ apiserver/apiserver/api/views.py | 14 ++++++++++- 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 apiserver/apiserver/api/emails/interest.html create mode 100644 apiserver/apiserver/api/emails/interest.txt diff --git a/apiserver/apiserver/api/emails/interest.html b/apiserver/apiserver/api/emails/interest.html new file mode 100644 index 0000000..63190e3 --- /dev/null +++ b/apiserver/apiserver/api/emails/interest.html @@ -0,0 +1,19 @@ + + + + + + + +
Hi [name],
+

+
There's been a class scheduled for [course] that you expressed interest in.
+

+
You can find the class on its course page here:
+
[link]
+

+
Your "interest" in this course is now removed and you won't receive any more notifications about its classes until you press the "Interest +" button again.
+

+
Spaceport
+ + diff --git a/apiserver/apiserver/api/emails/interest.txt b/apiserver/apiserver/api/emails/interest.txt new file mode 100644 index 0000000..ea54041 --- /dev/null +++ b/apiserver/apiserver/api/emails/interest.txt @@ -0,0 +1,11 @@ +Hi [name], + +There's been a class scheduled for [course] that you expressed interest in. + +You can find the class on its course page here: +[link] + +Your "interest" in this course is now removed and you won't receive any more +notifications about its classes until you press the "Interest +" button again. + +Spaceport diff --git a/apiserver/apiserver/api/utils_email.py b/apiserver/apiserver/api/utils_email.py index a8f4819..549b85d 100644 --- a/apiserver/apiserver/api/utils_email.py +++ b/apiserver/apiserver/api/utils_email.py @@ -67,3 +67,29 @@ def send_ical_email(member, session, ical_file): msg.send() logger.info('Sent ical email:\n' + email_text) + +def send_interest_email(interest): + def replace_fields(text): + return text.replace( + '[name]', interest.user.member.first_name, + ).replace( + '[course]', interest.course.name, + ).replace( + '[link]', 'https://my.protospace.ca/courses/' + str(interest.course.id), + ) + + with open(EMAIL_DIR + 'interest.txt', 'r') as f: + email_text = replace_fields(f.read()) + + with open(EMAIL_DIR + 'interest.html', 'r') as f: + email_html = replace_fields(f.read()) + + send_mail( + subject='Protospace class scheduled', + message=email_text, + from_email=None, # defaults to DEFAULT_FROM_EMAIL + recipient_list=[interest.user.email], + html_message=email_html, + ) + + logger.info('Sent interest email:\n' + email_text) diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index 1f53e8d..e3237ae 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -285,7 +285,19 @@ class SessionViewSet(Base, List, Retrieve, Create, Update): return serializers.SessionSerializer def perform_create(self, serializer): - serializer.save(instructor=self.request.user) + session = serializer.save(instructor=self.request.user) + interests = models.Interest.objects.filter(course=session.course, satisfied_by__isnull=True) + + for interest in interests: + try: + utils_email.send_interest_email(interest) + except BaseException as e: + msg = 'Problem interest email: ' + str(e) + logger.exception(msg) + alert_tanner(msg) + + num_satisfied = interests.update(satisfied_by=session) + logging.info('Satisfied %s interests.', num_satisfied) def generate_ical(self, session): cal = icalendar.Calendar()