Skip to content

Commit

Permalink
Add banquet email reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
didrikmunther committed Oct 8, 2024
1 parent abef520 commit 4a6ca5c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 38 deletions.
28 changes: 24 additions & 4 deletions banquet/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
from util.email import send_mail


def send_invitation_mail(request, invitation, name, banquet, link, email, fair):
def send_invitation_mail(
request,
invitation,
name,
banquet,
link,
email,
fair,
template="banquet/email/invitation.html",
subject="THS Armada Banquet Invitation",
):
"""Send banquet invitation mail"""
try:
send_mail(
request,
template="banquet/email/invitation.html",
template=template,
context={
"name": name,
"date": banquet.date,
Expand All @@ -18,7 +28,7 @@ def send_invitation_mail(request, invitation, name, banquet, link, email, fair):
"year": fair.year,
"deadline": invitation.group.deadline,
},
subject="THS Armada Banquet Invitation",
subject=subject,
to=[email],
# file_paths=[settings.MEDIA_ROOT + signature.contract.contract.url[6:]],
)
Expand All @@ -32,7 +42,15 @@ def send_invitation_mail(request, invitation, name, banquet, link, email, fair):
return True


def send_confirmation_email(request, invitation, name, email_address, fair):
def send_confirmation_email(
request,
invitation,
name,
email_address,
fair,
template="banquet/email/invitation.html",
subject="THS Armada Banquet Confirmation",
):
"""Send banquet confirmation mail"""

if invitation.has_sent_mail:
Expand Down Expand Up @@ -72,4 +90,6 @@ def send_confirmation_email(request, invitation, name, email_address, fair):
link,
email_address,
fair,
template=template,
subject=subject,
)
108 changes: 79 additions & 29 deletions banquet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,49 +713,99 @@ def manage_handle_email(request, year, banquet_pk):
invitations = Invitation.objects.filter(banquet=banquet, has_sent_mail=False)

if request.POST:
did_error_email = False
if "unsent" in request.POST:
did_error_email = False
sent = 0

for invitation in invitations:
invitation.save()

if not send_confirmation_email(
request,
invitation,
invitation.name,
invitation.email_address,
fair,
):
did_error_email = True
continue

for invitation in invitations:
invitation.save()
invitation.has_sent_mail = True
invitation.save()

print(
invitation,
invitation.name,
invitation.email_address,
fair,
return redirect(
reverse(
"banquet_handle_email",
kwargs={"year": year, "banquet_pk": banquet_pk},
)
+ "?"
+ ("&error=Failed to send some emails!" if did_error_email else "")
+ f"&success={sent} emails sent!"
)

if not send_confirmation_email(
request,
invitation,
invitation.name,
invitation.email_address,
fair,
):
did_error_email = True
continue

invitation.has_sent_mail = True
invitation.save()
if "reminder" in request.POST:
group_id = request.POST["group"]
if group_id == "":
return redirect(
reverse(
"banquet_handle_email",
kwargs={"year": year, "banquet_pk": banquet_pk},
)
+ "?error=Must%20select%20group"
)

return redirect(
reverse(
"banquet_handle_email",
kwargs={"year": year, "banquet_pk": banquet_pk},
)
+ ("?did_error_email=1" if did_error_email else "")
)
group = InvitationGroup.objects.get(pk=group_id)
did_error_email = False

print(invitations)
if group is None:
return redirect(
reverse(
"banquet_handle_email",
kwargs={"year": year, "banquet_pk": banquet_pk},
)
+ "?error=Group does not exist"
)

sent = 0
for invitation in invitations.filter(group=group, has_sent_mail=True):
invitation.save()

if not send_confirmation_email(
request,
invitation,
invitation.name,
invitation.email_address,
fair,
template="banquet/email/reminder.html",
subject="Reminder: THS Armada Banquet Confirmation",
):
did_error_email = True
continue

sent += 1
invitation.has_sent_mail = True
invitation.save()

return redirect(
reverse(
"banquet_handle_email",
kwargs={"year": year, "banquet_pk": banquet_pk},
)
+ "?"
+ ("&error=Failed to send some emails!" if did_error_email else "")
+ f'&success={sent} emails sent for group "{group}"!'
)

return render(
request,
"banquet/manage_handle_email.html",
{
"fair": fair,
"banquet": banquet,
"did_error_email": request.GET.get("did_error_email", False),
"error": request.GET.get("error", False),
"success": request.GET.get("success", False),
"invitations": invitations,
"groups": InvitationGroup.objects.filter(banquet=banquet),
},
)

Expand Down
4 changes: 2 additions & 2 deletions templates/banquet/email/invitation.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
<p style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
You have been invited to the Grand Banquet of THS Armada.
</p>
{% include 'email/button.html' with content="Go to invitation" url=link %}
{% include 'email/divider.html' %}
<ul>
<li style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
Time: {{ date|date:"Y-m-d" }} {{ date|date:"H:i" }}.
Expand All @@ -23,6 +21,8 @@
Deadline for accepting: {{ deadline|date:"d M" }}.
</li>
</ul>
{% include 'email/button.html' with content="Go to invitation" url=link %}
{% include 'email/divider.html' %}
<p style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
Please note that this is an automatic email, and you cannot respond to this email.
You can always reach out to {% include 'email/link.html' with url='mailto:[email protected]' text='[email protected]' %} if you have any questions.
Expand Down
33 changes: 33 additions & 0 deletions templates/banquet/email/reminder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends "email/base.html" %}

{% block content %}
<p style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
Hello {{ name }}!
</p>
<p style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
We would like to remind you that you have until the {{ deadline|date:"d M" }} to accept and fill in the invitation for the Grand Banquet of THS Armada.
</p>
<ul>
<li style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
Time: {{ date|date:"Y-m-d" }} {{ date|date:"H:i" }}.
</li>
<li style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
Location: {{ location }}.
</li>
<li style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
Dress code: {{ dress_code }}.
</li>
</ul>
{% include 'email/button.html' with content="Go to invitation" url=link %}
{% include 'email/divider.html' %}
<p style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
Please note that this is an automatic email, and you cannot respond to this email.
You can always reach out to {% include 'email/link.html' with url='mailto:[email protected]' text='[email protected]' %} if you have any questions.
</p>
<p style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
We hope to see you at the banquet!
</p>
<p style="font-size:16px;line-height:24px;margin:16px 0;color:#525f7f;text-align:left">
The Banquet Team of THS Armada {{ year }}
</p>
{% endblock %}
31 changes: 28 additions & 3 deletions templates/banquet/manage_handle_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% endblock %}

{% block content %}
{% if did_error_email %}
{% if error %}
<h1 style="
background-color: #f2dede;
border-color: #ebccd1;
Expand All @@ -16,7 +16,19 @@
padding: 15px;
border: 1px solid transparent;
border-radius: 4px;
">Failed to send some emails!</h1>
">{{error}}</h1>
{% endif %}

{% if success %}
<h1 style="
background-color: #ccebcd;
border-color: #ccebcd;
color: #1c9237;
margin: 0 0 20px 0;
padding: 15px;
border: 1px solid transparent;
border-radius: 4px;
">{{success}}</h1>
{% endif %}

<h1>{{ banquet.name }} – Unsent Email</h1>
Expand All @@ -29,7 +41,13 @@ <h1>{{ banquet.name }} – Unsent Email</h1>
<div>
<form method="post">
{% csrf_token %}
<button class="btn btn-danger">Send emails</button>
<select name="group">
<option value="">Select group</option>
{% for group in groups %}
<option value="{{ group.pk }}">{{ group.name }}</option>
{% endfor %}
</select>
<button class="btn btn-danger" type="submit" name="reminder">Send reminder emails</button>
</form>
</div>
</div>
Expand All @@ -53,6 +71,13 @@ <h1>{{ banquet.name }} – Unsent Email</h1>
}
</style>

<hr />

<form method="post">
{% csrf_token %}
<button class="btn btn-danger" type="submit" name="unsent">Send unsent emails</button>
</form>

<div class="table-responsive">
<table class="table" id="invitation_table">
<thead>
Expand Down

0 comments on commit 4a6ca5c

Please sign in to comment.