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
26 class HomeView (FreeAccessView, ThemeView):
27 template_name = 'home-view.html'
29 # expose this so we can mention the backend URL on the welcome page
30 def default_env (self):
32 'MANIFOLD_URL':ConfigEngine().manifold_url(),
35 def post (self,request):
36 env = self.default_env()
37 env['theme'] = self.theme
38 env['section'] = "Dashboard"
40 username = request.POST.get('username')
41 password = request.POST.get('password')
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}
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
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:
66 if request.user.is_authenticated():
67 env['person'] = self.request.user
68 env['username'] = self.request.user
71 activity.user.login(self.request)
73 ## check user is pi or not
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')
92 if acc_auth_cred=={} or acc_auth_cred=='N/A':
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'
101 exp_date = get_expiration(acc_user_cred, 'timestamp')
102 if exp_date < time.time():
103 user_cred = 'creds_expired'
105 user_cred = 'has_creds'
107 # list the pending slices of this user
109 for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all():
110 pending_slices.append(slices.slice_name)
112 env['pending_slices'] = pending_slices
114 env['user_cred'] = user_cred
117 return render_to_response(self.template,env, context_instance=RequestContext(request))
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"
124 return render_to_response(self.template,env, context_instance=RequestContext(request))
128 activity.user.login(self.request, "error")
129 env['state'] = "Your username and/or password were incorrect."
131 return render_to_response(self.template, env, context_instance=RequestContext(request))
133 def get (self, request, state=None):
134 env = self.default_env()
136 if request.user.is_authenticated():
138 ## check user is pi or not
139 platform_details = {}
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')
159 if acc_auth_cred=={} or acc_auth_cred=='N/A':
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'
168 exp_date = get_expiration(acc_user_cred, 'timestamp')
169 if exp_date < time.time():
170 user_cred = 'creds_expired'
172 user_cred = 'has_creds'
174 # list the pending slices of this user
176 for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all():
177 pending_slices.append(slices.slice_name)
179 env['pending_slices'] = pending_slices
181 env['user_cred'] = user_cred
182 env['person'] = self.request.user
186 env['theme'] = self.theme
187 env['section'] = "Dashboard"
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
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')
206 # if 'N/A' not in acc_user_cred:
207 # exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
209 # user_exp_date = exp_date.group(1)
210 # user_cred_exp_list.append(user_exp_date)
212 # my_users = [{'cred_exp': t[0]}
213 # for t in zip(user_cred_exp_list)]
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)
222 # exp_date = exp_date.group(1)
223 # slice_cred_exp_list.append(exp_date)
225 # my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
226 # for t in zip(slice_list, slice_cred_exp_list)]
228 # if 'N/A' not in acc_auth_cred:
229 # for key, value in acc_auth_cred.iteritems():
230 # auth_list.append(key)
232 # exp_date = re.search('<expires>(.*)</expires>', value)
234 # exp_date = exp_date.group(1)
235 # auth_cred_exp_list.append(exp_date)
238 return render_to_response(self.template, env, context_instance=RequestContext(request))