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

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

Introduced two optional configurable parameters: COOKIE_DOMAIN, REDIRECT_URL

  • 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.conf import settings
11import logging
12
13# Get an instance of a logger
14logger = logging.getLogger(__name__)
15
16def error_handle(request, context):
17    form = LoginForm()
18    context['form'] = form
19    context.update(csrf(request))
20    return render_to_response('login.html', context)
21
22def login(request):       
23    context = {}
24    if request.method == 'POST': # If the form has been submitted...
25        form = LoginForm(request.POST) # A form bound to the POST data
26        context['form'] = form
27        if form.is_valid(): # All validation rules pass
28            username = request.POST['username']
29            password = request.POST['password']
30            user = authenticate(username=username, password=password)           
31            if user is not None:
32                if user.is_active:
33                    login(request, user)
34                    redirect_parameter = getattr(settings, 'REDIRECT_URL', 'r')
35                    context['redirect_url'] = request.GET.get(redirect_parameter, '')
36                    return render_to_response('logged_in.html', 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        # An unbound form
50        form = LoginForm()
51        context['form'] = form
52        context.update(csrf(request))
53        return render_to_response('login.html', context)
Note: See TracBrowser for help on using the repository browser.