views.py 5.26 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
from django.utils.http import is_safe_url
28 29
from django.utils.translation import ugettext as _

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

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

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

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

57
def site_login(request):
58
    """ Site-specific login page. Not named 'login' to not confuse with auth.login """
59 60 61 62 63 64 65
    def redirect(referer):
        if is_safe_url(referer, host=request.get_host()):
            return HttpResponseRedirect(referer)
        else:
            return HttpResponseRedirect(reverse("home"))

    referer = request.META.get('HTTP_REFERER', None)
66 67
    openid_path = ''
    if request.method == 'POST':
68 69 70
        if request.POST.get('referer', None):
            referer = request.POST['referer']

71
        if 'logout' in request.POST and request.POST['logout']:
72
            logout(request)
73
            messages.success(request, _("You have been logged out."))
74
            return redirect(referer)
75
        elif 'username' in request.POST:
76 77 78 79 80 81
            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)
82
                    messages.success(request, _("You have been successfully logged in."))
83 84
                    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>.") % {
85
                            'url': reverse('person_team_join'),
86
                        }
87
                        messages.info(request, message)
88
                    return redirect(referer)
89
                else:
90
                    messages.error(request, _("We're sorry, but your account has been disabled."))
91
            else:
92
                messages.error(request, _("Login unsuccessful. Please verify your username and password."))
93

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

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

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:
127
        return render(request, 'error.html', {'error': _("Sorry, the key you provided is not valid.")})
128
    person.activate()
129 130
    messages.success(request, _("Your account has been activated."))
    return site_login(request)
131

132
def help(request, topic, modal):
133 134 135 136 137
    template = 'help/%s.html' % topic
    try:
        t = get_template(template)
    except TemplateDoesNotExist:
        raise Http404
138 139 140
    return render(request, template, {
        'base': 'base_modal.html' if modal and int(modal) else 'base.html'
    })