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').lower()
36 password = request.POST.get('password')
38 # LDAP form - If FIBRE, then get the possibilite to authenticate using usernameldap
39 #if self.theme == 'fibre':
40 usernameldap = request.POST.get('usernameldap')
41 token = {'usernameldap': usernameldap, 'username': username ,'password': password, 'request': request}
44 # Follow original code
45 ## pass request within the token, so manifold session key can be attached to the request session.
46 #token = {'username': username, 'password': password, 'request': request}
48 # our authenticate function returns either
49 # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
50 # . a django User in case of success
51 # . or None if the backend could be reached but the authentication failed
52 auth_result = authenticate(token=token)
53 # use one or two columns for the layout - not logged in users will see the login prompt
54 # high-level errors, like connection refused or the like
55 if isinstance (auth_result, ManifoldResult):
56 manifoldresult = auth_result
57 # let's use ManifoldResult.__repr__
58 env['state']="%s"%manifoldresult
60 return render_to_response(self.template,env, context_instance=RequestContext(request))
61 # user was authenticated at the backend
62 elif auth_result is not None:
68 if request.user.is_authenticated():
69 env['person'] = self.request.user
70 env['username'] = self.request.user
72 ## check user is pi or not
73 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
74 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
75 platform_details = execute_query(self.request, platform_query)
76 account_details = execute_query(self.request, account_query)
77 for platform_detail in platform_details:
78 for account_detail in account_details:
79 if platform_detail['platform_id'] == account_detail['platform_id']:
80 if 'config' in account_detail and account_detail['config'] is not '':
81 account_config = json.loads(account_detail['config'])
82 if 'myslice' in platform_detail['platform']:
83 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
85 if acc_auth_cred=={} or acc_auth_cred=='N/A':
93 return render_to_response(self.template,env, context_instance=RequestContext(request))
95 env['state'] = "Your account is not active, please contact the site admin."
96 env['layout_1_or_2']="layout-unfold2.html"
98 return render_to_response(self.template,env, context_instance=RequestContext(request))
101 env['state'] = "Your username and/or password were incorrect."
103 return render_to_response(self.template, env, context_instance=RequestContext(request))
105 def get (self, request, state=None):
106 env = self.default_env()
108 if request.user.is_authenticated():
109 ## check user is pi or not
110 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
111 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
112 # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
113 platform_details = execute_query(self.request, platform_query)
114 account_details = execute_query(self.request, account_query)
115 for platform_detail in platform_details:
116 for account_detail in account_details:
117 if 'platform_id' in platform_detail:
118 if platform_detail['platform_id'] == account_detail['platform_id']:
119 if 'config' in account_detail and account_detail['config'] is not '':
120 account_config = json.loads(account_detail['config'])
121 if 'myslice' in platform_detail['platform']:
122 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
124 if acc_auth_cred=={} or acc_auth_cred=='N/A':
130 env['person'] = self.request.user
134 env['theme'] = self.theme
135 env['section'] = "Dashboard"
138 env['username']=the_user(request)
139 env['topmenu_items'] = topmenu_items(None, request)
140 if state: env['state'] = state
141 elif not env['username']: env['state'] = None
142 # use one or two columns for the layout - not logged in users will see the login prompt
144 # account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
145 # account_details = execute_query(self.request, account_query)
146 # for account_detail in account_details:
147 # account_config = json.loads(account_detail['config'])
148 # platform_name = platform_detail['platform']
149 # if 'myslice' in platform_detail['platform']:
150 # acc_user_cred = account_config.get('delegated_user_credential','N/A')
151 # acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
152 # acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
154 # if 'N/A' not in acc_user_cred:
155 # exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
157 # user_exp_date = exp_date.group(1)
158 # user_cred_exp_list.append(user_exp_date)
160 # my_users = [{'cred_exp': t[0]}
161 # for t in zip(user_cred_exp_list)]
164 # if 'N/A' not in acc_slice_cred:
165 # for key, value in acc_slice_cred.iteritems():
166 # slice_list.append(key)
167 # # get cred_exp date
168 # exp_date = re.search('<expires>(.*)</expires>', value)
170 # exp_date = exp_date.group(1)
171 # slice_cred_exp_list.append(exp_date)
173 # my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
174 # for t in zip(slice_list, slice_cred_exp_list)]
176 # if 'N/A' not in acc_auth_cred:
177 # for key, value in acc_auth_cred.iteritems():
178 # auth_list.append(key)
180 # exp_date = re.search('<expires>(.*)</expires>', value)
182 # exp_date = exp_date.group(1)
183 # auth_cred_exp_list.append(exp_date)
186 return render_to_response(self.template, env, context_instance=RequestContext(request))