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

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

adds a few of logging

  • 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
12from django.conf import settings
13import logging
14
15# Get an instance of a logger
16logger = logging.getLogger(__name__)
17
18def error_handle(request, context):
19    form = LoginForm()
20    context['form'] = form
21    context.update(csrf(request))
22    return render_to_response('login.html', context)
23
24def my_login(request):       
25    context = {}
26    if request.method == 'POST': # If the form has been submitted...
27        form = LoginForm(request.POST) # A form bound to the POST data
28        context['form'] = form
29        if form.is_valid(): # All validation rules pass
30            username = request.POST['username']
31            password = request.POST['password']
32            user = authenticate(username=username, password=password)           
33            if user is not None:
34                if user.is_active:
35                    remote_ip = _calculate_remote_ip(request)
36                    # Redirect to a success page.
37                    login(request, user)             
38                    token = AuthTicket(
39                        getattr(settings, 'SHARED_SECRET', 'sharedsecret'),
40                        username,
41                        remote_ip)
42                    res = HttpResponseRedirect(request.GET.get('r', 'accounts/profile/'))
43                    res.set_cookie('auth_tkt', token.cookie_value())
44                    logger.info("Logging %s from %s" % (username, remote_ip))
45                    return res
46                else:
47                    # Return a 'disabled account' error message
48                    context['error'] = u'account disabled'
49                    return error_handle(request, context)
50            else:
51                # Return an 'invalid login' error message.
52                context['error'] = u'invalid login'               
53                return error_handle(request, context)
54        else:
55            context['error'] = u'form is invalid'
56            return error_handle(request, context)
57    else:
58        # An unbound form
59        form = LoginForm()
60        context['form'] = form
61        context.update(csrf(request))
62        return render_to_response('login.html', context)
63
64def _calculate_remote_ip(request):
65    import urlparse
66    import socket
67    remote_url = urlparse.urlparse(request.GET.get('r', ''))
68    if remote_url.netloc != '':
69        return socket.gethostbyname_ex(remote_url.netloc).split(':')[0][2][1]
70    return '127.0.0.1'
Note: See TracBrowser for help on using the repository browser.