views.py 3.96 KB
Newer Older
1
from django.conf import settings
Claude Paroz's avatar
Claude Paroz committed
2
from django.contrib.auth import login, authenticate
3
from django.contrib import messages
4
from django.http import HttpResponseRedirect, Http404
5
from django.shortcuts import render
6
from django.template.loader import get_template, TemplateDoesNotExist
7
from django.urls import reverse
8
from django.utils.http import is_safe_url
9 10
from django.utils.translation import ugettext as _

11
from people.models import Person, obfuscate_email
12
from teams.models import Role
13
from people.forms import LoginForm, RegistrationForm
14
from common.utils import get_user_locale
15

16

17
def index(request):
18
    """ Homepage view """
19 20 21 22 23 24 25 26
    curlang = get_user_locale(request)

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

27

28
def about(request):
29
    translator_credits = _("translator-credits")
30
    if translator_credits == "translator-credits":
31 32
        translator_credits = ''
    else:
33
        translator_credits = [obfuscate_email(line) for line in translator_credits.split('\n')]
34 35
    context = {
        'pageSection': 'home',
36
        'translator_credits': translator_credits,
37
    }
38
    return render(request, 'about.html', context)
39

40

41
def site_login(request):
42
    """ Site-specific login page. Not named 'login' to not confuse with auth.login """
43 44 45 46 47 48 49
    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)
50
    openid_path = ''
51
    login_form = LoginForm(request, data=request.POST)
52
    if request.method == 'POST':
53 54 55
        if request.POST.get('referer', None):
            referer = request.POST['referer']

56 57 58 59 60 61 62 63 64 65 66 67
        if login_form.is_valid():
            user = login_form.get_user()
            login(request, user)
            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>.") % {
                    'url': reverse('person_team_join'),
                }
                messages.info(request, message)
            return redirect(referer)
        else:
            messages.error(request, _("Login unsuccessful. Please verify your username and password."))
68

69 70
    if 'django_openid_auth' in settings.INSTALLED_APPS:
        openid_path = '/openid/login/'
71 72 73 74
    context = {
        'pageSection': 'home',
        'openid_path': openid_path,
        'referer': referer,
75
        'next': referer,
76
        'form': login_form,
77
    }
78
    return render(request, 'login.html', context)
79

80

81 82 83 84 85
def site_register(request):
    openid_path = ''
    if request.method == 'POST':
        form = RegistrationForm(data = request.POST)
        if form.is_valid():
86
            form.save(request)
87 88 89
            return HttpResponseRedirect(reverse('register_success'))
    else:
        form = RegistrationForm()
90
    if 'django_openid_auth' in settings.INSTALLED_APPS:
91 92 93 94 95 96
        openid_path = '/openid/'
    context = {
        'pageSection': 'home',
        'form': form,
        'openid_path': openid_path,
    }
97
    return render(request, 'registration/register.html', context)
98 99 100 101 102 103

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

109
def help(request, topic, modal):
110 111
    template = 'help/%s.html' % topic
    try:
112
        get_template(template)
113 114
    except TemplateDoesNotExist:
        raise Http404
115 116 117
    return render(request, template, {
        'base': 'base_modal.html' if modal and int(modal) else 'base.html'
    })