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

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

Share project "dj_security" into " http://proj.badc.rl.ac.uk/svn/ndg/mauRepo"

  • 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
12
13def errorHandle(request, context):
14    form = LoginForm()
15    context['form'] = form
16    context.update(csrf(request))
17    return render_to_response('login.html', context)
18
19def my_login(request):       
20    context = {}
21    if request.method == 'POST': # If the form has been submitted...
22        form = LoginForm(request.POST) # A form bound to the POST data
23        context['form'] = form
24        if form.is_valid(): # All validation rules pass
25            username = request.POST['username']
26            password = request.POST['password']
27            user = authenticate(username=username, password=password)
28            if user is not None:
29                if user.is_active:
30                    # Redirect to a success page.
31                    login(request, user)
32                    #return render_to_response('logged_in.html', {
33                    #    'username': username,
34                    #})
35                   
36                    token = AuthTicket('sharedsecret', username, request.META['REMOTE_ADDR'])
37                    #self.request().environ()['REMOTE_ADDR'], tokens=['admin'])
38                    #self.response().setCookie('auth_tkt', token.cookie_value())
39                    res = HttpResponseRedirect(request.GET.get('r', 'accounts/profile/'))
40                    res.set_cookie('auth_tkt', token.cookie_value())
41                    return res
42                    #return HttpResponseRedirect('accounts/profile/')
43                else:
44                    # Return a 'disabled account' error message
45                    context['error'] = u'account disabled'
46                    return errorHandle(request, context)
47            else:
48                 # Return an 'invalid login' error message.
49                context['error'] = u'invalid login'               
50                return errorHandle(request, context)
51        else:
52            context['error'] = u'form is invalid'
53            return errorHandle(request, context)
54    else:
55        form = LoginForm() # An unbound form
56        context['form'] = form
57        context.update(csrf(request))
58        return render_to_response('login.html', context)
Note: See TracBrowser for help on using the repository browser.