source: mauRepo/dj_security/trunk/dj_security/views/my_view.py @ 8667

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/dj_security/trunk/dj_security/views/my_view.py@8667
Revision 8667, 2.2 KB checked in by mnagni, 7 years ago (diff)

FIxes uncorrect context_processor definition

  • Property svn:mime-type set to text/plain
Line 
1'''
2Created on 29 Oct 2012
3
4@author: mnagni
5'''
6from django.contrib.auth import authenticate, login
7from django.shortcuts import render_to_response
8from dj_security.login_form import LoginForm
9from django.core.context_processors import csrf
10from django.http import HttpResponseRedirect
11from paste.auth.auth_tkt import AuthTicket
12from django.conf import settings
13
14def error_handle(request, context):
15    form = LoginForm()
16    context['form'] = form
17    context.update(csrf(request))
18    return render_to_response('login.html', context)
19
20def my_login(request):       
21    context = {}
22    if request.method == 'POST': # If the form has been submitted...
23        form = LoginForm(request.POST) # A form bound to the POST data
24        context['form'] = form
25        if form.is_valid(): # All validation rules pass
26            username = request.POST['username']
27            password = request.POST['password']
28            user = authenticate(username=username, password=password)
29            if user is not None:
30                if user.is_active:
31                    # Redirect to a success page.
32                    login(request, user)                   
33                    token = AuthTicket(
34                        getattr(settings, 'SHARED_SECRET', 'sharedsecret'),
35                        username,
36                        request.META['REMOTE_ADDR'])
37                    res = HttpResponseRedirect(request.GET.get('r', 'accounts/profile/'))
38                    res.set_cookie('auth_tkt', token.cookie_value())
39                    return res
40                else:
41                    # Return a 'disabled account' error message
42                    context['error'] = u'account disabled'
43                    return error_handle(request, context)
44            else:
45                # Return an 'invalid login' error message.
46                context['error'] = u'invalid login'               
47                return error_handle(request, context)
48        else:
49            context['error'] = u'form is invalid'
50            return error_handle(request, context)
51    else:
52        # An unbound form
53        form = LoginForm()
54        context['form'] = form
55        context.update(csrf(request))
56        return render_to_response('login.html', context)
Note: See TracBrowser for help on using the repository browser.