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
72 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
73 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
74 platform_details = execute_query(self.request, platform_query)
75 account_details = execute_query(self.request, account_query)
76 for platform_detail in platform_details:
77 for account_detail in account_details:
78 if platform_detail['platform_id'] == account_detail['platform_id']:
79 if 'config' in account_detail and account_detail['config'] is not '':
80 account_config = json.loads(account_detail['config'])
81 if 'myslice' in platform_detail['platform']:
82 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
84 if acc_auth_cred=={} or acc_auth_cred=='N/A':
92 return render_to_response(self.template,env, context_instance=RequestContext(request))
95 activity.user.login(self.request, "notactive")
96 env['state'] = "Your account is not active, please contact the site admin."
97 env['layout_1_or_2']="layout-unfold2.html"
99 return render_to_response(self.template,env, context_instance=RequestContext(request))
103 activity.user.login(self.request, "error")
104 env['state'] = "Your username and/or password were incorrect."
106 return render_to_response(self.template, env, context_instance=RequestContext(request))
108 def get (self, request, state=None):
109 env = self.default_env()
111 if request.user.is_authenticated():
112 ## check user is pi or not
113 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
114 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
115 # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
116 platform_details = execute_query(self.request, platform_query)
117 account_details = execute_query(self.request, account_query)
118 if platform_details is None:
121 for platform_detail in platform_details:
122 for account_detail in account_details:
123 if 'platform_id' in platform_detail:
124 if platform_detail['platform_id'] == account_detail['platform_id']:
125 if 'config' in account_detail and account_detail['config'] is not '':
126 account_config = json.loads(account_detail['config'])
127 if 'myslice' in platform_detail['platform']:
128 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
130 if acc_auth_cred=={} or acc_auth_cred=='N/A':
136 env['person'] = self.request.user
140 env['theme'] = self.theme
141 env['section'] = "Dashboard"
144 env['username']=the_user(request)
145 env['topmenu_items'] = topmenu_items(None, request)
146 if state: env['state'] = state
147 elif not env['username']: env['state'] = None
148 # use one or two columns for the layout - not logged in users will see the login prompt
150 # account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
151 # account_details = execute_query(self.request, account_query)
152 # for account_detail in account_details:
153 # account_config = json.loads(account_detail['config'])
154 # platform_name = platform_detail['platform']
155 # if 'myslice' in platform_detail['platform']:
156 # acc_user_cred = account_config.get('delegated_user_credential','N/A')
157 # acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
158 # acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
160 # if 'N/A' not in acc_user_cred:
161 # exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
163 # user_exp_date = exp_date.group(1)
164 # user_cred_exp_list.append(user_exp_date)
166 # my_users = [{'cred_exp': t[0]}
167 # for t in zip(user_cred_exp_list)]
170 # if 'N/A' not in acc_slice_cred:
171 # for key, value in acc_slice_cred.iteritems():
172 # slice_list.append(key)
173 # # get cred_exp date
174 # exp_date = re.search('<expires>(.*)</expires>', value)
176 # exp_date = exp_date.group(1)
177 # slice_cred_exp_list.append(exp_date)
179 # my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
180 # for t in zip(slice_list, slice_cred_exp_list)]
182 # if 'N/A' not in acc_auth_cred:
183 # for key, value in acc_auth_cred.iteritems():
184 # auth_list.append(key)
186 # exp_date = re.search('<expires>(.*)</expires>', value)
188 # exp_date = exp_date.group(1)
189 # auth_cred_exp_list.append(exp_date)
192 return render_to_response(self.template, env, context_instance=RequestContext(request))