Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for (ticket) email blasts #749

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3039,6 +3039,75 @@ def issue_tickets(self, request, *args, **kwargs):
{"success": True, "detail": f"Issued {len(tickets)} tickets", "errors": []}
)

@action(detail=True, methods=["post"])
def email_blast(self, request, *args, **kwargs):
"""
Send an email blast to all users holding tickets.
---
requestBody:
content:
application/json:
schema:
type: object
properties:
content:
type: string
description: The content of the email blast to send
required:
- content
responses:
"200":
description: Email blast was sent successfully
content:
application/json:
schema:
type: object
properties:
detail:
type: string
description: A message indicating how many
recipients received the blast
"404":
description: Event not found
content:
application/json:
schema:
type: object
properties:
detail:
type: string
description: Error message indicating event was
not found
---
"""
event = self.get_object()

holder_emails = Ticket.objects.filter(
event=event, owner__isnull=False
).values_list("owner__email", flat=True)
officer_emails = event.club.get_officer_emails()
emails = list(holder_emails) + list(officer_emails)

send_mail_helper(
name="blast",
subject=f"Update on {event.name} from {event.club.name}",
emails=emails,
context={
"sender": event.club.name,
"content": request.data.get("content"),
aviupadhyayula marked this conversation as resolved.
Show resolved Hide resolved
"reply_emails": event.club.get_officer_emails(),
},
)

return Response(
{
"detail": (
f"Blast sent to {len(holder_emails)} ticket holders "
f"and {len(officer_emails)} officers"
)
}
)

@action(detail=True, methods=["post"])
def upload(self, request, *args, **kwargs):
"""
Expand Down
25 changes: 25 additions & 0 deletions backend/templates/emails/blast.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- TYPES:
sender:
type: string
content:
type: string
reply_emails:
type: array
items:
type: string
-->
{% extends 'emails/base.html' %}

{% block content %}
<h2>Message from {{ sender }}</h2>

<p style="font-size: 1.2em">
The following message was sent by <b>{{ sender }}</b>:
</p>

<p style="font-size: 1.2em; padding: 8px; border-left: 5px solid #ccc; white-space: pre-wrap;">{{ content }}</p>

<p style="font-size: 1.2em">
If you have any questions, please respond to this email or <a href="mailto:{% for email in reply_emails %}{% if not forloop.first %},{% endif %}{{ email }}{% endfor %}">contact the sender</a>.
</p>
{% endblock %}
29 changes: 29 additions & 0 deletions backend/tests/clubs/test_ticketing.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,35 @@ def test_issue_tickets_insufficient_quantity(self):
Ticket.objects.filter(type="normal", holder__isnull=False).count(), 0
)

def test_email_blast(self):
Membership.objects.create(
person=self.user1, club=self.club1, role=Membership.ROLE_OFFICER
)
self.client.login(username=self.user1.username, password="test")

ticket1 = self.tickets1[0]
ticket1.owner = self.user2
ticket1.save()

resp = self.client.post(
reverse("club-events-email-blast", args=(self.club1.code, self.event1.pk)),
{"content": "Test email blast content"},
format="json",
)

self.assertEqual(resp.status_code, 200, resp.content)

self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]

self.assertIn(self.user2.email, email.to)
self.assertIn(self.user1.email, email.to)

self.assertEqual(
email.subject, f"Update on {self.event1.name} from {self.club1.name}"
)
self.assertIn("Test email blast content", email.body)

def test_get_tickets_information_no_tickets(self):
# Delete all the tickets
Ticket.objects.all().delete()
Expand Down
Loading