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

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

corrects the previous (wrong) workflow

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