4 # this somehow is not used anymore - should it not be ?
5 from django.core.context_processors import csrf
6 from django.http import HttpResponseRedirect
7 from django.contrib.auth import authenticate, login, logout
8 from django.template import RequestContext
9 from django.shortcuts import render_to_response
10 from django.shortcuts import render
12 from unfold.loginrequired import FreeAccessView
13 from ui.topmenu import topmenu_items, the_user
15 from manifold.core.query import Query
16 from manifoldapi.manifoldapi import execute_query
17 from manifoldapi.manifoldresult import ManifoldResult
19 from myslice.configengine import ConfigEngine
20 from myslice.theme import ThemeView
22 from portal.account import Account, get_expiration
23 from portal.models import PendingSlice
24 from portal.actions import authority_check_pis, get_jfed_identity, get_myslice_account
28 class HomeView (FreeAccessView, ThemeView):
29 template_name = 'home-view.html'
31 # expose this so we can mention the backend URL on the welcome page
32 def default_env (self):
34 'MANIFOLD_URL':ConfigEngine().manifold_url(),
37 def post (self,request):
38 env = self.default_env()
39 env['theme'] = self.theme
40 env['section'] = "Dashboard"
42 username = request.POST.get('username')
43 password = request.POST.get('password')
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 # user was authenticated at the backend
61 elif auth_result is not None:
66 if request.user.is_authenticated():
67 env['person'] = self.request.user
68 env['username'] = self.request.user
71 activity.user.login(self.request)
73 ## check user is pi or not
77 account_detail = get_myslice_account(self.request)
78 if 'config' in account_detail and account_detail['config'] is not '':
79 account_config = json.loads(account_detail['config'])
80 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
81 acc_user_cred = account_config.get('delegated_user_credential','N/A')
83 #if acc_auth_cred=={} or acc_auth_cred=='N/A':
87 user_email = str(self.request.user)
88 pi = authority_check_pis(self.request, user_email)
90 # check if the user has creds or not
91 if acc_user_cred == {} or acc_user_cred == 'N/A':
92 user_cred = 'no_creds'
94 exp_date = get_expiration(acc_user_cred, 'timestamp')
95 if exp_date < time.time():
96 user_cred = 'creds_expired'
98 user_cred = 'has_creds'
100 # list the pending slices of this user
102 for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all():
103 pending_slices.append(slices.slice_name)
105 env['pending_slices'] = pending_slices
107 env['user_cred'] = user_cred
112 activity.user.login(self.request, "notactive")
113 env['state'] = "Your account is not active, please contact the site admin."
114 env['layout_1_or_2']="layout-unfold2.html"
116 jfed_identity = get_jfed_identity(request)
117 if jfed_identity is not None:
119 encoded_jfed_identity = base64.b64encode(jfed_identity)
120 env['jfed_identity'] = encoded_jfed_identity
122 env['jfed_identity'] = None
127 activity.user.login(self.request, "error")
128 env['state'] = "Your username and/or password were incorrect."
129 env['request'] = request
130 return render_to_response(self.template,env, context_instance=RequestContext(request))
132 def get (self, request, state=None):
133 env = self.default_env()
138 if request.user.is_authenticated():
139 jfed_identity = get_jfed_identity(request)
140 if jfed_identity is not None:
142 encoded_jfed_identity = base64.b64encode(jfed_identity)
143 env['jfed_identity'] = encoded_jfed_identity
145 env['jfed_identity'] = None
147 ## check user is pi or not
148 platform_details = {}
152 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
153 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
154 # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
156 account_detail = get_myslice_account(self.request)
157 if 'config' in account_detail and account_detail['config'] is not '':
158 account_config = json.loads(account_detail['config'])
159 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
160 acc_user_cred = account_config.get('delegated_user_credential','N/A')
162 #if acc_auth_cred=={} or acc_auth_cred=='N/A':
166 user_email = str(self.request.user)
167 pi = authority_check_pis(self.request, user_email)
168 # check if the user has creds or not
169 if acc_user_cred == {} or acc_user_cred == 'N/A':
170 user_cred = 'no_creds'
172 exp_date = get_expiration(acc_user_cred, 'timestamp')
173 if exp_date < time.time():
174 user_cred = 'creds_expired'
176 user_cred = 'has_creds'
178 # list the pending slices of this user
180 for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all():
181 pending_slices.append(slices.slice_name)
183 env['pending_slices'] = pending_slices
185 env['user_cred'] = user_cred
186 env['person'] = self.request.user
190 env['theme'] = self.theme
191 env['section'] = "Dashboard"
193 env['username']=the_user(request)
194 env['topmenu_items'] = topmenu_items(None, request)
195 env['request'] = request
196 if state: env['state'] = state
197 elif not env['username']: env['state'] = None
198 # use one or two columns for the layout - not logged in users will see the login prompt
199 return render_to_response(self.template, env, context_instance=RequestContext(request))