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
11 from unfold.loginrequired import FreeAccessView
13 from manifold.core.query import Query
14 from manifoldapi.manifoldapi import execute_query
16 from manifoldapi.manifoldresult import ManifoldResult
17 from ui.topmenu import topmenu_items, the_user
18 from myslice.configengine import ConfigEngine
20 from myslice.theme import ThemeView
24 class HomeView (FreeAccessView, ThemeView):
25 template_name = 'home-view.html'
27 # expose this so we can mention the backend URL on the welcome page
28 def default_env (self):
30 'MANIFOLD_URL':ConfigEngine().manifold_url(),
33 def post (self,request):
34 env = self.default_env()
35 env['theme'] = self.theme
36 env['section'] = "Dashboard"
38 username = request.POST.get('username')
39 password = request.POST.get('password')
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}
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
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:
64 if request.user.is_authenticated():
65 env['person'] = self.request.user
66 env['username'] = self.request.user
69 activity.user.login(self.request)
71 ## check user is pi or not
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')
90 if acc_auth_cred=={} or acc_auth_cred=='N/A':
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'
99 user_cred = 'has_creds'
103 env['user_cred'] = user_cred
106 return render_to_response(self.template,env, context_instance=RequestContext(request))
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"
113 return render_to_response(self.template,env, context_instance=RequestContext(request))
117 activity.user.login(self.request, "error")
118 env['state'] = "Your username and/or password were incorrect."
120 return render_to_response(self.template, env, context_instance=RequestContext(request))
122 def get (self, request, state=None):
123 env = self.default_env()
125 if request.user.is_authenticated():
127 ## check user is pi or not
128 platform_details = {}
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')
148 if acc_auth_cred=={} or acc_auth_cred=='N/A':
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'
157 user_cred = 'has_creds'
161 env['user_cred'] = user_cred
162 env['person'] = self.request.user
166 env['theme'] = self.theme
167 env['section'] = "Dashboard"
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
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')
186 # if 'N/A' not in acc_user_cred:
187 # exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
189 # user_exp_date = exp_date.group(1)
190 # user_cred_exp_list.append(user_exp_date)
192 # my_users = [{'cred_exp': t[0]}
193 # for t in zip(user_cred_exp_list)]
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)
202 # exp_date = exp_date.group(1)
203 # slice_cred_exp_list.append(exp_date)
205 # my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
206 # for t in zip(slice_list, slice_cred_exp_list)]
208 # if 'N/A' not in acc_auth_cred:
209 # for key, value in acc_auth_cred.iteritems():
210 # auth_list.append(key)
212 # exp_date = re.search('<expires>(.*)</expires>', value)
214 # exp_date = exp_date.group(1)
215 # auth_cred_exp_list.append(exp_date)
218 return render_to_response(self.template, env, context_instance=RequestContext(request))