From 89d691b6ba987c32e67063a5edbb329db85f1930 Mon Sep 17 00:00:00 2001
From: Michael Hall
Date: Sun, 13 May 2018 16:59:03 -0400
Subject: [PATCH] Send emails to team admins when they get new members, and to
event hosts when they get new attendees
---
events/admin.py | 4 +-
.../commands/send_daily_attendee_update.py | 59 +++++++++++++++++++
.../commands/send_daily_member_update.py | 59 +++++++++++++++++++
.../templates/get_together/emails/base.txt | 3 +
.../emails/new_event_attendees.html | 14 +++++
.../emails/new_event_attendees.txt | 11 ++++
.../get_together/emails/new_team_members.html | 14 +++++
.../get_together/emails/new_team_members.txt | 11 ++++
get_together/views/events.py | 1 +
9 files changed, 174 insertions(+), 2 deletions(-)
create mode 100644 get_together/management/commands/send_daily_attendee_update.py
create mode 100644 get_together/management/commands/send_daily_member_update.py
create mode 100644 get_together/templates/get_together/emails/new_event_attendees.html
create mode 100644 get_together/templates/get_together/emails/new_event_attendees.txt
create mode 100644 get_together/templates/get_together/emails/new_team_members.html
create mode 100644 get_together/templates/get_together/emails/new_team_members.txt
diff --git a/events/admin.py b/events/admin.py
index 60224a1..d01770b 100644
--- a/events/admin.py
+++ b/events/admin.py
@@ -123,8 +123,8 @@ class MemberAdmin(admin.ModelAdmin):
admin.site.register(Member, MemberAdmin)
class AttendeeAdmin(admin.ModelAdmin):
- list_display = ('__str__', 'role', 'status', 'last_reminded')
- list_filter = ('role', 'status')
+ list_display = ('__str__', 'role', 'status', 'joined_date', 'last_reminded')
+ list_filter = ('role', 'status', 'joined_date')
admin.site.register(Attendee, AttendeeAdmin)
class CategoryAdmin(admin.ModelAdmin):
diff --git a/get_together/management/commands/send_daily_attendee_update.py b/get_together/management/commands/send_daily_attendee_update.py
new file mode 100644
index 0000000..9b5d0bc
--- /dev/null
+++ b/get_together/management/commands/send_daily_attendee_update.py
@@ -0,0 +1,59 @@
+from django.core.management.base import BaseCommand, CommandError
+from django.urls import reverse
+from django.core.mail import send_mail
+from django.template.loader import get_template, render_to_string
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.utils import timezone
+
+from events.models import Event, Attendee
+
+import datetime
+
+
+class Command(BaseCommand):
+ help = "Sends email to event hosts about new attendees"
+
+ def handle(self, *args, **options):
+ # Attendees who recently joined
+ attendees = Attendee.objects.filter(role=Attendee.NORMAL, joined_date__gte=timezone.now() - datetime.timedelta(days=1)).order_by('event')
+
+ current_event = None
+ new_attendees = []
+ for attendee in attendees:
+
+ if attendee.event != current_event:
+ send_new_attendees(current_event, new_attendees)
+ current_event = attendee.event
+ new_attendees = []
+
+ new_attendees.append(attendee)
+ if current_event is not None:
+ send_new_attendees(current_event, new_attendees)
+
+
+def send_new_attendees(event, new_attendees):
+ if len(new_attendees) < 1:
+ return
+ host_emails = [attendee.user.user.email for attendee in Attendee.objects.filter(event=event, role=Attendee.HOST) if attendee.user.user.account.is_email_confirmed]
+ if len(host_emails) < 1:
+ return
+ context = {
+ 'event': event,
+ 'attendees': new_attendees,
+ 'site': Site.objects.get(id=1)
+ }
+
+ email_subject = '[GetTogether] New event attendees'
+ email_body_text = render_to_string('get_together/emails/new_event_attendees.txt', context)
+ email_body_html = render_to_string('get_together/emails/new_event_attendees.html', context)
+ email_recipients = host_emails
+ email_from = getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@gettogether.community')
+
+ send_mail(
+ from_email=email_from,
+ html_message=email_body_html,
+ message=email_body_text,
+ recipient_list=email_recipients,
+ subject=email_subject,
+ )
diff --git a/get_together/management/commands/send_daily_member_update.py b/get_together/management/commands/send_daily_member_update.py
new file mode 100644
index 0000000..07c75b2
--- /dev/null
+++ b/get_together/management/commands/send_daily_member_update.py
@@ -0,0 +1,59 @@
+from django.core.management.base import BaseCommand, CommandError
+from django.urls import reverse
+from django.core.mail import send_mail
+from django.template.loader import get_template, render_to_string
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.utils import timezone
+
+from events.models import Event, Member
+
+import datetime
+
+
+class Command(BaseCommand):
+ help = "Sends email to team admins about new members"
+
+ def handle(self, *args, **options):
+ # members who recently joined
+ members = Member.objects.filter(role=Member.NORMAL, joined_date__gte=timezone.now() - datetime.timedelta(days=1)).order_by('team')
+
+ current_team = None
+ new_members = []
+ for member in members:
+
+ if member.team != current_team:
+ send_new_members(current_team, new_members)
+ current_team = member.team
+ new_members = []
+
+ new_members.append(member)
+ if current_team is not None:
+ send_new_members(current_team, new_members)
+
+
+def send_new_members(team, new_members):
+ if len(new_members) < 1:
+ return
+ admin_emails = [member.user.user.email for member in Member.objects.filter(team=team, role=Member.ADMIN) if member.user.user.account.is_email_confirmed]
+ if len(admin_emails) < 1:
+ return
+ context = {
+ 'team': team,
+ 'members': new_members,
+ 'site': Site.objects.get(id=1)
+ }
+
+ email_subject = '[GetTogether] New team members'
+ email_body_text = render_to_string('get_together/emails/new_team_members.txt', context)
+ email_body_html = render_to_string('get_together/emails/new_team_members.html', context)
+ email_recipients = admin_emails
+ email_from = getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@gettogether.community')
+
+ send_mail(
+ from_email=email_from,
+ html_message=email_body_html,
+ message=email_body_text,
+ recipient_list=email_recipients,
+ subject=email_subject,
+ )
diff --git a/get_together/templates/get_together/emails/base.txt b/get_together/templates/get_together/emails/base.txt
index f6f6f91..beeb450 100644
--- a/get_together/templates/get_together/emails/base.txt
+++ b/get_together/templates/get_together/emails/base.txt
@@ -1,3 +1,6 @@
+= Email from GetTogether.Community =
+
+{% block content %}{% endblock %}
--
This is an automated email sent by https://gettogether.community
Learn more at https://github.com/GetTogetherComm/GetTogether/
diff --git a/get_together/templates/get_together/emails/new_event_attendees.html b/get_together/templates/get_together/emails/new_event_attendees.html
new file mode 100644
index 0000000..3abe7ec
--- /dev/null
+++ b/get_together/templates/get_together/emails/new_event_attendees.html
@@ -0,0 +1,14 @@
+{% extends "get_together/emails/base.html" %}
+
+{% block content %}
+You have new attendees for {{event.name|striptags}}
+
+
+ {% for attendee in attendees %}
+ - {{attendee.user}} - {{attendee.status_name}}
+ {% endfor %}
+
+
+View this event.
+
+{% endblock %}
diff --git a/get_together/templates/get_together/emails/new_event_attendees.txt b/get_together/templates/get_together/emails/new_event_attendees.txt
new file mode 100644
index 0000000..8b01f38
--- /dev/null
+++ b/get_together/templates/get_together/emails/new_event_attendees.txt
@@ -0,0 +1,11 @@
+{% extends 'get_together/emails/base.txt' %}
+{% block content %}
+== You have new attendees for {{event.name|striptags}} ==
+
+{% for attendee in attendees %}
+ * {{attendee.user}} - {{attendee.status_name}}
+{% endfor %}
+
+Click here to view this event: {{event.get_full_url}}
+
+{% endblock %}
diff --git a/get_together/templates/get_together/emails/new_team_members.html b/get_together/templates/get_together/emails/new_team_members.html
new file mode 100644
index 0000000..cb4fac7
--- /dev/null
+++ b/get_together/templates/get_together/emails/new_team_members.html
@@ -0,0 +1,14 @@
+{% extends "get_together/emails/base.html" %}
+
+{% block content %}
+You have members of {{team.name|striptags}}
+
+
+
+View this team.
+
+{% endblock %}
diff --git a/get_together/templates/get_together/emails/new_team_members.txt b/get_together/templates/get_together/emails/new_team_members.txt
new file mode 100644
index 0000000..6448ff3
--- /dev/null
+++ b/get_together/templates/get_together/emails/new_team_members.txt
@@ -0,0 +1,11 @@
+{% extends 'get_together/emails/base.txt' %}
+{% block content %}
+== You have new members of {{team.name|striptags}} ==
+
+{% for member in members %}
+ * {{member.user}}
+{% endfor %}
+
+Click here to view this team: https://{{site.domain}}{% url 'show-team' team.id %}
+
+{% endblock %}
diff --git a/get_together/views/events.py b/get_together/views/events.py
index c969f8d..afdbccb 100644
--- a/get_together/views/events.py
+++ b/get_together/views/events.py
@@ -243,6 +243,7 @@ def attend_event(request, event_id):
attendee.status = Attendee.MAYBE
if request.GET.get('response', None) == 'no':
attendee.status = Attendee.NO
+ attendee.joined_date = timezone.now()
attendee.save()
if attendee.status == Attendee.YES:
messages.add_message(request, messages.SUCCESS, message=_("We'll see you there!"))