views.py 5.17 KB
Newer Older
1 2 3 4
from operator import itemgetter

from django.conf import settings
from django.conf.locale import LANG_INFO
5 6
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm
7
from django.contrib import messages
8
from django.contrib.sites.shortcuts import get_current_site
9 10 11
from django.core import urlresolvers
from django.db import IntegrityError
from django.http import HttpResponseRedirect
12
from django.shortcuts import render, get_object_or_404
13
from django.utils.translation import ugettext_lazy, ugettext as _
14
from django.views.generic import ListView, DetailView, UpdateView
15

16
from common.utils import lc_sorted
17
from people.models import Person
18
from teams.models import Team, Role
19
from people.forms import TeamJoinForm, DetailForm
20
from vertimus.models import State
21

22 23 24 25 26

class PeopleListView(ListView):
    model = Person

    def get_context_data(self, **kwargs):
Claude Paroz's avatar
Claude Paroz committed
27
        context = super().get_context_data(**kwargs)
28 29 30
        context['pageSection'] = "teams"
        return context

31 32 33 34
class PersonDetailView(DetailView):
    model = Person
    slug_field = 'username'
    context_object_name = 'person'
35

36
    def get_context_data(self, **kwargs):
Claude Paroz's avatar
Claude Paroz committed
37
        context = super().get_context_data(**kwargs)
38
        states = State.objects.filter(action__person=self.object).distinct()
39
        all_languages = [(lg[0], LANG_INFO.get(lg[0], {'name_local': lg[1]})['name_local']) for lg in settings.LANGUAGES]
40
        all_languages = lc_sorted(all_languages, key=itemgetter(1))
41 42 43 44
        context.update({
            'pageSection': "teams",
            'all_languages': all_languages,
            'on_own_page': self.request.user.is_authenticated() and self.object.username == self.request.user.username,
45
            'states': [(s, s.stats) for s in states],
46 47
        })
        return context
48

49 50 51 52 53 54 55 56
class PersonEditView(UpdateView):
    model = Person
    slug_field = 'username'
    form_class = DetailForm
    template_name = 'people/person_detail_change_form.html'

    def get_object(self):
        self.kwargs['slug'] = self.request.user.username
Claude Paroz's avatar
Claude Paroz committed
57
        return super().get_object()
58 59

    def get_context_data(self, **kwargs):
Claude Paroz's avatar
Claude Paroz committed
60
        context = super().get_context_data(**kwargs)
61 62 63
        context['pageSection'] = "teams"
        context['on_own_page'] = self.object.username == self.request.user.username,
        return context
64

65 66
    def form_invalid(self, form):
        messages.error(self.request, _("Sorry, the form is not valid."))
Claude Paroz's avatar
Claude Paroz committed
67
        return super().form_invalid(form)
68

69 70 71 72 73 74 75 76
@login_required
def person_team_join(request):
    """Handle the form to join a team"""
    person = get_object_or_404(Person, username=request.user.username)
    if request.method == 'POST':
        form = TeamJoinForm(request.POST)
        if form.is_valid():
            team = form.cleaned_data['teams']
77 78
            # Role default to 'translator'
            new_role = Role(team=team, person=person)
79 80
            try:
                new_role.save()
81
                messages.success(request, _("You have successfully joined the team “%s”.") % team.get_description())
82 83
                team.send_mail_to_coordinator(subject=ugettext_lazy("A new person joined your team"),
                                              message=ugettext_lazy("%(name)s has just joined your translation team on %(site)s"),
84
                                              messagekw = {'name': person.name, 'site': get_current_site(request)})
85
            except IntegrityError:
86
                messages.info(request, _("You are already member of this team."))
87 88 89 90 91 92
    else:
        form = TeamJoinForm()

    context = {
        'pageSection': "teams",
        'person': person,
93
        'on_own_page': person.username == request.user.username,
94 95
        'form': form,
    }
96
    return render(request, 'people/person_team_join_form.html', context)
97

98 99 100 101 102 103 104
@login_required
def person_team_leave(request, team_slug):
    person = get_object_or_404(Person, username=request.user.username)
    team = get_object_or_404(Team, name=team_slug)
    try:
        role = Role.objects.get(team=team, person=person)
        role.delete()
105
        messages.success(request, _("You have been removed from the team “%s”.") % team.get_description())
106 107
    except Role.DoesNotExist:
        # Message no i18n'ed, should never happen under normal conditions
108
        messages.error(request, _("You are not a member of this team."))
109
    # redirect to normal person detail
110
    return HttpResponseRedirect(urlresolvers.reverse('person_detail_username',
111
                                                     args=(person.username,)))
112 113 114 115 116 117 118 119 120

@login_required
def person_password_change(request):
    """Handle the generic form to change the password"""
    person = get_object_or_404(Person, username=request.user.username)

    if request.method == 'POST':
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
121
            messages.success(request, _("Your password has been changed."))
122 123 124 125 126 127 128
            form.save()
    else:
        form = PasswordChangeForm(request.user)

    context = {
        'pageSection': "teams",
        'person': person,
129
        'on_own_page': person.username == request.user.username,
130 131
        'form': form,
    }
132
    return render(request, 'people/person_password_change_form.html', context)