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

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

Corrects syntax erros

  • Property svn:mime-type set to text/plain
RevLine 
[8650]1'''
2Created on 29 Oct 2012
3
4@author: mnagni
5'''
[8680]6from django.contrib.auth import authenticate
[8650]7from django.shortcuts import render_to_response
8from dj_security.login_form import LoginForm
9from django.core.context_processors import csrf
[8678]10from django.conf import settings
[8673]11import logging
[8680]12from dj_security.encoder import SecurityEncoder
[8650]13
[8673]14# Get an instance of a logger
[8681]15LOGGER = logging.getLogger(__name__)
[8673]16
[8666]17def error_handle(request, context):
[8650]18    form = LoginForm()
19    context['form'] = form
20    context.update(csrf(request))
21    return render_to_response('login.html', context)
22
[8678]23def login(request):       
[8650]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
[8688]29            username = request.POST.get('username')
30            password = request.POST.get('password')
[8687]31            LOGGER.debug("logging: %s" % (username))           
[8680]32            user = authenticate(username=username, password=password)             
[8650]33            if user is not None:
[8687]34                LOGGER.debug("authenticated: %s" % (username))
35                if user.is_active:
36                    LOGGER.debug("user %s is Active" % (username))           
[8680]37                    context['user'] = SecurityEncoder().encode(user)
[8688]38                    request.auth_user = context.get('user')
[8686]39                    return _encode_authenticated_response(request, context)         
[8650]40                else:
41                    # Return a 'disabled account' error message
42                    context['error'] = u'account disabled'
[8687]43                    LOGGER.debug("Account Disabled for user %s" % (username))                   
[8666]44                    return error_handle(request, context)
[8650]45            else:
[8667]46                # Return an 'invalid login' error message.
[8650]47                context['error'] = u'invalid login'               
[8687]48                LOGGER.debug("Invalid login for user %s" % (username))               
[8666]49                return error_handle(request, context)
[8650]50        else:
51            context['error'] = u'form is invalid'
[8666]52            return error_handle(request, context)
[8650]53    else:
[8680]54        if getattr(settings, "FAKE_AUTHENTICATION", False):
55            context['user'] = '{"username": "mnagni", '
56            '"first_name": "Maurizio", "last_name": "Nagni", '
57            '"is_active": true, "email": "maurizio.nagni@stfc.ac.uk", '
58            '"is_superuser": false, "is_staff": true, '
59            '"last_login": "2012-10-18 11:05:28.700139+00:00", '
60            '"date_joined": "2012-03-22 14:20:56+00:00", "id": 29, '
61            '"permissions": "auth.add_user,proginfo.add_dataproduct, '
62            'proginfo.change_programme, cedainfoapp.delete_vmrequest"}'
[8681]63            return _encode_authenticated_response(request, context) 
[8680]64       
[8667]65        # An unbound form
66        form = LoginForm()
[8650]67        context['form'] = form
68        context.update(csrf(request))
[8680]69        return render_to_response('login.html', context)
70   
[8681]71def _encode_authenticated_response(request, context):
[8680]72    redirect_parameter = getattr(settings, 'REDIRECT_URL', 'r')
73    context['redirect_url'] = request.GET.get(redirect_parameter, '')
74    return render_to_response('logged_in.html', context)
Note: See TracBrowser for help on using the repository browser.