Commit 51df419d authored by Claude Paroz's avatar Claude Paroz

Notify users when role changed or removed from team

Fixes bug #770033.
parent 82da6900
......@@ -4,6 +4,7 @@ import hashlib, random
from django import forms
from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.utils.encoding import force_bytes
from django.utils.translation import ugettext_lazy, ugettext as _
......@@ -76,7 +77,6 @@ class RegistrationForm(forms.Form):
new_user.is_active = False
new_user.save()
# Send activation email
from django.core.mail import send_mail
current_site = get_current_site(request)
subject = settings.EMAIL_SUBJECT_PREFIX + _(u'Account activation')
message = _(u"This is a confirmation that your registration on %s succeeded. To activate your account, please click on the link below or copy and paste it in a browser.") % current_site.name
......
# -*- coding: utf-8 -*-
from django import forms
from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import send_mail
from django.utils.translation import ugettext as _
from common.utils import is_site_admin
......@@ -50,6 +53,7 @@ class EditTeamDetailsForm(forms.ModelForm):
new_coord.role = 'coordinator'
new_coord.save()
class EditMemberRoleForm(forms.Form):
def __init__(self, roles, *args, **kwargs):
......@@ -69,3 +73,35 @@ class EditMemberRoleForm(forms.Form):
for key, field in self.fields.items():
if key not in ('form_type',):
yield self[key]
def save(self, request):
current_site = get_current_site(request)
for key, field in self.fields.items():
form_value = self.cleaned_data[key]
if field.initial != form_value:
role = Role.objects.get(pk=key)
if form_value == "remove":
team = role.team.description
email = role.person.email
role.delete()
subject = settings.EMAIL_SUBJECT_PREFIX + ' ' + _('Removed from team')
message = _("You have been removed from the %(team)s team on %(site)s") % {
'team': team,
'site': current_site,
}
message += "\n\n" + _("This is an automatic message sent from %(site)s. Please do not answer.") % {'site': current_site}
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [email])
elif form_value == "inactivate":
role.is_active = False
role.save()
else:
role.role = form_value
role.save()
subject = settings.EMAIL_SUBJECT_PREFIX + ' ' + _('Role changed')
message = _("Your role in the %(team)s team on %(site)s has been set to '%(role)s'") % {
'team': role.team.description,
'site': current_site,
'role': role.get_role_display(),
}
message += "\n\n" + _("This is an automatic message sent from %(site)s. Please do not answer.") % {'site': current_site}
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [role.person.email])
......@@ -167,6 +167,35 @@ class TeamTest(TeamsAndRolesTests):
team = Team.objects.get(name='fr')
self.assertEqual(team.webpage_url, "http://www.gnomefr.org/")
def test_edit_team_roles(self):
team_url = reverse('team_slug', args = ['fr'])
# Login as team coordinator
response = self.client.post('/login/',
{'username': self.pcoo.username, 'password': 'password'})
# Team member role modification
response = self.client.post(team_url, {
'form_type': 'reviewer',
'%d' % Role.objects.get(team=self.t, person=self.pr).pk: 'committer',
})
self.assertEqual(len(mail.outbox), 1)
self.assertIn('Role changed', mail.outbox[0].subject)
self.assertIn(
"Your role in the French team on example.com has been set to 'Committer'",
mail.outbox[0].body
)
mail.outbox = []
# Team member removal
response = self.client.post(team_url, {
'form_type': 'translator',
'%d' % Role.objects.get(team=self.t, person=self.pt).pk: 'remove',
})
self.assertEqual(len(mail.outbox), 1)
self.assertIn('Removed from team', mail.outbox[0].subject)
self.assertIn(
"You have been removed from the French team on example.com",
mail.outbox[0].body
)
def test_send_mail_to_coordinator(self):
self.t.send_mail_to_coordinator(subject="foo", message="bar")
self.assertEqual(len(mail.outbox), 1)
......
......@@ -87,18 +87,7 @@ def team(request, team_slug):
roles = Role.objects.filter(team=team, role=form_type, is_active=True)
form = EditMemberRoleForm(roles, request.POST)
if form.is_valid():
for key, field in form.fields.items():
form_value = form.cleaned_data[key]
if field.initial != form_value:
role = Role.objects.get(pk=key)
if form_value == "remove":
role.delete()
elif form_value == "inactivate":
role.is_active = False
role.save()
else:
role.role = form_value
role.save()
form.save(request)
# Create forms for template
commit_roles = Role.objects.filter(team=team, role='committer', is_active=True)
if commit_roles:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment