NoCreds: Warning Msg visible in Dashboard
[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 import json
10
11 from unfold.loginrequired import FreeAccessView
12
13 from manifold.core.query                import Query
14 from manifoldapi.manifoldapi            import execute_query
15
16 from manifoldapi.manifoldresult import ManifoldResult
17 from ui.topmenu import topmenu_items, the_user
18 from myslice.configengine import ConfigEngine
19
20 from myslice.theme import ThemeView
21
22 import activity.user
23
24 class HomeView (FreeAccessView, ThemeView):
25     template_name = 'home-view.html'
26         
27     # expose this so we can mention the backend URL on the welcome page
28     def default_env (self):
29         return { 
30                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
31                  }
32
33     def post (self,request):
34         env = self.default_env()
35         env['theme'] = self.theme
36         env['section'] = "Dashboard"
37         
38         username = request.POST.get('username')
39         password = request.POST.get('password')
40         
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                     
68                     # log user activity
69                     activity.user.login(self.request)
70                     
71                     ## check user is pi or not
72                     platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
73                     account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
74                     platform_details = execute_query(self.request, platform_query)
75                     account_details = execute_query(self.request, account_query)
76                     for platform_detail in platform_details:
77                         for account_detail in account_details:
78                             if platform_detail['platform_id'] == account_detail['platform_id']:
79                                 if 'config' in account_detail and account_detail['config'] is not '':
80                                     account_config = json.loads(account_detail['config'])
81                                     if 'myslice' in platform_detail['platform']:
82                                         acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
83                                         acc_user_cred = account_config.get('delegated_user_credential','N/A')
84                     # assigning values
85                     if acc_auth_cred=={} or acc_auth_cred=='N/A':
86                         pi = "is_not_pi"
87                     else:
88                         pi = "is_pi"
89
90                     # check if the user has creds or not
91                     if acc_user_cred == {} or acc_user_cred == 'N/A':
92                         user_cred = 'no_creds'
93                     else:
94                         user_cred = 'has_creds'
95
96
97                     env['pi'] = pi
98                     env['user_cred'] = user_cred                
99                 else: 
100                     env['person'] = None
101                 return render_to_response(self.template,env, context_instance=RequestContext(request))
102             else:
103                 # log user activity
104                 activity.user.login(self.request, "notactive")
105                 env['state'] = "Your account is not active, please contact the site admin."
106                 env['layout_1_or_2']="layout-unfold2.html"
107                 
108                 return render_to_response(self.template,env, context_instance=RequestContext(request))
109         # otherwise
110         else:
111             # log user activity
112             activity.user.login(self.request, "error")
113             env['state'] = "Your username and/or password were incorrect."
114             
115             return render_to_response(self.template, env, context_instance=RequestContext(request))
116
117     def get (self, request, state=None):
118         env = self.default_env()
119         acc_auth_cred={}
120         if request.user.is_authenticated():
121             ## check user is pi or not
122             platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
123             account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
124             # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
125             platform_details = execute_query(self.request, platform_query)
126             account_details = execute_query(self.request, account_query)
127             for platform_detail in platform_details:
128                 for account_detail in account_details:
129                     if 'platform_id' in platform_detail:
130                         if platform_detail['platform_id'] == account_detail['platform_id']:
131                             if 'config' in account_detail and account_detail['config'] is not '':
132                                 account_config = json.loads(account_detail['config'])
133                                 if 'myslice' in platform_detail['platform']:
134                                     acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
135                                     acc_user_cred = account_config.get('delegated_user_credential','N/A')
136             # assigning values
137             if acc_auth_cred=={} or acc_auth_cred=='N/A':
138                 pi = "is_not_pi"
139             else:
140                 pi = "is_pi"
141
142             # check if the user has creds or not
143             if acc_user_cred == {} or acc_user_cred == 'N/A':
144                 user_cred = 'no_creds'
145             else:
146                 user_cred = 'has_creds'
147            
148
149             env['pi'] = pi
150             env['user_cred'] = user_cred                
151             env['person'] = self.request.user
152         else: 
153             env['person'] = None
154
155         env['theme'] = self.theme
156         env['section'] = "Dashboard"
157
158
159         env['username']=the_user(request)
160         env['topmenu_items'] = topmenu_items(None, request)
161         if state: env['state'] = state
162         elif not env['username']: env['state'] = None
163         # use one or two columns for the layout - not logged in users will see the login prompt
164         
165 #         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
166 #         account_details = execute_query(self.request, account_query)
167 #         for account_detail in account_details:
168 #             account_config = json.loads(account_detail['config'])
169 #             platform_name = platform_detail['platform']
170 #             if 'myslice' in platform_detail['platform']:
171 #                 acc_user_cred = account_config.get('delegated_user_credential','N/A')
172 #                 acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
173 #                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
174
175 #                 if 'N/A' not in acc_user_cred:
176 #                     exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
177 #                     if exp_date:
178 #                         user_exp_date = exp_date.group(1)
179 #                         user_cred_exp_list.append(user_exp_date)
180
181 #                     my_users = [{'cred_exp': t[0]}
182 #                         for t in zip(user_cred_exp_list)]
183 #                
184
185 #                 if 'N/A' not in acc_slice_cred:
186 #                     for key, value in acc_slice_cred.iteritems():
187 #                         slice_list.append(key)
188 #                         # get cred_exp date
189 #                         exp_date = re.search('<expires>(.*)</expires>', value)
190 #                         if exp_date:
191 #                             exp_date = exp_date.group(1)
192 #                             slice_cred_exp_list.append(exp_date)
193
194 #                     my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
195 #                         for t in zip(slice_list, slice_cred_exp_list)]
196
197 #                 if 'N/A' not in acc_auth_cred:
198 #                     for key, value in acc_auth_cred.iteritems():
199 #                         auth_list.append(key)
200 #                         #get cred_exp date
201 #                         exp_date = re.search('<expires>(.*)</expires>', value)
202 #                         if exp_date:
203 #                             exp_date = exp_date.group(1)
204 #                             auth_cred_exp_list.append(exp_date)
205
206         
207         return render_to_response(self.template, env, context_instance=RequestContext(request))
208