Skip to content

Commit

Permalink
Add email sending for getting off the waitlist
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-zw committed Apr 24, 2020
1 parent 517f3de commit 0356cef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
7 changes: 2 additions & 5 deletions hknweb/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions hknweb/events/templates/events/off_waitlist_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ subject }}</title>
</head>
<body>

<p>
You have gotten off the waitlist for the event {{ event_name }}!
Click
<a href="{{ event_link }}">here</a>
to view the event.
</p>

<p>
Have a nice day!
</p>

<img src="{{ img_link }}" alt="cute animal"><br>
<span style="font-size: 0.8em;"> Image source: pexels.com </span>

</body>
</html>
31 changes: 28 additions & 3 deletions hknweb/events/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()

0 comments on commit 0356cef

Please sign in to comment.