Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[unfold.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 manifold.core.query                import Query
12 from manifoldapi.manifoldapi            import execute_query
13
14 from manifoldapi.manifoldresult import ManifoldResult
15 from ui.topmenu import topmenu_items, the_user
16 from myslice.configengine import ConfigEngine
17
18 from myslice.theme import ThemeView
19
20 class HomeView (FreeAccessView, ThemeView):
21     template_name = 'home-view.html'
22         
23     # expose this so we can mention the backend URL on the welcome page
24     def default_env (self):
25         return { 
26                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
27                  }
28
29     def post (self,request):
30         env = self.default_env()
31         env['theme'] = self.theme
32         env['section'] = "Dashboard"
33         
34         username = request.POST.get('username')
35         password = request.POST.get('password')
36         
37         # pass request within the token, so manifold session key can be attached to the request session.
38         token = {'username': username, 'password': password, 'request': request}    
39
40         # our authenticate function returns either
41         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
42         # . a django User in case of success
43         # . or None if the backend could be reached but the authentication failed
44         auth_result = authenticate(token=token)
45         # use one or two columns for the layout - not logged in users will see the login prompt
46         # high-level errors, like connection refused or the like
47         if isinstance (auth_result, ManifoldResult):
48             manifoldresult = auth_result
49             # let's use ManifoldResult.__repr__
50             env['state']="%s"%manifoldresult
51             
52             return render_to_response(self.template,env, context_instance=RequestContext(request))
53         # user was authenticated at the backend
54         elif auth_result is not None:
55             user=auth_result
56             if user.is_active:
57                 print "LOGGING IN"
58                 login(request, user)
59                 
60                 if request.user.is_authenticated(): 
61                     env['person'] = self.request.user
62                     env['username'] = self.request.user
63                 else: 
64                     env['person'] = None
65                 return render_to_response(self.template,env, context_instance=RequestContext(request))
66             else:
67                 env['state'] = "Your account is not active, please contact the site admin."
68                 env['layout_1_or_2']="layout-unfold2.html"
69                 
70                 return render_to_response(self.template,env, context_instance=RequestContext(request))
71         # otherwise
72         else:
73             env['state'] = "Your username and/or password were incorrect."
74             
75             return render_to_response(self.template, env, context_instance=RequestContext(request))
76
77     def get (self, request, state=None):
78         env = self.default_env()
79
80         if request.user.is_authenticated(): 
81             env['person'] = self.request.user
82         else: 
83             env['person'] = None
84
85         env['theme'] = self.theme
86         env['section'] = "Dashboard"
87
88
89         env['username']=the_user(request)
90         env['topmenu_items'] = topmenu_items(None, request)
91         if state: env['state'] = state
92         elif not env['username']: env['state'] = None
93         # use one or two columns for the layout - not logged in users will see the login prompt
94         
95 #         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
96 #         account_details = execute_query(self.request, account_query)
97 #         for account_detail in account_details:
98 #             account_config = json.loads(account_detail['config'])
99 #             platform_name = platform_detail['platform']
100 #             if 'myslice' in platform_detail['platform']:
101 #                 acc_user_cred = account_config.get('delegated_user_credential','N/A')
102 #                 acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
103 #                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
104
105 #                 if 'N/A' not in acc_user_cred:
106 #                     exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
107 #                     if exp_date:
108 #                         user_exp_date = exp_date.group(1)
109 #                         user_cred_exp_list.append(user_exp_date)
110
111 #                     my_users = [{'cred_exp': t[0]}
112 #                         for t in zip(user_cred_exp_list)]
113 #                
114
115 #                 if 'N/A' not in acc_slice_cred:
116 #                     for key, value in acc_slice_cred.iteritems():
117 #                         slice_list.append(key)
118 #                         # get cred_exp date
119 #                         exp_date = re.search('<expires>(.*)</expires>', value)
120 #                         if exp_date:
121 #                             exp_date = exp_date.group(1)
122 #                             slice_cred_exp_list.append(exp_date)
123
124 #                     my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
125 #                         for t in zip(slice_list, slice_cred_exp_list)]
126
127 #                 if 'N/A' not in acc_auth_cred:
128 #                     for key, value in acc_auth_cred.iteritems():
129 #                         auth_list.append(key)
130 #                         #get cred_exp date
131 #                         exp_date = re.search('<expires>(.*)</expires>', value)
132 #                         if exp_date:
133 #                             exp_date = exp_date.group(1)
134 #                             auth_cred_exp_list.append(exp_date)
135
136         
137         return render_to_response(self.template, env, context_instance=RequestContext(request))
138