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