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 @@ +<!DOCTYPE html> +<html> + <head> + <title></title> + <style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style> + </head> + <body> + <div>Hi [name],<br></div> + <div><br></div> + <div>There's been a class scheduled for [course] that you expressed interest in.<br></div> + <div><br></div> + <div>You can find the class on its course page here:<br></div> + <div><a href="[link]">[link]</a><br></div> + <div><br></div> + <div>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.<br></div> + <div><br></div> + <div>Spaceport<br></div> + </body> +</html> 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()