From b120321cd952b8778fce3efc18222d2b77270511 Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Mon, 6 Aug 2018 22:53:55 -0400 Subject: [PATCH] Email admins from an organization's member teams to notify them of a newly created common event that they should participate in --- events/models/events.py | 19 ++++---- .../emails/orgs/invite_to_common_event.html | 16 +++++++ .../emails/orgs/invite_to_common_event.txt | 13 ++++++ get_together/views/events.py | 2 + get_together/views/orgs.py | 43 ++++++++++++++++++- 5 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 get_together/templates/get_together/emails/orgs/invite_to_common_event.html create mode 100644 get_together/templates/get_together/emails/orgs/invite_to_common_event.txt diff --git a/events/models/events.py b/events/models/events.py index 3c9cc08..c37c395 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -341,16 +341,17 @@ class CommonEvent(models.Model): return "%s://%s%s" % (schema, site.domain, self.get_absolute_url()) def location(self): - if not self.continent: - return _('Global') - elif not self.country: - return self.continent - elif not self.spr: - return self.country - elif not self.city: - return self.spr - else: + if self.city: return self.city + elif self.spr: + return self.spr + elif self.country: + return self.country + elif self.continent: + return self.continent + else: + return _('Global') + @property def slug(self): return slugify(self.name) diff --git a/get_together/templates/get_together/emails/orgs/invite_to_common_event.html b/get_together/templates/get_together/emails/orgs/invite_to_common_event.html new file mode 100644 index 0000000..eb7787b --- /dev/null +++ b/get_together/templates/get_together/emails/orgs/invite_to_common_event.html @@ -0,0 +1,16 @@ +{% extends "get_together/emails/base.html" %} + +{% block content %} +

Your team has been invited to participate in an event

+ +

{{org.name}} is organizing an event on {{event.start_time|date:"D d M Y"}} and they want {{team.name}} to join them.

+ +

{{event.name}}

+

{{event.summary}}

+ +
+Click here to host an event with your team. +
+or you can click here to view the organization event. +

+{% endblock %} diff --git a/get_together/templates/get_together/emails/orgs/invite_to_common_event.txt b/get_together/templates/get_together/emails/orgs/invite_to_common_event.txt new file mode 100644 index 0000000..4ed7673 --- /dev/null +++ b/get_together/templates/get_together/emails/orgs/invite_to_common_event.txt @@ -0,0 +1,13 @@ +{% extends "get_together/emails/base.txt" %} +{% block content %} +== Your team has been invited to participate in an event == + +{{org.name}} is organizing an event on {{event.start_time|date:"D d M Y"}} and they want {{team.name}} to join them. + +=== {{event.name}} === +{{event.summary}} + +Click here to host an event with your team: https://{{site.domain}}{% url 'create-event' team.id %}?common={{event.id}} +or you can view the organization event: {{event.get_full_url}} + +{% endblock %} diff --git a/get_together/views/events.py b/get_together/views/events.py index 5e66c2a..da7d9f8 100644 --- a/get_together/views/events.py +++ b/get_together/views/events.py @@ -129,6 +129,8 @@ def create_event(request, team_id): new_event.parent = CommonEvent.objects.get(id=request.GET['common']) initial['name'] = new_event.parent.name initial['summary'] = new_event.parent.summary + initial['start_time'] = new_event.parent.start_time + initial['end_time'] = new_event.parent.end_time form = NewTeamEventForm(instance=new_event, initial=initial) context = { diff --git a/get_together/views/orgs.py b/get_together/views/orgs.py index b79cc55..e87d7d1 100644 --- a/get_together/views/orgs.py +++ b/get_together/views/orgs.py @@ -4,7 +4,7 @@ from django.contrib import messages from django.contrib.auth import logout as logout_user from django.contrib.auth.decorators import login_required from django.contrib.sites.models import Site -from django.shortcuts import render, redirect, get_object_or_404 +from django.shortcuts import render, redirect, reverse, get_object_or_404 from django.http import HttpResponse, JsonResponse from django.core.mail import send_mail from django.template.loader import get_template, render_to_string @@ -316,6 +316,7 @@ def create_common_event(request, org_slug): form = NewCommonEventForm(request.POST, instance=new_event) if form.is_valid: new_event = form.save() + send_common_event_invite(new_event) return redirect('show-common-event', new_event.id, new_event.slug) else: context = { @@ -327,6 +328,46 @@ def create_common_event(request, org_slug): return redirect('home') +def send_common_event_invite(event): + context = { + 'sender': event.created_by, + 'org': event.organization, + 'event': event, + 'site': Site.objects.get(id=1), + } + email_subject = 'Participate in our event: %s' % event.name + email_from = getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@gettogether.community') + + teams = event.organization.teams.all() + if event.city: + teams = teams.filter(city=event.city) + elif event.spr: + teams = teams.filter(city__spr=event.spr) + elif event.country: + teams = teams.filter(city__spr__country=event.country) + + for admin in Member.objects.filter(team__in=teams, role=Member.ADMIN, user__user__account__is_email_confirmed=True): + context['team'] = admin.team + email_body_text = render_to_string('get_together/emails/orgs/invite_to_common_event.txt', context) + email_body_html = render_to_string('get_together/emails/orgs/invite_to_common_event.html', context) + success = send_mail( + from_email=email_from, + html_message=email_body_html, + message=email_body_text, + recipient_list=[admin.user.user.email], + subject=email_subject, + fail_silently=True, + ) + EmailRecord.objects.create( + sender=event.created_by.user, + recipient=admin.user.user, + email=admin.user.user.email, + subject=email_subject, + body=email_body_text, + ok=success + ) + + @login_required def create_common_event_team_select(request, event_id): teams = request.user.profile.moderating