diff --git a/hknweb/events/models.py b/hknweb/events/models.py index f67950b4..7f682637 100644 --- a/hknweb/events/models.py +++ b/hknweb/events/models.py @@ -50,13 +50,10 @@ def waitlist_set(self): return self.rsvp_set.none() return self.rsvp_set.order_by("created_at")[self.rsvp_limit:] - def check_newly_off_waitlist(self, old_admitted): + def newly_off_waitlist_rsvps(self, old_admitted): """ old_admitted must be a set, not a QuerySet. QuerySets are mutable views into the database. """ new_admitted = set(self.admitted_set()) - newly_off_waitlist = new_admitted - old_admitted - print("CHECKING OFF WAITLIST:", new_admitted, old_admitted, newly_off_waitlist) - for rsvp in newly_off_waitlist: - print("NEW USER:", rsvp.user.username) + return new_admitted - old_admitted class Rsvp(models.Model): diff --git a/hknweb/events/templates/events/off_waitlist_email.html b/hknweb/events/templates/events/off_waitlist_email.html new file mode 100644 index 00000000..32850761 --- /dev/null +++ b/hknweb/events/templates/events/off_waitlist_email.html @@ -0,0 +1,24 @@ + + + + + {{ subject }} + + + +

+ You have gotten off the waitlist for the event {{ event_name }}! + Click + here + to view the event. +

+ +

+ Have a nice day! +

+ + cute animal
+ Image source: pexels.com + + + diff --git a/hknweb/events/views.py b/hknweb/events/views.py index 632c4d45..c993407a 100644 --- a/hknweb/events/views.py +++ b/hknweb/events/views.py @@ -1,10 +1,13 @@ from django.shortcuts import render, redirect from django.http import Http404 from django.contrib import messages -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, reverse +from django.core.mail import EmailMultiAlternatives +from django.conf import settings +from django.template.loader import render_to_string from django.views import generic -from hknweb.utils import login_and_permission, method_login_and_permission +from hknweb.utils import login_and_permission, method_login_and_permission, get_rand_photo from .models import Event, EventType, Rsvp from .forms import EventForm @@ -81,7 +84,8 @@ def unrsvp(request, id): else: old_admitted = set(event.admitted_set()) rsvp.delete() - event.check_newly_off_waitlist(old_admitted) + for off_waitlist_rsvp in event.newly_off_waitlist_rsvps(old_admitted): + send_off_waitlist_email(request, off_waitlist_rsvp.user, event) return redirect(event) @login_and_permission('events.add_event') @@ -112,3 +116,24 @@ def form_valid(self, form): messages.success(self.request, "People who rsvp'd or are on the waitlist are not notified" " when you change the rsvp limit. Be sure to make an announcement!") return super().form_valid(form) + +# Helpers + +def send_off_waitlist_email(request, user, event): + subject = '[HKN] You have gotten off the waitlist for your event' + + event_link = request.build_absolute_uri( + reverse("events:detail", kwargs={ 'id': event.id })) + html_content = render_to_string( + 'events/off_waitlist_email.html', + { + 'subject': subject, + 'event_name': event.name, + 'event_link': event_link, + 'img_link': get_rand_photo(), + } + ) + msg = EmailMultiAlternatives(subject, subject, + settings.NO_REPLY_EMAIL, [user.email]) + msg.attach_alternative(html_content, "text/html") + msg.send()