myslice/config becomes myslice/configengine to avoid confusion
[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.configengine import ConfigEngine
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         return { 
19                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
20                  }
21
22     def post (self,request):
23         env = self.default_env()
24         username = request.POST.get('username')
25         password = request.POST.get('password')
26         
27         # pass request within the token, so manifold session key can be attached to the request session.
28         token = {'username': username, 'password': password, 'request': request}    
29
30         # our authenticate function returns either
31         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
32         # . a django User in case of success
33         # . or None if the backend could be reached but the authentication failed
34         auth_result = authenticate(token=token)
35         # use one or two columns for the layout - not logged in users will see the login prompt
36         # high-level errors, like connection refused or the like
37         if isinstance (auth_result, ManifoldResult):
38             manifoldresult = auth_result
39             # let's use ManifoldResult.__repr__
40             env['state']="%s"%manifoldresult
41             env['layout_1_or_2']="layout-unfold2.html"
42             return render_to_response('home-view.html',env, context_instance=RequestContext(request))
43         # user was authenticated at the backend
44         elif auth_result is not None:
45             user=auth_result
46             if user.is_active:
47                 print "LOGGING IN"
48                 login(request, user)
49                 return HttpResponseRedirect ('/login-ok')
50             else:
51                 env['state'] = "Your account is not active, please contact the site admin."
52                 env['layout_1_or_2']="layout-unfold2.html"
53                 return render_to_response('home-view.html',env, context_instance=RequestContext(request))
54         # otherwise
55         else:
56             env['state'] = "Your username and/or password were incorrect."
57             env['layout_1_or_2']="layout-unfold2.html"
58             return render_to_response('home-view.html',env, context_instance=RequestContext(request))
59
60     # login-ok sets state="Welcome to MySlice" in urls.py
61     def get (self, request, state=None):
62         env = self.default_env()
63         env['username']=the_user(request)
64         env['topmenu_items'] = topmenu_items(None, request)
65         if state: env['state'] = state
66         elif not env['username']: env['state'] = "Please sign in"
67         # use one or two columns for the layout - not logged in users will see the login prompt
68         env['layout_1_or_2']="layout-unfold2.html" if not env['username'] else "layout-unfold1.html"
69         return render_to_response('home-view.html',env, context_instance=RequestContext(request))
70