Commit e6557cb6 authored by Claude Paroz's avatar Claude Paroz

Add form to edit team's details by coordinator (Fixes #592529)

parent ddf70fdb
from django import forms
from teams.models import ROLE_CHOICES
from teams.models import Team, ROLE_CHOICES
class EditTeamDetailsForm(forms.ModelForm):
class Meta:
model = Team
fields = ('webpage_url', 'mailing_list', 'mailing_list_subscribe')
def __init__(self, *args, **kwargs):
super(EditTeamDetailsForm, self).__init__(*args, **kwargs)
for f in ('webpage_url', 'mailing_list', 'mailing_list_subscribe'):
self.fields[f].widget.attrs['size'] = 60
class EditMemberRoleForm(forms.Form):
......
......@@ -85,9 +85,9 @@ class Team(models.Model):
name = models.CharField(max_length=80)
description = models.TextField()
members = models.ManyToManyField(Person, through='Role', related_name='teams')
webpage_url = models.URLField(null=True, blank=True)
mailing_list = models.EmailField(null=True, blank=True)
mailing_list_subscribe = models.URLField(null=True, blank=True)
webpage_url = models.URLField(null=True, blank=True, verbose_name=_("Web page"))
mailing_list = models.EmailField(null=True, blank=True, verbose_name=_("Mailing list"))
mailing_list_subscribe = models.URLField(null=True, blank=True, verbose_name=_("URL to subscribe"))
objects = TeamManager()
class Meta:
......@@ -105,6 +105,13 @@ class Team(models.Model):
def get_absolute_url(self):
return ('team_slug', [self.name])
def can_edit(self, user):
""" Return True if user is allowed to edit this team
user is a User (from request.user), not a Person
"""
coordinator = self.get_coordinator()
return user.is_authenticated() and coordinator and user.username == coordinator.username
def fill_role(self, role, person):
""" Used by TeamManager to prefill roles in team """
if not self.roles:
......
......@@ -29,6 +29,7 @@ class TeamTest(TestCase):
self.pcoo = Person(first_name='John', last_name='Coordinator',
email='jcoo@imthebigboss.fr', username= 'jcoo')
self.pcoo.set_password('password')
self.pcoo.save()
self.t = Team(name='fr', description='French')
......@@ -124,3 +125,20 @@ class TeamTest(TestCase):
self.assertEquals(len(mail.outbox), 1)
self.assertEquals(mail.outbox[0].recipients()[0], self.pcoo.email)
def test_edit_team(self):
""" Test team edit form """
c = Client()
edit_url = reverse('team_edit', args = ['fr'], current_app='teams')
response = c.get(edit_url)
self.assertEquals(response.status_code, 403)
# Login as team coordinator
response = c.post('/login/', {'username': self.pcoo.username, 'password': 'password'})
# Try team modification
response = c.post(edit_url, {
'webpage_url' : u"http://www.gnomefr.org/",
'mailing_list' : u"gnomefr@traduc.org",
'mailing_list_subscribe': u""
})
team = Team.objects.get(name='fr')
self.assertEquals(team.webpage_url, u"http://www.gnomefr.org/")
......@@ -16,9 +16,13 @@ urlpatterns = patterns('',
view = 'teams.views.teams',
name = 'teams'),
url(
regex = r'(?P<team_slug>[\w\-@]+)',
regex = r'(?P<team_slug>[\w\-@]+)$',
view = 'teams.views.team',
name = 'team_slug'),
url(
regex = r'(?P<team_slug>[\w\-@]+)/edit/',
view = 'teams.views.team_edit',
name = 'team_edit'),
url(
regex = r'(?P<object_id>\d+)',
view = 'django.views.generic.list_detail.object_detail',
......
......@@ -20,11 +20,13 @@
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from django.utils.translation import ugettext_lazy as _
from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse
from django.template import RequestContext
from common import utils
from teams.models import Team, FakeTeam, Role
from teams.forms import EditMemberRoleForm
from teams.forms import EditMemberRoleForm, EditTeamDetailsForm
from languages.models import Language
def teams(request):
......@@ -72,8 +74,12 @@ def team(request, team_slug):
coordinator = None
mem_groups = ()
# Compare username because request.user is User and get_coordinator is Person
if request.user.is_authenticated() and coordinator and request.user.username == coordinator.username:
context = {
'pageSection': 'teams',
'team': team,
'can_edit_team': False,
}
if team.can_edit(request.user):
if request.method == 'POST':
form_type = request.POST['form_type']
roles = Role.objects.filter(team=team, role=form_type)
......@@ -98,9 +104,23 @@ def team(request, team_slug):
translate_roles = Role.objects.filter(team=team, role='translator')
if translate_roles:
mem_groups[2]['form'] = EditMemberRoleForm(translate_roles)
context['can_edit_team'] = True
context['mem_groups'] = mem_groups
return render_to_response('teams/team_detail.html', context, context_instance=RequestContext(request))
def team_edit(request, team_slug):
team = get_object_or_404(Team, name=team_slug)
if not team.can_edit(request.user):
return HttpResponseForbidden("You are not allowed to edit this team.")
form = EditTeamDetailsForm(request.POST or None, instance=team)
if request.method == 'POST':
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('team_slug', args=[team_slug]))
context = {
'pageSection': 'teams',
'team': team,
'mem_groups': mem_groups
'form': form
}
return render_to_response('teams/team_detail.html', context, context_instance=RequestContext(request))
return render_to_response('teams/team_edit.html', context, context_instance=RequestContext(request))
......@@ -80,7 +80,7 @@ $(document).ready(function() {
<a href="#{{ branch.name }}">{{ branch.name }}</a>
{% endfor %}
{% if can_edit_branches %}
<a href="{% url stats.views.module_edit_branches module_name=module.name %}"><img src="/media/img/edit.png" alt="Edit" /></a>
<a href="{% url stats.views.module_edit_branches module_name=module.name %}"><img src="{{ MEDIA_URL }}img/edit.png" alt="Edit" /></a>
{% endif %}
</p>
......
......@@ -3,7 +3,12 @@
{% with team.get_description as lang %}
<table width="100%"><tr><td valign="top" width="50%">
<h2>{% trans "Details" %}</h2>
<h2>{% trans "Details" %}
{% if can_edit_team %}<a href="{% url team_edit team.name %}">
<img src="{{ MEDIA_URL }}img/edit_small.png" alt="Edit"/></a>
{% endif %}
</h2>
{% if team.webpage_url %}
<strong>{% blocktrans %}{{ lang }} Translation Team Page:{% endblocktrans %}</strong>
......
{% extends "base.html" %}
{% load i18n %}
{% block title %}{{ team.get_description }}{% endblock %}
{% block content %}
<div class="mainpage">
<h1>{{ team.get_description }}</h1>
<form action="#" method="POST">
<table>
{{ form.as_table }}
<tr><td colspan="2" align="right"><input type="submit" value="{% trans 'Save' %}"/></td></tr>
</table>
</form>
</div>
{% endblock %}
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