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
21 class HomeView (FreeAccessView, ThemeView):
22 template_name = 'home-view.html'
24 # expose this so we can mention the backend URL on the welcome page
25 def default_env (self):
27 'MANIFOLD_URL':ConfigEngine().manifold_url(),
30 def post (self,request):
31 env = self.default_env()
32 env['theme'] = self.theme
33 env['section'] = "Dashboard"
35 username = request.POST.get('username')
36 password = request.POST.get('password')
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}
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
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:
61 if request.user.is_authenticated():
62 env['person'] = self.request.user
63 env['username'] = self.request.user
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')
78 if acc_auth_cred=={} or acc_auth_cred=='N/A':
86 return render_to_response(self.template,env, context_instance=RequestContext(request))
88 env['state'] = "Your account is not active, please contact the site admin."
89 env['layout_1_or_2']="layout-unfold2.html"
91 return render_to_response(self.template,env, context_instance=RequestContext(request))
94 env['state'] = "Your username and/or password were incorrect."
96 return render_to_response(self.template, env, context_instance=RequestContext(request))
98 def get (self, request, state=None):
99 env = self.default_env()
100 if request.user.is_authenticated():
101 ## check user is pi or not
102 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
103 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
104 # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
105 platform_details = execute_query(self.request, platform_query)
106 account_details = execute_query(self.request, account_query)
107 for platform_detail in platform_details:
108 for account_detail in account_details:
109 if platform_detail['platform_id'] == account_detail['platform_id']:
110 if 'config' in account_detail and account_detail['config'] is not '':
111 account_config = json.loads(account_detail['config'])
112 if 'myslice' in platform_detail['platform']:
113 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
115 if acc_auth_cred=={} or acc_auth_cred=='N/A':
121 env['person'] = self.request.user
125 env['theme'] = self.theme
126 env['section'] = "Dashboard"
129 env['username']=the_user(request)
130 env['topmenu_items'] = topmenu_items(None, request)
131 if state: env['state'] = state
132 elif not env['username']: env['state'] = None
133 # use one or two columns for the layout - not logged in users will see the login prompt
135 # account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
136 # account_details = execute_query(self.request, account_query)
137 # for account_detail in account_details:
138 # account_config = json.loads(account_detail['config'])
139 # platform_name = platform_detail['platform']
140 # if 'myslice' in platform_detail['platform']:
141 # acc_user_cred = account_config.get('delegated_user_credential','N/A')
142 # acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
143 # acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
145 # if 'N/A' not in acc_user_cred:
146 # exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
148 # user_exp_date = exp_date.group(1)
149 # user_cred_exp_list.append(user_exp_date)
151 # my_users = [{'cred_exp': t[0]}
152 # for t in zip(user_cred_exp_list)]
155 # if 'N/A' not in acc_slice_cred:
156 # for key, value in acc_slice_cred.iteritems():
157 # slice_list.append(key)
158 # # get cred_exp date
159 # exp_date = re.search('<expires>(.*)</expires>', value)
161 # exp_date = exp_date.group(1)
162 # slice_cred_exp_list.append(exp_date)
164 # my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
165 # for t in zip(slice_list, slice_cred_exp_list)]
167 # if 'N/A' not in acc_auth_cred:
168 # for key, value in acc_auth_cred.iteritems():
169 # auth_list.append(key)
171 # exp_date = re.search('<expires>(.*)</expires>', value)
173 # exp_date = exp_date.group(1)
174 # auth_cred_exp_list.append(exp_date)
177 return render_to_response(self.template, env, context_instance=RequestContext(request))