views.py 3.91 KB
Newer Older
1
from django.core.urlresolvers import reverse
2
from django.http import HttpResponseRedirect, HttpResponseForbidden
3
from django.shortcuts import render, get_object_or_404
4 5
from django.utils.translation import ugettext_lazy as _

6
from common import utils
7
from teams.models import Team, FakeTeam, Role
8
from teams.forms import EditMemberRoleForm, EditTeamDetailsForm
9 10
from languages.models import Language

11
def teams(request, format='html'):
12
    teams = Team.objects.all_with_coordinator()
13
    format = request.GET.get('format') or format
14 15
    if format in ('xml', 'json'):
        return render(request, 'teams/team_list.%s' % format, {'teams': teams},
16 17
            content_type=utils.MIME_TYPES[format]
        )
18 19 20
    else:
        context = {
            'pageSection': 'teams',
21
            'teams': utils.trans_sort_object_list(teams, 'description'),
22
        }
23
        return render(request, 'teams/team_list.html', context)
24 25 26 27

def team(request, team_slug):
    try:
        team = Team.objects.get(name=team_slug)
28
        mem_groups = (
29 30
               {'id': 'committers',
                'title': _("Committers"),
31
                'members': team.get_committers_exact(),
32
                'form': None,
33
                'no_member': _("No committers")
34
               },
35 36
               {'id': 'reviewers',
                'title': _("Reviewers"),
37
                'members': team.get_reviewers_exact(),
38 39 40
                'form': None,
                'no_member': _("No reviewers")
               },
41 42
               {'id': 'translators',
                'title': _("Translators"),
43
                'members': team.get_translators_exact(),
44 45 46
                'form': None,
                'no_member': _("No translators")
               },
47 48
               {'id': 'inactive',
                'title': _("Inactive members"),
49 50 51 52
                'members': team.get_inactive_members(),
                'form': None,
                'no_member': _("No inactive members")
               },
53 54 55 56 57
        )
    except Team.DoesNotExist:
        lang = get_object_or_404(Language, locale=team_slug)
        team = FakeTeam(lang)
        mem_groups = ()
58

59 60 61 62 63 64
    context = {
        'pageSection': 'teams',
        'team': team,
        'can_edit_team': False,
    }
    if team.can_edit(request.user):
65 66
        if request.method == 'POST':
            form_type = request.POST['form_type']
67
            roles = Role.objects.filter(team=team, role=form_type, is_active=True)
68 69
            form = EditMemberRoleForm(roles, request.POST)
            if form.is_valid():
70
                form.save(request)
71
        # Create forms for template
72
        commit_roles = Role.objects.filter(team=team, role='committer', is_active=True)
73 74
        if commit_roles:
            mem_groups[0]['form'] = EditMemberRoleForm(commit_roles)
75
        review_roles = Role.objects.filter(team=team, role='reviewer', is_active=True)
76 77
        if review_roles:
            mem_groups[1]['form'] = EditMemberRoleForm(review_roles)
78
        translate_roles = Role.objects.filter(team=team, role='translator', is_active=True)
79 80
        if translate_roles:
            mem_groups[2]['form'] = EditMemberRoleForm(translate_roles)
81 82 83
        context['can_edit_team'] = True

    context['mem_groups'] = mem_groups
84
    context['can_edit_details'] = context['can_edit_team'] or utils.is_site_admin(request.user)
85
    return render(request, 'teams/team_detail.html', context)
86 87 88

def team_edit(request, team_slug):
    team = get_object_or_404(Team, name=team_slug)
89
    if not (team.can_edit(request.user) or utils.is_site_admin(request.user)):
90
        return HttpResponseForbidden("You are not allowed to edit this team.")
91
    form = EditTeamDetailsForm(request.user, request.POST or None, instance=team)
92 93 94 95
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('team_slug', args=[team_slug]))
96
    context = {
97
        'team': team,
98
        'form': form
99
    }
100
    return render(request, 'teams/team_edit.html', context)