8bde68ca5039ee272d62c19f416e9344c8894869
[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').lower()
36         password = request.POST.get('password')
37        
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}    
42         #else:
43         
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}    
47
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
59             
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:
63             user=auth_result
64             if user.is_active:
65                 print "LOGGING IN"
66                 login(request, user)
67                 
68                 if request.user.is_authenticated(): 
69                     env['person'] = self.request.user
70                     env['username'] = self.request.user
71                     
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')
84                     # assigning values
85                     if acc_auth_cred=={} or acc_auth_cred=='N/A':
86                         pi = "is_not_pi"
87                     else:
88                         pi = "is_pi"
89
90                     env['pi'] = pi                
91                 else: 
92                     env['person'] = None
93                 return render_to_response(self.template,env, context_instance=RequestContext(request))
94             else:
95                 env['state'] = "Your account is not active, please contact the site admin."
96                 env['layout_1_or_2']="layout-unfold2.html"
97                 
98                 return render_to_response(self.template,env, context_instance=RequestContext(request))
99         # otherwise
100         else:
101             env['state'] = "Your username and/or password were incorrect."
102             
103             return render_to_response(self.template, env, context_instance=RequestContext(request))
104
105     def get (self, request, state=None):
106         env = self.default_env()
107         acc_auth_cred={}
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')
123             # assigning values
124             if acc_auth_cred=={} or acc_auth_cred=='N/A':
125                 pi = "is_not_pi"
126             else:
127                 pi = "is_pi"
128
129             env['pi'] = pi     
130             env['person'] = self.request.user
131         else: 
132             env['person'] = None
133
134         env['theme'] = self.theme
135         env['section'] = "Dashboard"
136
137
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
143         
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')
153
154 #                 if 'N/A' not in acc_user_cred:
155 #                     exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
156 #                     if exp_date:
157 #                         user_exp_date = exp_date.group(1)
158 #                         user_cred_exp_list.append(user_exp_date)
159
160 #                     my_users = [{'cred_exp': t[0]}
161 #                         for t in zip(user_cred_exp_list)]
162 #                
163
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)
169 #                         if exp_date:
170 #                             exp_date = exp_date.group(1)
171 #                             slice_cred_exp_list.append(exp_date)
172
173 #                     my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
174 #                         for t in zip(slice_list, slice_cred_exp_list)]
175
176 #                 if 'N/A' not in acc_auth_cred:
177 #                     for key, value in acc_auth_cred.iteritems():
178 #                         auth_list.append(key)
179 #                         #get cred_exp date
180 #                         exp_date = re.search('<expires>(.*)</expires>', value)
181 #                         if exp_date:
182 #                             exp_date = exp_date.group(1)
183 #                             auth_cred_exp_list.append(exp_date)
184
185         
186         return render_to_response(self.template, env, context_instance=RequestContext(request))
187