Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[myslice.git] / portal / homeview.py
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
8 import json
9
10 from unfold.loginrequired import FreeAccessView
11
12 from manifold.core.query                import Query
13 from manifoldapi.manifoldapi            import execute_query
14
15 from manifoldapi.manifoldresult import ManifoldResult
16 from ui.topmenu import topmenu_items, the_user
17 from myslice.configengine import ConfigEngine
18
19 from myslice.theme import ThemeView
20
21 class HomeView (FreeAccessView, ThemeView):
22     template_name = 'home-view.html'
23         
24     # expose this so we can mention the backend URL on the welcome page
25     def default_env (self):
26         return { 
27                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
28                  }
29
30     def post (self,request):
31         env = self.default_env()
32         env['theme'] = self.theme
33         env['section'] = "Dashboard"
34         
35         username = request.POST.get('username')
36         password = request.POST.get('password')
37         
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}    
40
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
52             
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:
56             user=auth_result
57             if user.is_active:
58                 print "LOGGING IN"
59                 login(request, user)
60                 
61                 if request.user.is_authenticated(): 
62                     env['person'] = self.request.user
63                     env['username'] = self.request.user
64                     
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')
77                     # assigning values
78                     if acc_auth_cred == {}:
79                         pi = "is_not_pi"
80                     else:
81                         pi = "is_pi"
82
83                     print "testing"
84                     print pi
85  
86                     env['pi'] = pi                
87                 else: 
88                     env['person'] = None
89                 return render_to_response(self.template,env, context_instance=RequestContext(request))
90             else:
91                 env['state'] = "Your account is not active, please contact the site admin."
92                 env['layout_1_or_2']="layout-unfold2.html"
93                 
94                 return render_to_response(self.template,env, context_instance=RequestContext(request))
95         # otherwise
96         else:
97             env['state'] = "Your username and/or password were incorrect."
98             
99             return render_to_response(self.template, env, context_instance=RequestContext(request))
100
101     def get (self, request, state=None):
102         env = self.default_env()
103
104         if request.user.is_authenticated(): 
105             env['person'] = self.request.user
106         else: 
107             env['person'] = None
108
109         env['theme'] = self.theme
110         env['section'] = "Dashboard"
111
112
113         env['username']=the_user(request)
114         env['topmenu_items'] = topmenu_items(None, request)
115         if state: env['state'] = state
116         elif not env['username']: env['state'] = None
117         # use one or two columns for the layout - not logged in users will see the login prompt
118         
119 #         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
120 #         account_details = execute_query(self.request, account_query)
121 #         for account_detail in account_details:
122 #             account_config = json.loads(account_detail['config'])
123 #             platform_name = platform_detail['platform']
124 #             if 'myslice' in platform_detail['platform']:
125 #                 acc_user_cred = account_config.get('delegated_user_credential','N/A')
126 #                 acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
127 #                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
128
129 #                 if 'N/A' not in acc_user_cred:
130 #                     exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
131 #                     if exp_date:
132 #                         user_exp_date = exp_date.group(1)
133 #                         user_cred_exp_list.append(user_exp_date)
134
135 #                     my_users = [{'cred_exp': t[0]}
136 #                         for t in zip(user_cred_exp_list)]
137 #                
138
139 #                 if 'N/A' not in acc_slice_cred:
140 #                     for key, value in acc_slice_cred.iteritems():
141 #                         slice_list.append(key)
142 #                         # get cred_exp date
143 #                         exp_date = re.search('<expires>(.*)</expires>', value)
144 #                         if exp_date:
145 #                             exp_date = exp_date.group(1)
146 #                             slice_cred_exp_list.append(exp_date)
147
148 #                     my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
149 #                         for t in zip(slice_list, slice_cred_exp_list)]
150
151 #                 if 'N/A' not in acc_auth_cred:
152 #                     for key, value in acc_auth_cred.iteritems():
153 #                         auth_list.append(key)
154 #                         #get cred_exp date
155 #                         exp_date = re.search('<expires>(.*)</expires>', value)
156 #                         if exp_date:
157 #                             exp_date = exp_date.group(1)
158 #                             auth_cred_exp_list.append(exp_date)
159
160         
161         return render_to_response(self.template, env, context_instance=RequestContext(request))
162