Skip to content

Commit

Permalink
Merge pull request #5200 from onepercentclub/bugfix/BB-20448-join-tea…
Browse files Browse the repository at this point in the history
…m-mails

Fix mails for joining a team
  • Loading branch information
gannetson authored Aug 17, 2022
2 parents febba5c + 21e02cf commit aac6f8f
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 63 deletions.
4 changes: 2 additions & 2 deletions bluebottle/activities/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ def is_valid(self):
self.instance.activity.team_activity == Activity.TeamActivityChoices.teams
)

def post_save(self, **kwargs):
def pre_save(self, **kwargs):
if self.instance.accepted_invite:
self.instance.team = self.instance.accepted_invite.contributor.team
self.instance.save()

def post_save(self, **kwargs):
if not self.instance.team:
self.instance.team = Team.objects.create(
owner=self.instance.user,
Expand Down
4 changes: 2 additions & 2 deletions bluebottle/activities/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class TeamAppliedMessage(TeamNotification):

class TeamCaptainAcceptedMessage(TeamNotification):
subject = pgettext('email', 'Your team has been accepted for "{title}"')
template = 'messages/team_accepted'
template = 'messages/team_captain_accepted'

context = {
'title': 'activity.title',
Expand Down Expand Up @@ -382,7 +382,7 @@ def get_recipients(self):


class TeamMemberAddedMessage(ActivityNotification):
subject = pgettext('email', "New team member")
subject = pgettext('email', 'Someone has joined your team for "{title}"')
template = 'messages/team_member_added'

context = {
Expand Down
15 changes: 0 additions & 15 deletions bluebottle/activities/templates/mails/messages/team_accepted.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "mails/messages/activity_base.html" %}
{% load i18n %}

{% block message%}
{% block message %}
<p>
{% if custom_message %}
{{custom_message|linebreaks}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "mails/messages/activity_base.html" %}
{% load i18n %}

{% block message %}
<p>
{% if custom_message %}
{{ custom_message|linebreaks }}
{% else %}
{% blocktrans context 'email' %}Your team has been accepted for the activity '{{ title }}'.
{% endblocktrans %}
{% endif %}
</p>
<p>
{% blocktrans context 'email' %}
On the activity page you will find the link to invite your team members.
{% endblocktrans %}
</p>
{% endblock %}
2 changes: 1 addition & 1 deletion bluebottle/activities/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_team_member_added_notification(self):
self.message_class = TeamMemberAddedMessage
self.create()
self.assertRecipients([self.captain])
self.assertSubject("New team member")
self.assertSubject('Someone has joined your team for "Save the world!"')
self.assertHtmlBodyContains(
f"{self.obj.user.full_name} is now part of your team for the activity ‘Save the world!’."
)
Expand Down
23 changes: 17 additions & 6 deletions bluebottle/activities/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from bluebottle.impact.effects import UpdateImpactGoalEffect
from bluebottle.notifications.effects import NotificationEffect
from bluebottle.time_based.messages import TeamParticipantJoinedNotification
from bluebottle.time_based.states import ParticipantStateMachine, TimeBasedStateMachine, TeamSlotStateMachine


Expand Down Expand Up @@ -207,7 +208,7 @@ def contributor_is_active(contribution):
]


def automatically_accept(effect):
def automatically_accept_team(effect):
"""
automatically accept team
"""
Expand Down Expand Up @@ -261,28 +262,38 @@ class TeamTriggers(TriggerManager):
effects=[
NotificationEffect(
TeamAddedMessage,
conditions=[automatically_accept]
conditions=[automatically_accept_team]
),
NotificationEffect(
TeamAppliedMessage,
conditions=[needs_review]
conditions=[
needs_review,
]
),
TransitionEffect(
TeamStateMachine.accept,
conditions=[
automatically_accept
automatically_accept_team
]
)
),
]
),

TransitionTrigger(
TeamStateMachine.accept,
effects=[
NotificationEffect(
TeamParticipantJoinedNotification,
conditions=[
automatically_accept_team
]
),
RelatedTransitionEffect(
'members',
ParticipantStateMachine.accept,
conditions=[needs_review]
conditions=[
needs_review
]
),
RelatedTransitionEffect(
'activity',
Expand Down
33 changes: 30 additions & 3 deletions bluebottle/time_based/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class ReminderTeamSlotNotification(TransitionMessage):
'start': 'start',
'duration': 'duration',
'end': 'end',
'timezone': 'timezone',
'location': 'location',
}

def already_send(self, recipient):
Expand Down Expand Up @@ -281,6 +283,8 @@ class TeamSlotChangedNotification(TransitionMessage):
'start': 'start',
'duration': 'duration',
'end': 'end',
'timezone': 'timezone',
'location': 'location',
}

@property
Expand Down Expand Up @@ -456,7 +460,7 @@ def get_recipients(self):
return [self.obj.user]


class TeamParticipantJoinedNotification(TimeBasedInfoMixin, TransitionMessage):
class TeamParticipantJoinedNotification(TransitionMessage):
"""
The participant joined
"""
Expand All @@ -475,8 +479,8 @@ def action_link(self):
action_title = pgettext('email', 'View activity')

def get_recipients(self):
"""participant"""
return [self.obj.user]
"""team captain"""
return [self.obj.owner]


class ParticipantChangedNotification(TimeBasedInfoMixin, TransitionMessage):
Expand Down Expand Up @@ -563,6 +567,29 @@ def get_recipients(self):
return [self.obj.user]


class TeamMemberJoinedNotification(TimeBasedInfoMixin, TransitionMessage):
"""
The participant joined as a team joined
"""
subject = pgettext('email', 'You have joined {team_name} for "{title}"')
template = 'messages/team_member_joined'
context = {
'title': 'activity.title',
'team_name': 'team.name'
}
# delay = 60

@property
def action_link(self):
return self.obj.activity.get_absolute_url()

action_title = pgettext('email', 'View activity')

def get_recipients(self):
"""participant"""
return [self.obj.user]


class ParticipantAcceptedNotification(TimeBasedInfoMixin, TransitionMessage):
"""
The participant got accepted after review
Expand Down
5 changes: 5 additions & 0 deletions bluebottle/time_based/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,11 @@ def end(self):
if self.start and self.duration:
return self.start + self.duration

@property
def timezone(self):
if self.start:
return self.start.strftime("%Z %z")

@property
def is_complete(self):
return self.start and self.duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
Some details of the team activity you are a part of have changed:
{% endblocktrans %}
</p>
<p>
{% blocktrans context 'email' %}
Updated details below:
{% endblocktrans %}
</p>
<p>
{{ team_name }}
</p>
<p>
{{ title }}
"{{ title }}"
</p>

{% if duration %}
{% include 'mails/messages/partial/period.html' %}
{% else %}
{% include 'mails/messages/partial/slots.html' %}
{% endif %}
</p>
{% include 'mails/messages/partial/team_slot.html' %}

<p>
{% blocktrans context 'email' %}
Please view your team from the activity page to see the changes.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% load i18n tz %}
<p>
{% trans 'Date' %}: {{ start|date:"SHORT_DATE_FORMAT" }}
</p>
<p>
{% trans 'Time' %}: {{ start|date:'TIME_FORMAT' }} - {{ end|date:'TIME_FORMAT' }} {{ timezone }}
</p>
{% if location %}
<p>
{% trans 'Location' %}: {{ location }} {{ location_hint }}
</p>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{{ title }}
</p>
<p>
{% include 'mails/messages/partial/period.html' %}
{% include 'mails/messages/partial/team_slot.html' %}
</p>

<p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends "mails/messages/participant_base.html" %}
{% load i18n %}

{% block message %}
<p>
{% blocktrans context 'email' %}
You joined {{ team_name }} for an activity on <b>{{ site_name }}!</b>
{% endblocktrans %}
</p>

<p>
<b>{{ title }}</b>
</p>

{% if slots %}
{% include 'mails/messages/partial/slots.html' %}
{% else %}
<p>
{% blocktrans context 'email' %}
The activity manager will be in touch to confirm details such as time, date and location.
{% endblocktrans %}
</p>
{% endif %}

{% endblock %}


{% block end_message %}
<p>
<i>
{% blocktrans context 'email' %}
If you are unable to participate, please withdraw via the activity page so that others can take your place.
{% endblocktrans %}
</i>
</p>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
</p>

<p>
The activity manager will be in touch to confirm details such as time, date and location. You can start inviting team members, or wait until the details have been set.
{% blocktrans context 'email' %}
The activity manager will be in touch to confirm details such as time, date and location. You can start inviting team members, or wait until the details have been set.
{% endblocktrans %}
</p>

<p>
Expand Down
42 changes: 38 additions & 4 deletions bluebottle/time_based/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,40 @@ class PeriodParticipantListAPIViewTestCase(ParticipantListViewTestCase, Bluebott
document_url_name = 'period-participant-document'
participant_type = 'contributors/time-based/period-participants'

def test_join_team(self):
self.activity.team_activity = 'teams'
self.activity.save()
captain = PeriodParticipantFactory.create(
activity=self.activity
)
self.data['data']['relationships']['accepted-invite'] = {
'data': {
'type': 'activities/invites',
'id': str(captain.invite.id)
}
}
mail.outbox = []
self.response = self.client.post(self.url, json.dumps(self.data), user=self.user)
self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)
self.assertEqual(len(mail.outbox), 2)

self.assertEqual(
mail.outbox[0].subject,
f'You have joined {captain.team.name} for "{self.activity.title}"'
)
self.assertEqual(
mail.outbox[0].to[0],
self.user.email
)
self.assertEqual(
mail.outbox[1].subject,
f'Someone has joined your team for "{self.activity.title}"'
)
self.assertEqual(
mail.outbox[1].to[0],
captain.user.email
)


class ParticipantDetailViewTestCase():
def setUp(self):
Expand Down Expand Up @@ -2221,15 +2255,15 @@ def test_accept_with_custom_message(self):
self.assertTrue('Great to have you!' in message.body)

def test_accept_team_with_custom_message(self):
self.activity.team_activity = 'teams'
self.activity.review = True
self.activity.save()
self.participant.status = 'new'
self.participant.team = TeamFactory.create(
activity=self.activity,
owner=self.participant.user
)
self.participant.status = 'new'
self.participant.save()
self.activity.team_activity = 'teams'
self.activity.review = True
self.activity.save()
self.data['data']['attributes']['transition'] = 'accept'
self.data['data']['attributes']['message'] = 'Great to have you!'
mail.outbox = []
Expand Down
2 changes: 1 addition & 1 deletion bluebottle/time_based/tests/test_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def setUp(self):
)
self.client = JSONAPITestClient()

def test_user_joins_activity(self):
def test_user_starts_a_team(self):
api_user_joins_period_activity(self, self.activity, self.supporter)
assert_participant_status(self, self.activity, self.supporter, status='accepted')
self.assertEqual(self.activity.teams.count(), 1)
Loading

0 comments on commit aac6f8f

Please sign in to comment.