Merge branch 'master' of ssh://git.onelab.eu/git/myslice-django
[myslice.git] / auth / views.py
1 # Create your views here.
2 from django.core.context_processors import csrf
3 from django.template import RequestContext
4 from django.shortcuts import render_to_response
5 from django.contrib.auth import authenticate, login, logout
6 from django.http import HttpResponseRedirect
7
8 from auth.backend import MyCustomBackend
9
10 from myslice.viewutils import the_user
11 from myslice.config import Config
12
13 def login_user(request):
14     state = "Please log in below..."
15     username = password = ''
16     env={'hard_wired_users':MyCustomBackend.hard_wired_users,
17          'manifold_url':Config.manifold_url(),
18          }
19
20     if request.POST:
21         username = request.POST.get('username')
22         password = request.POST.get('password')
23         
24         # pass request within the token, so manifold session key could be attached to the request session.
25         token = {'username': username, 'password': password, 'request': request}    
26
27         user = authenticate(token=token)
28         if user is not None:
29             if user.is_active:
30                 login(request, user)
31                 #state = "You're successfully logged in!"
32                 return HttpResponseRedirect ('/login-ok')
33             else:
34                 env['state'] = "Your account is not active, please contact the site admin."
35                 return render_to_response('view-login.html',env, context_instance=RequestContext(request))
36         else:
37             env['state'] = "Your username and/or password were incorrect."
38             return render_to_response('view-login.html',env, context_instance=RequestContext(request))
39     else:
40         state='Welcome to MySlice'
41         env['state']=state
42         env['username']=the_user(request)
43         return render_to_response('view-login.html',env, context_instance=RequestContext(request))
44
45 # hard question : where should we redirect requests to logout if user is not logged in ?
46 def logout_user (request):
47     # xxx check that we're indeed logged in
48     if not request.user.is_authenticated():
49         return HttpResponseRedirect ('/')
50     return render_to_response('view-logout.html',{'username':the_user(request)},
51                               context_instance=RequestContext(request))
52
53 def do_logout_user (request):
54     # xxx check that we're indeed logged in
55     if not request.user.is_authenticated():
56         return HttpResponseRedirect ('/')
57     logout(request)
58     return HttpResponseRedirect ('/')
59         
60