source: mauRepo/dj_security/trunk/dj_security/views/dj_security_login.py @ 8686

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

Fixes a blocking bug as a function name was not correctly refactored

  • 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
7from django.shortcuts import render_to_response
8from dj_security.login_form import LoginForm
9from django.core.context_processors import csrf
10from django.conf import settings
11import logging
12from dj_security.encoder import SecurityEncoder
13
14# Get an instance of a logger
15LOGGER = logging.getLogger(__name__)
16
17def error_handle(request, context):
18    form = LoginForm()
19    context['form'] = form
20    context.update(csrf(request))
21    return render_to_response('login.html', context)
22
23def login(request):       
24    context = {}
25    if request.method == 'POST': # If the form has been submitted...
26        form = LoginForm(request.POST) # A form bound to the POST data
27        context['form'] = form
28        if form.is_valid(): # All validation rules pass
29            username = request.POST['username']
30            password = request.POST['password']
31            user = authenticate(username=username, password=password)             
32            if user is not None:
33                if user.is_active:                   
34                    context['user'] = SecurityEncoder().encode(user)
35                    request.auth_user = context['user']
36                    return _encode_authenticated_response(request, context)         
37                else:
38                    # Return a 'disabled account' error message
39                    context['error'] = u'account disabled'
40                    return error_handle(request, context)
41            else:
42                # Return an 'invalid login' error message.
43                context['error'] = u'invalid login'               
44                return error_handle(request, context)
45        else:
46            context['error'] = u'form is invalid'
47            return error_handle(request, context)
48    else:
49        if getattr(settings, "FAKE_AUTHENTICATION", False):
50            context['user'] = '{"username": "mnagni", '
51            '"first_name": "Maurizio", "last_name": "Nagni", '
52            '"is_active": true, "email": "maurizio.nagni@stfc.ac.uk", '
53            '"is_superuser": false, "is_staff": true, '
54            '"last_login": "2012-10-18 11:05:28.700139+00:00", '
55            '"date_joined": "2012-03-22 14:20:56+00:00", "id": 29, '
56            '"permissions": "auth.add_user,proginfo.add_dataproduct, '
57            'proginfo.change_programme, cedainfoapp.delete_vmrequest"}'
58            return _encode_authenticated_response(request, context) 
59       
60        # An unbound form
61        form = LoginForm()
62        context['form'] = form
63        context.update(csrf(request))
64        return render_to_response('login.html', context)
65   
66def _encode_authenticated_response(request, context):
67    redirect_parameter = getattr(settings, 'REDIRECT_URL', 'r')
68    context['redirect_url'] = request.GET.get(redirect_parameter, '')
69    return render_to_response('logged_in.html', context)
Note: See TracBrowser for help on using the repository browser.