added debug code
[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_details = {}
73                     account_details = {}
74                     acc_auth_cred = {}
75                     acc_user_cred = {}
76                     platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
77                     account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
78                     platform_details = execute_query(self.request, platform_query)
79                     account_details = execute_query(self.request, account_query)
80                     if platform_details is not None and platform_details != {}:
81                         for platform_detail in platform_details:
82                             for account_detail in account_details:
83                                 if platform_detail['platform_id'] == account_detail['platform_id']:
84                                     if 'config' in account_detail and account_detail['config'] is not '':
85                                         account_config = json.loads(account_detail['config'])
86                                         if 'myslice' in platform_detail['platform']:
87                                             acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
88                                             acc_user_cred = account_config.get('delegated_user_credential','N/A')
89                     # assigning values
90                     if acc_auth_cred=={} or acc_auth_cred=='N/A':
91                         pi = "is_not_pi"
92                     else:
93                         pi = "is_pi"
94
95                     # check if the user has creds or not
96                     if acc_user_cred == {} or acc_user_cred == 'N/A':
97                         user_cred = 'no_creds'
98                     else:
99                         user_cred = 'has_creds'
100
101
102                     env['pi'] = pi
103                     env['user_cred'] = user_cred                
104                 else: 
105                     env['person'] = None
106                 return render_to_response(self.template,env, context_instance=RequestContext(request))
107             else:
108                 # log user activity
109                 activity.user.login(self.request, "notactive")
110                 env['state'] = "Your account is not active, please contact the site admin."
111                 env['layout_1_or_2']="layout-unfold2.html"
112                 
113                 return render_to_response(self.template,env, context_instance=RequestContext(request))
114         # otherwise
115         else:
116             # log user activity
117             activity.user.login(self.request, "error")
118             env['state'] = "Your username and/or password were incorrect."
119             
120             return render_to_response(self.template, env, context_instance=RequestContext(request))
121
122     def get (self, request, state=None):
123         env = self.default_env()
124         acc_auth_cred={}
125         if request.user.is_authenticated():
126            
127             ## check user is pi or not
128             platform_details = {}
129             account_details = {}
130             acc_auth_cred = {}
131             acc_user_cred = {}
132             platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
133             account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
134             # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
135             platform_details = execute_query(self.request, platform_query)
136             account_details = execute_query(self.request, account_query)
137             if platform_details is not None and platform_details != {}:
138                 for platform_detail in platform_details:
139                     for account_detail in account_details:
140                         if 'platform_id' in platform_detail:
141                             if platform_detail['platform_id'] == account_detail['platform_id']:
142                                 if 'config' in account_detail and account_detail['config'] is not '':
143                                     account_config = json.loads(account_detail['config'])
144                                     if 'myslice' in platform_detail['platform']:
145                                         acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
146                                         acc_user_cred = account_config.get('delegated_user_credential','N/A')
147             # assigning values
148             if acc_auth_cred=={} or acc_auth_cred=='N/A':
149                 pi = "is_not_pi"
150             else:
151                 pi = "is_pi"
152
153             # check if the user has creds or not
154             if acc_user_cred == {} or acc_user_cred == 'N/A':
155                 user_cred = 'no_creds'
156             else:
157                 user_cred = 'has_creds'
158            
159
160             env['pi'] = pi
161             env['user_cred'] = user_cred                
162             env['person'] = self.request.user
163         else: 
164             env['person'] = None
165
166         env['theme'] = self.theme
167         env['section'] = "Dashboard"
168
169
170         env['username']=the_user(request)
171         env['topmenu_items'] = topmenu_items(None, request)
172         if state: env['state'] = state
173         elif not env['username']: env['state'] = None
174         # use one or two columns for the layout - not logged in users will see the login prompt
175         
176 #         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
177 #         account_details = execute_query(self.request, account_query)
178 #         for account_detail in account_details:
179 #             account_config = json.loads(account_detail['config'])
180 #             platform_name = platform_detail['platform']
181 #             if 'myslice' in platform_detail['platform']:
182 #                 acc_user_cred = account_config.get('delegated_user_credential','N/A')
183 #                 acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
184 #                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
185
186 #                 if 'N/A' not in acc_user_cred:
187 #                     exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
188 #                     if exp_date:
189 #                         user_exp_date = exp_date.group(1)
190 #                         user_cred_exp_list.append(user_exp_date)
191
192 #                     my_users = [{'cred_exp': t[0]}
193 #                         for t in zip(user_cred_exp_list)]
194 #                
195
196 #                 if 'N/A' not in acc_slice_cred:
197 #                     for key, value in acc_slice_cred.iteritems():
198 #                         slice_list.append(key)
199 #                         # get cred_exp date
200 #                         exp_date = re.search('<expires>(.*)</expires>', value)
201 #                         if exp_date:
202 #                             exp_date = exp_date.group(1)
203 #                             slice_cred_exp_list.append(exp_date)
204
205 #                     my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
206 #                         for t in zip(slice_list, slice_cred_exp_list)]
207
208 #                 if 'N/A' not in acc_auth_cred:
209 #                     for key, value in acc_auth_cred.iteritems():
210 #                         auth_list.append(key)
211 #                         #get cred_exp date
212 #                         exp_date = re.search('<expires>(.*)</expires>', value)
213 #                         if exp_date:
214 #                             exp_date = exp_date.group(1)
215 #                             auth_cred_exp_list.append(exp_date)
216
217         
218         return render_to_response(self.template, env, context_instance=RequestContext(request))
219