views.py 5.01 KB
Newer Older
1 2
# -*- coding: utf-8 -*-
#
3
# Copyright (c) 2008-2011 Claude Paroz <claude@2xlibre.net>.
4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# This file is part of Damned Lies.
#
# Damned Lies is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Damned Lies is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, see <http://www.gnu.org/licenses/>.
19

20
from django.conf import settings
21
from django.contrib.auth import login, authenticate, logout
22
from django.contrib import messages
23
from django.core.urlresolvers import reverse
24
from django.http import HttpResponseRedirect, Http404
25
from django.shortcuts import render
26
from django.template.loader import get_template, TemplateDoesNotExist
27 28
from django.utils.translation import ugettext as _

29
from people.models import Person, obfuscate_email
30
from teams.models import Role
31
from people.forms import RegistrationForm
32
from common.utils import get_user_locale
33 34

def index(request):
35
    """ Homepage view """
36 37 38 39 40 41 42 43 44
    curlang = get_user_locale(request)

    context = {
        'pageSection': 'home',
        'user_language': curlang,
    }
    return render(request, 'index.html', context)

def about(request):
45
    translator_credits = _("translator-credits")
46
    if translator_credits == "translator-credits":
47 48
        translator_credits = ''
    else:
49
        translator_credits = [obfuscate_email(line) for line in translator_credits.split('\n')]
50 51
    context = {
        'pageSection': 'home',
52
        'translator_credits': translator_credits,
53
    }
54
    return render(request, 'about.html', context)
55

56
def site_login(request):
57 58 59 60
    """ Site-specific login page. Not named 'login' to not confuse with auth.login """
    referer = None
    openid_path = ''
    if request.method == 'POST':
61
        if 'logout' in request.POST and request.POST['logout']:
62
            logout(request)
63
            messages.success(request, _("You have been logged out."))
64
        elif 'username' in request.POST:
65 66 67 68 69 70
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
71
                    messages.success(request, _("You have been successfully logged in."))
72 73
                    if Role.objects.filter(person__username=user.username).count() < 1:
                        message = _("You have not joined any translation team yet. You can do it from <a href=\"%(url)s\">your profile</a>.") % {
74
                            'url': reverse('person_team_join'),
75
                        }
76
                        messages.info(request, message)
77
                    if 'referer' in request.POST:
78 79 80 81
                        return HttpResponseRedirect(request.POST['referer'])
                    else:
                        return HttpResponseRedirect(reverse("home"))
                else:
82
                    messages.error(request, _("We're sorry, but your account has been disabled."))
83
            else:
84
                messages.error(request, _("Login unsuccessful. Please verify your username and password."))
85 86 87
    else:
        referer = request.META.get('HTTP_REFERER', None)

88 89
    if 'django_openid_auth' in settings.INSTALLED_APPS:
        openid_path = '/openid/login/'
90 91 92 93
    context = {
        'pageSection': 'home',
        'openid_path': openid_path,
        'referer': referer,
94
        'next': referer,
95
    }
96
    return render(request, 'login.html', context)
97 98 99 100 101 102 103 104 105 106

def site_register(request):
    openid_path = ''
    if request.method == 'POST':
        form = RegistrationForm(data = request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect(reverse('register_success'))
    else:
        form = RegistrationForm()
107
    if 'django_openid_auth' in settings.INSTALLED_APPS:
108 109 110 111 112 113
        openid_path = '/openid/'
    context = {
        'pageSection': 'home',
        'form': form,
        'openid_path': openid_path,
    }
114
    return render(request, 'registration/register.html', context)
115 116 117 118 119 120

def activate_account(request, key):
    """ Activate an account through the link a requestor has received by email """
    try:
        person = Person.objects.get(activation_key=key)
    except Person.DoesNotExist:
121
        return render(request, 'error.html', {'error': _("Sorry, the key you provided is not valid.")})
122
    person.activate()
123 124
    messages.success(request, _("Your account has been activated."))
    return site_login(request)
125 126 127 128 129 130 131 132

def help(request, topic):
    template = 'help/%s.html' % topic
    try:
        t = get_template(template)
    except TemplateDoesNotExist:
        raise Http404
    return render(request, template, {})