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