Pi check in each view to make the topmenu show correctly
[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 import json
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
21 class HomeView (FreeAccessView, ThemeView):
22     template_name = 'home-view.html'
23         
24     # expose this so we can mention the backend URL on the welcome page
25     def default_env (self):
26         return { 
27                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
28                  }
29
30     def post (self,request):
31         env = self.default_env()
32         env['theme'] = self.theme
33         env['section'] = "Dashboard"
34         
35         username = request.POST.get('username')
36         password = request.POST.get('password')
37         
38         # pass request within the token, so manifold session key can be attached to the request session.
39         token = {'username': username, 'password': password, 'request': request}    
40
41         # our authenticate function returns either
42         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
43         # . a django User in case of success
44         # . or None if the backend could be reached but the authentication failed
45         auth_result = authenticate(token=token)
46         # use one or two columns for the layout - not logged in users will see the login prompt
47         # high-level errors, like connection refused or the like
48         if isinstance (auth_result, ManifoldResult):
49             manifoldresult = auth_result
50             # let's use ManifoldResult.__repr__
51             env['state']="%s"%manifoldresult
52             
53             return render_to_response(self.template,env, context_instance=RequestContext(request))
54         # user was authenticated at the backend
55         elif auth_result is not None:
56             user=auth_result
57             if user.is_active:
58                 print "LOGGING IN"
59                 login(request, user)
60                 
61                 if request.user.is_authenticated(): 
62                     env['person'] = self.request.user
63                     env['username'] = self.request.user
64                     
65                     ## check user is pi or not
66                     platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
67                     account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
68                     platform_details = execute_query(self.request, platform_query)
69                     account_details = execute_query(self.request, account_query)
70                     for platform_detail in platform_details:
71                         for account_detail in account_details:
72                             if platform_detail['platform_id'] == account_detail['platform_id']:
73                                 if 'config' in account_detail and account_detail['config'] is not '':
74                                     account_config = json.loads(account_detail['config'])
75                                     if 'myslice' in platform_detail['platform']:
76                                         acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
77                     # assigning values
78                     if acc_auth_cred == {}:
79                         pi = "is_not_pi"
80                     else:
81                         pi = "is_pi"
82
83                     print "testing"
84                     print pi
85  
86                     env['pi'] = pi                
87                 else: 
88                     env['person'] = None
89                 return render_to_response(self.template,env, context_instance=RequestContext(request))
90             else:
91                 env['state'] = "Your account is not active, please contact the site admin."
92                 env['layout_1_or_2']="layout-unfold2.html"
93                 
94                 return render_to_response(self.template,env, context_instance=RequestContext(request))
95         # otherwise
96         else:
97             env['state'] = "Your username and/or password were incorrect."
98             
99             return render_to_response(self.template, env, context_instance=RequestContext(request))
100
101     def get (self, request, state=None):
102         env = self.default_env()
103         ## check user is pi or not
104         platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
105         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
106         platform_details = execute_query(self.request, platform_query)
107         account_details = execute_query(self.request, account_query)
108         for platform_detail in platform_details:
109             for account_detail in account_details:
110                 if platform_detail['platform_id'] == account_detail['platform_id']:
111                     if 'config' in account_detail and account_detail['config'] is not '':
112                         account_config = json.loads(account_detail['config'])
113                         if 'myslice' in platform_detail['platform']:
114                             acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
115         # assigning values
116         if acc_auth_cred == {}:
117             pi = "is_not_pi"
118         else:
119             pi = "is_pi"
120
121  
122         env['pi'] = pi
123         if request.user.is_authenticated(): 
124             env['person'] = self.request.user
125         else: 
126             env['person'] = None
127
128         env['theme'] = self.theme
129         env['section'] = "Dashboard"
130
131
132         env['username']=the_user(request)
133         env['topmenu_items'] = topmenu_items(None, request)
134         if state: env['state'] = state
135         elif not env['username']: env['state'] = None
136         # use one or two columns for the layout - not logged in users will see the login prompt
137         
138 #         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
139 #         account_details = execute_query(self.request, account_query)
140 #         for account_detail in account_details:
141 #             account_config = json.loads(account_detail['config'])
142 #             platform_name = platform_detail['platform']
143 #             if 'myslice' in platform_detail['platform']:
144 #                 acc_user_cred = account_config.get('delegated_user_credential','N/A')
145 #                 acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
146 #                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
147
148 #                 if 'N/A' not in acc_user_cred:
149 #                     exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
150 #                     if exp_date:
151 #                         user_exp_date = exp_date.group(1)
152 #                         user_cred_exp_list.append(user_exp_date)
153
154 #                     my_users = [{'cred_exp': t[0]}
155 #                         for t in zip(user_cred_exp_list)]
156 #                
157
158 #                 if 'N/A' not in acc_slice_cred:
159 #                     for key, value in acc_slice_cred.iteritems():
160 #                         slice_list.append(key)
161 #                         # get cred_exp date
162 #                         exp_date = re.search('<expires>(.*)</expires>', value)
163 #                         if exp_date:
164 #                             exp_date = exp_date.group(1)
165 #                             slice_cred_exp_list.append(exp_date)
166
167 #                     my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
168 #                         for t in zip(slice_list, slice_cred_exp_list)]
169
170 #                 if 'N/A' not in acc_auth_cred:
171 #                     for key, value in acc_auth_cred.iteritems():
172 #                         auth_list.append(key)
173 #                         #get cred_exp date
174 #                         exp_date = re.search('<expires>(.*)</expires>', value)
175 #                         if exp_date:
176 #                             exp_date = exp_date.group(1)
177 #                             auth_cred_exp_list.append(exp_date)
178
179         
180         return render_to_response(self.template, env, context_instance=RequestContext(request))
181