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
9 from unfold.loginrequired import FreeAccessView
11 from manifold.core.query import Query
12 from manifoldapi.manifoldapi import execute_query
14 from manifoldapi.manifoldresult import ManifoldResult
15 from ui.topmenu import topmenu_items, the_user
16 from myslice.configengine import ConfigEngine
18 from myslice.theme import ThemeView
20 class HomeView (FreeAccessView, ThemeView):
21 template_name = 'home-view.html'
23 # expose this so we can mention the backend URL on the welcome page
24 def default_env (self):
26 'MANIFOLD_URL':ConfigEngine().manifold_url(),
29 def post (self,request):
30 env = self.default_env()
31 env['theme'] = self.theme
32 env['section'] = "Dashboard"
34 username = request.POST.get('username')
35 password = request.POST.get('password')
37 # pass request within the token, so manifold session key can be attached to the request session.
38 token = {'username': username, 'password': password, 'request': request}
40 # our authenticate function returns either
41 # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
42 # . a django User in case of success
43 # . or None if the backend could be reached but the authentication failed
44 auth_result = authenticate(token=token)
45 # use one or two columns for the layout - not logged in users will see the login prompt
46 # high-level errors, like connection refused or the like
47 if isinstance (auth_result, ManifoldResult):
48 manifoldresult = auth_result
49 # let's use ManifoldResult.__repr__
50 env['state']="%s"%manifoldresult
52 return render_to_response(self.template,env, context_instance=RequestContext(request))
53 # user was authenticated at the backend
54 elif auth_result is not None:
60 if request.user.is_authenticated():
61 env['person'] = self.request.user
62 env['username'] = self.request.user
65 return render_to_response(self.template,env, context_instance=RequestContext(request))
67 env['state'] = "Your account is not active, please contact the site admin."
68 env['layout_1_or_2']="layout-unfold2.html"
70 return render_to_response(self.template,env, context_instance=RequestContext(request))
73 env['state'] = "Your username and/or password were incorrect."
75 return render_to_response(self.template, env, context_instance=RequestContext(request))
77 def get (self, request, state=None):
78 env = self.default_env()
80 if request.user.is_authenticated():
81 env['person'] = self.request.user
85 env['theme'] = self.theme
86 env['section'] = "Dashboard"
89 env['username']=the_user(request)
90 env['topmenu_items'] = topmenu_items(None, request)
91 if state: env['state'] = state
92 elif not env['username']: env['state'] = None
93 # use one or two columns for the layout - not logged in users will see the login prompt
95 # account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
96 # account_details = execute_query(self.request, account_query)
97 # for account_detail in account_details:
98 # account_config = json.loads(account_detail['config'])
99 # platform_name = platform_detail['platform']
100 # if 'myslice' in platform_detail['platform']:
101 # acc_user_cred = account_config.get('delegated_user_credential','N/A')
102 # acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
103 # acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
105 # if 'N/A' not in acc_user_cred:
106 # exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
108 # user_exp_date = exp_date.group(1)
109 # user_cred_exp_list.append(user_exp_date)
111 # my_users = [{'cred_exp': t[0]}
112 # for t in zip(user_cred_exp_list)]
115 # if 'N/A' not in acc_slice_cred:
116 # for key, value in acc_slice_cred.iteritems():
117 # slice_list.append(key)
118 # # get cred_exp date
119 # exp_date = re.search('<expires>(.*)</expires>', value)
121 # exp_date = exp_date.group(1)
122 # slice_cred_exp_list.append(exp_date)
124 # my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
125 # for t in zip(slice_list, slice_cred_exp_list)]
127 # if 'N/A' not in acc_auth_cred:
128 # for key, value in acc_auth_cred.iteritems():
129 # auth_list.append(key)
131 # exp_date = re.search('<expires>(.*)</expires>', value)
133 # exp_date = exp_date.group(1)
134 # auth_cred_exp_list.append(exp_date)
137 return render_to_response(self.template, env, context_instance=RequestContext(request))