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 # 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()
107 if request.user.is_authenticated():
108 ## check user is pi or not
109 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
110 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
111 platform_details = execute_query(self.request, platform_query)
112 account_details = execute_query(self.request, account_query)
113 for platform_detail in platform_details:
114 for account_detail in account_details:
115 if platform_detail['platform_id'] == account_detail['platform_id']:
116 if 'config' in account_detail and account_detail['config'] is not '':
117 account_config = json.loads(account_detail['config'])
118 if 'myslice' in platform_detail['platform']:
119 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
121 if acc_auth_cred=={} or acc_auth_cred=='N/A':
127 env['person'] = self.request.user
131 env['theme'] = self.theme
132 env['section'] = "Dashboard"
135 env['username']=the_user(request)
136 env['topmenu_items'] = topmenu_items(None, request)
137 if state: env['state'] = state
138 elif not env['username']: env['state'] = None
139 # use one or two columns for the layout - not logged in users will see the login prompt
141 # account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
142 # account_details = execute_query(self.request, account_query)
143 # for account_detail in account_details:
144 # account_config = json.loads(account_detail['config'])
145 # platform_name = platform_detail['platform']
146 # if 'myslice' in platform_detail['platform']:
147 # acc_user_cred = account_config.get('delegated_user_credential','N/A')
148 # acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
149 # acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
151 # if 'N/A' not in acc_user_cred:
152 # exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
154 # user_exp_date = exp_date.group(1)
155 # user_cred_exp_list.append(user_exp_date)
157 # my_users = [{'cred_exp': t[0]}
158 # for t in zip(user_cred_exp_list)]
161 # if 'N/A' not in acc_slice_cred:
162 # for key, value in acc_slice_cred.iteritems():
163 # slice_list.append(key)
164 # # get cred_exp date
165 # exp_date = re.search('<expires>(.*)</expires>', value)
167 # exp_date = exp_date.group(1)
168 # slice_cred_exp_list.append(exp_date)
170 # my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
171 # for t in zip(slice_list, slice_cred_exp_list)]
173 # if 'N/A' not in acc_auth_cred:
174 # for key, value in acc_auth_cred.iteritems():
175 # auth_list.append(key)
177 # exp_date = re.search('<expires>(.*)</expires>', value)
179 # exp_date = exp_date.group(1)
180 # auth_cred_exp_list.append(exp_date)
183 return render_to_response(self.template, env, context_instance=RequestContext(request))