Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into fibre
[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 from django.shortcuts import render
8
9 from unfold.loginrequired import FreeAccessView
10
11 from manifoldapi.manifoldresult import ManifoldResult
12 from ui.topmenu import topmenu_items, the_user
13 from myslice.configengine import ConfigEngine
14
15 from myslice.theme import ThemeView
16
17 class HomeView (FreeAccessView, ThemeView):
18     template_name = 'home-view.html'
19         
20     # expose this so we can mention the backend URL on the welcome page
21     def default_env (self):
22         return { 
23                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
24                  }
25
26     def post (self,request):
27         env = self.default_env()
28         env['theme'] = self.theme
29         env['section'] = "Dashboard"
30         
31         username = request.POST.get('username')
32         password = request.POST.get('password')
33        
34         # LDAP form - If FIBRE, then get the possibilite to authenticate using usernameldap
35         #if self.theme == 'fibre':
36         usernameldap = request.POST.get('usernameldap')
37         token = {'usernameldap': usernameldap, 'username': username ,'password': password, 'request': request}    
38         #else:
39         
40         # Follow original code
41         ## pass request within the token, so manifold session key can be attached to the request session.
42         #token = {'username': username, 'password': password, 'request': request}    
43
44         # our authenticate function returns either
45         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
46         # . a django User in case of success
47         # . or None if the backend could be reached but the authentication failed
48         auth_result = authenticate(token=token)
49         # use one or two columns for the layout - not logged in users will see the login prompt
50         # high-level errors, like connection refused or the like
51         if isinstance (auth_result, ManifoldResult):
52             manifoldresult = auth_result
53             # let's use ManifoldResult.__repr__
54             env['state']="%s"%manifoldresult
55             
56             return render_to_response(self.template,env, context_instance=RequestContext(request))
57         # user was authenticated at the backend
58         elif auth_result is not None:
59             user=auth_result
60             if user.is_active:
61                 print "LOGGING IN"
62                 login(request, user)
63                 
64                 if request.user.is_authenticated(): 
65                     env['person'] = self.request.user
66                     env['username'] = self.request.user
67                 else: 
68                     env['person'] = None
69                 return render_to_response(self.template,env, context_instance=RequestContext(request))
70             else:
71                 env['state'] = "Your account is not active, please contact the site admin."
72                 env['layout_1_or_2']="layout-unfold2.html"
73                 
74                 return render_to_response(self.template,env, context_instance=RequestContext(request))
75         # otherwise
76         else:
77             env['state'] = "Your username and/or password were incorrect."
78             
79             return render_to_response(self.template, env, context_instance=RequestContext(request))
80
81     def get (self, request, state=None):
82         env = self.default_env()
83
84         if request.user.is_authenticated(): 
85             env['person'] = self.request.user
86         else: 
87             env['person'] = None
88     
89         env['theme'] = self.theme
90         env['section'] = "Dashboard"
91
92         env['username']=the_user(request)
93         env['topmenu_items'] = topmenu_items(None, request)
94         if state: env['state'] = state
95         elif not env['username']: env['state'] = None
96         # use one or two columns for the layout - not logged in users will see the login prompt
97         
98         
99         return render_to_response(self.template, env, context_instance=RequestContext(request))
100