changed column width
[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
10 from unfold.loginrequired               import FreeAccessView
11
12 from manifold.core.query                import Query
13 from manifoldapi.manifoldapi            import execute_query
14
15 from manifoldapi.manifoldresult         import ManifoldResult
16 from ui.topmenu                         import topmenu_items, the_user
17 from myslice.configengine               import ConfigEngine
18
19 from myslice.theme                      import ThemeView
20 from portal.account                     import Account, get_expiration
21 from portal.models                      import PendingSlice
22
23 import json, time
24 import activity.user
25
26 class HomeView (FreeAccessView, ThemeView):
27     template_name = 'home-view.html'
28         
29     # expose this so we can mention the backend URL on the welcome page
30     def default_env (self):
31         return { 
32                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
33                  }
34
35     def post (self,request):
36         env = self.default_env()
37         env['theme'] = self.theme
38         env['section'] = "Dashboard"
39         
40         username = request.POST.get('username')
41         password = request.POST.get('password')
42         
43         # pass request within the token, so manifold session key can be attached to the request session.
44         token = {'username': username, 'password': password, 'request': request}    
45
46         # our authenticate function returns either
47         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
48         # . a django User in case of success
49         # . or None if the backend could be reached but the authentication failed
50         auth_result = authenticate(token=token)
51         # use one or two columns for the layout - not logged in users will see the login prompt
52         # high-level errors, like connection refused or the like
53         if isinstance (auth_result, ManifoldResult):
54             manifoldresult = auth_result
55             # let's use ManifoldResult.__repr__
56             env['state']="%s"%manifoldresult
57             
58             return render_to_response(self.template,env, context_instance=RequestContext(request))
59         # user was authenticated at the backend
60         elif auth_result is not None:
61             user=auth_result
62             if user.is_active:
63                 print "LOGGING IN"
64                 login(request, user)
65                 
66                 if request.user.is_authenticated(): 
67                     env['person'] = self.request.user
68                     env['username'] = self.request.user
69                     
70                     # log user activity
71                     activity.user.login(self.request)
72                     
73                     ## check user is pi or not
74                     platform_details = {}
75                     account_details = {}
76                     acc_auth_cred = {}
77                     acc_user_cred = {}
78                     platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
79                     account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
80                     platform_details = execute_query(self.request, platform_query)
81                     account_details = execute_query(self.request, account_query)
82                     if platform_details is not None and platform_details != {}:
83                         for platform_detail in platform_details:
84                             for account_detail in account_details:
85                                 if platform_detail['platform_id'] == account_detail['platform_id']:
86                                     if 'config' in account_detail and account_detail['config'] is not '':
87                                         account_config = json.loads(account_detail['config'])
88                                         if 'myslice' in platform_detail['platform']:
89                                             acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
90                                             acc_user_cred = account_config.get('delegated_user_credential','N/A')
91                     # assigning values
92                     if acc_auth_cred=={} or acc_auth_cred=='N/A':
93                         pi = "is_not_pi"
94                     else:
95                         pi = "is_pi"
96
97                     # check if the user has creds or not
98                     if acc_user_cred == {} or acc_user_cred == 'N/A':
99                         user_cred = 'no_creds'
100                     else:
101                         exp_date = get_expiration(acc_user_cred, 'timestamp')
102                         if exp_date < time.time():
103                             user_cred = 'creds_expired'
104                         else:
105                             user_cred = 'has_creds'
106
107                     # list the pending slices of this user
108                     pending_slices = []
109                     for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all():
110                         pending_slices.append(slices.slice_name)
111
112                     env['pending_slices'] = pending_slices
113                     env['pi'] = pi
114                     env['user_cred'] = user_cred                
115                 else: 
116                     env['person'] = None
117                 return render_to_response(self.template,env, context_instance=RequestContext(request))
118             else:
119                 # log user activity
120                 activity.user.login(self.request, "notactive")
121                 env['state'] = "Your account is not active, please contact the site admin."
122                 env['layout_1_or_2']="layout-unfold2.html"
123                 
124                 return render_to_response(self.template,env, context_instance=RequestContext(request))
125         # otherwise
126         else:
127             # log user activity
128             activity.user.login(self.request, "error")
129             env['state'] = "Your username and/or password were incorrect."
130             
131             return render_to_response(self.template, env, context_instance=RequestContext(request))
132
133     def get (self, request, state=None):
134         env = self.default_env()
135         acc_auth_cred={}
136         if request.user.is_authenticated():
137            
138             ## check user is pi or not
139             platform_details = {}
140             account_details = {}
141             acc_auth_cred = {}
142             acc_user_cred = {}
143             platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
144             account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
145             # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
146             platform_details = execute_query(self.request, platform_query)
147             account_details = execute_query(self.request, account_query)
148             if platform_details is not None and platform_details != {}:
149                 for platform_detail in platform_details:
150                     for account_detail in account_details:
151                         if 'platform_id' in platform_detail:
152                             if platform_detail['platform_id'] == account_detail['platform_id']:
153                                 if 'config' in account_detail and account_detail['config'] is not '':
154                                     account_config = json.loads(account_detail['config'])
155                                     if 'myslice' in platform_detail['platform']:
156                                         acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
157                                         acc_user_cred = account_config.get('delegated_user_credential','N/A')
158             # assigning values
159             if acc_auth_cred=={} or acc_auth_cred=='N/A':
160                 pi = "is_not_pi"
161             else:
162                 pi = "is_pi"
163
164             # check if the user has creds or not
165             if acc_user_cred == {} or acc_user_cred == 'N/A':
166                 user_cred = 'no_creds'
167             else:
168                 exp_date = get_expiration(acc_user_cred, 'timestamp')
169                 if exp_date < time.time():
170                     user_cred = 'creds_expired'
171                 else:
172                     user_cred = 'has_creds'
173
174             # list the pending slices of this user
175             pending_slices = []
176             for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all():
177                 pending_slices.append(slices.slice_name)
178         
179             env['pending_slices'] = pending_slices
180             env['pi'] = pi
181             env['user_cred'] = user_cred                
182             env['person'] = self.request.user
183         else: 
184             env['person'] = None
185
186         env['theme'] = self.theme
187         env['section'] = "Dashboard"
188
189
190         env['username']=the_user(request)
191         env['topmenu_items'] = topmenu_items(None, request)
192         if state: env['state'] = state
193         elif not env['username']: env['state'] = None
194         # use one or two columns for the layout - not logged in users will see the login prompt
195         
196 #         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
197 #         account_details = execute_query(self.request, account_query)
198 #         for account_detail in account_details:
199 #             account_config = json.loads(account_detail['config'])
200 #             platform_name = platform_detail['platform']
201 #             if 'myslice' in platform_detail['platform']:
202 #                 acc_user_cred = account_config.get('delegated_user_credential','N/A')
203 #                 acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
204 #                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
205
206 #                 if 'N/A' not in acc_user_cred:
207 #                     exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
208 #                     if exp_date:
209 #                         user_exp_date = exp_date.group(1)
210 #                         user_cred_exp_list.append(user_exp_date)
211
212 #                     my_users = [{'cred_exp': t[0]}
213 #                         for t in zip(user_cred_exp_list)]
214 #                
215
216 #                 if 'N/A' not in acc_slice_cred:
217 #                     for key, value in acc_slice_cred.iteritems():
218 #                         slice_list.append(key)
219 #                         # get cred_exp date
220 #                         exp_date = re.search('<expires>(.*)</expires>', value)
221 #                         if exp_date:
222 #                             exp_date = exp_date.group(1)
223 #                             slice_cred_exp_list.append(exp_date)
224
225 #                     my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
226 #                         for t in zip(slice_list, slice_cred_exp_list)]
227
228 #                 if 'N/A' not in acc_auth_cred:
229 #                     for key, value in acc_auth_cred.iteritems():
230 #                         auth_list.append(key)
231 #                         #get cred_exp date
232 #                         exp_date = re.search('<expires>(.*)</expires>', value)
233 #                         if exp_date:
234 #                             exp_date = exp_date.group(1)
235 #                             auth_cred_exp_list.append(exp_date)
236
237         
238         return render_to_response(self.template, env, context_instance=RequestContext(request))
239