smoother handling of when backend cannot be reached at all
[myslice.git] / portal / homeview.py
1 # this somehow is not used anymore - should it not be ?
2 from django.views.generic import View
3 from django.core.context_processors import csrf
4 from django.http import HttpResponseRedirect
5 from django.contrib.auth import authenticate, login, logout
6 from django.template import RequestContext
7 from django.shortcuts import render_to_response
8
9 from manifold.manifoldresult import ManifoldResult
10 from myslice.viewutils import topmenu_items, the_user
11 from myslice.config import Config
12
13 class HomeView (View):
14
15     def default_env (self):
16         return { 
17                  'manifold_url':Config.manifold_url,
18                  }
19
20     def post (self,request):
21         env = self.default_env()
22         username = request.POST.get('username')
23         password = request.POST.get('password')
24         
25         # pass request within the token, so manifold session key can be attached to the request session.
26         token = {'username': username, 'password': password, 'request': request}    
27
28         # our authenticate function returns either
29         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
30         # . a django User in case of success
31         # . or None if the backend could be reached but the authentication failed
32         auth_result = authenticate(token=token)
33         # high-level errors, like connection refused or the like
34         if isinstance (auth_result, ManifoldResult):
35             manifoldresult = auth_result
36             # let's use ManifoldResult.__repr__
37             env['state']="%s"%manifoldresult
38             return render_to_response('home-view.html',env, context_instance=RequestContext(request))
39         # user was authenticated at the backend
40         elif auth_result is not None:
41             user=auth_result
42             if user.is_active:
43                 print "LOGGING IN"
44                 login(request, user)
45                 return HttpResponseRedirect ('/login-ok')
46             else:
47                 env['state'] = "Your account is not active, please contact the site admin."
48                 return render_to_response('home-view.html',env, context_instance=RequestContext(request))
49         # otherwise
50         else:
51             env['state'] = "Your username and/or password were incorrect."
52             return render_to_response('home-view.html',env, context_instance=RequestContext(request))
53
54     # login-ok sets state="Welcome to MySlice" in urls.py
55     def get (self, request, state=None):
56         env = self.default_env()
57         env['username']=the_user(request)
58         env['topmenu_items'] = topmenu_items('', request)
59         if state: env['state'] = state
60         elif not env['username']: env['state'] = "Please sign in"
61         return render_to_response('home-view.html',env, context_instance=RequestContext(request))
62