Create slice: clears local storage now
[unfold.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
9 import json
10
11 from unfold.loginrequired import FreeAccessView
12
13 from manifold.core.query                import Query
14 from manifoldapi.manifoldapi            import execute_query
15
16 from manifoldapi.manifoldresult import ManifoldResult
17 from ui.topmenu import topmenu_items, the_user
18 from myslice.configengine import ConfigEngine
19
20 from myslice.theme import ThemeView
21
22 import activity.user
23
24 class HomeView (FreeAccessView, ThemeView):
25     template_name = 'home-view.html'
26         
27     # expose this so we can mention the backend URL on the welcome page
28     def default_env (self):
29         return { 
30                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
31                  }
32
33     def post (self,request):
34         env = self.default_env()
35         env['theme'] = self.theme
36         env['section'] = "Dashboard"
37         
38         username = request.POST.get('username')
39         password = request.POST.get('password')
40         
41         # pass request within the token, so manifold session key can be attached to the request session.
42         token = {'username': username, 'password': password, 'request': request}    
43
44         # our authenticate function returns either
45         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
46         # . a django User in case of success
47         # . or None if the backend could be reached but the authentication failed
48         auth_result = authenticate(token=token)
49         # use one or two columns for the layout - not logged in users will see the login prompt
50         # high-level errors, like connection refused or the like
51         if isinstance (auth_result, ManifoldResult):
52             manifoldresult = auth_result
53             # let's use ManifoldResult.__repr__
54             env['state']="%s"%manifoldresult
55             
56             return render_to_response(self.template,env, context_instance=RequestContext(request))
57         # user was authenticated at the backend
58         elif auth_result is not None:
59             user=auth_result
60             if user.is_active:
61                 print "LOGGING IN"
62                 login(request, user)
63                 
64                 if request.user.is_authenticated(): 
65                     env['person'] = self.request.user
66                     env['username'] = self.request.user
67                     
68                     # log user activity
69                     activity.user.login(self.request)
70                     
71                     ## check user is pi or not
72                     platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
73                     account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
74                     platform_details = execute_query(self.request, platform_query)
75                     account_details = execute_query(self.request, account_query)
76                     for platform_detail in platform_details:
77                         for account_detail in account_details:
78                             if platform_detail['platform_id'] == account_detail['platform_id']:
79                                 if 'config' in account_detail and account_detail['config'] is not '':
80                                     account_config = json.loads(account_detail['config'])
81                                     if 'myslice' in platform_detail['platform']:
82                                         acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
83                     # assigning values
84                     if acc_auth_cred=={} or acc_auth_cred=='N/A':
85                         pi = "is_not_pi"
86                     else:
87                         pi = "is_pi"
88
89                     env['pi'] = pi                
90                 else: 
91                     env['person'] = None
92                 return render_to_response(self.template,env, context_instance=RequestContext(request))
93             else:
94                 # log user activity
95                 activity.user.login(self.request, "notactive")
96                 env['state'] = "Your account is not active, please contact the site admin."
97                 env['layout_1_or_2']="layout-unfold2.html"
98                 
99                 return render_to_response(self.template,env, context_instance=RequestContext(request))
100         # otherwise
101         else:
102             # log user activity
103             activity.user.login(self.request, "error")
104             env['state'] = "Your username and/or password were incorrect."
105             
106             return render_to_response(self.template, env, context_instance=RequestContext(request))
107
108     def get (self, request, state=None):
109         env = self.default_env()
110         acc_auth_cred={}
111         if request.user.is_authenticated():
112             ## check user is pi or not
113             platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
114             account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
115             # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
116             platform_details = execute_query(self.request, platform_query)
117             account_details = execute_query(self.request, account_query)
118             if platform_details is None:
119                 env['person'] = None
120             else:
121                 for platform_detail in platform_details:
122                     for account_detail in account_details:
123                         if 'platform_id' in platform_detail:
124                             if platform_detail['platform_id'] == account_detail['platform_id']:
125                                 if 'config' in account_detail and account_detail['config'] is not '':
126                                     account_config = json.loads(account_detail['config'])
127                                     if 'myslice' in platform_detail['platform']:
128                                         acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
129                 # assigning values
130                 if acc_auth_cred=={} or acc_auth_cred=='N/A':
131                     pi = "is_not_pi"
132                 else:
133                     pi = "is_pi"
134             
135                 env['pi'] = pi 
136                 env['person'] = self.request.user
137         else: 
138             env['person'] = None
139
140         env['theme'] = self.theme
141         env['section'] = "Dashboard"
142
143
144         env['username']=the_user(request)
145         env['topmenu_items'] = topmenu_items(None, request)
146         if state: env['state'] = state
147         elif not env['username']: env['state'] = None
148         # use one or two columns for the layout - not logged in users will see the login prompt
149         
150 #         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
151 #         account_details = execute_query(self.request, account_query)
152 #         for account_detail in account_details:
153 #             account_config = json.loads(account_detail['config'])
154 #             platform_name = platform_detail['platform']
155 #             if 'myslice' in platform_detail['platform']:
156 #                 acc_user_cred = account_config.get('delegated_user_credential','N/A')
157 #                 acc_slice_cred = account_config.get('delegated_slice_credentials','N/A')
158 #                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
159
160 #                 if 'N/A' not in acc_user_cred:
161 #                     exp_date = re.search('<expires>(.*)</expires>', acc_user_cred)
162 #                     if exp_date:
163 #                         user_exp_date = exp_date.group(1)
164 #                         user_cred_exp_list.append(user_exp_date)
165
166 #                     my_users = [{'cred_exp': t[0]}
167 #                         for t in zip(user_cred_exp_list)]
168 #                
169
170 #                 if 'N/A' not in acc_slice_cred:
171 #                     for key, value in acc_slice_cred.iteritems():
172 #                         slice_list.append(key)
173 #                         # get cred_exp date
174 #                         exp_date = re.search('<expires>(.*)</expires>', value)
175 #                         if exp_date:
176 #                             exp_date = exp_date.group(1)
177 #                             slice_cred_exp_list.append(exp_date)
178
179 #                     my_slices = [{'slice_name': t[0], 'cred_exp': t[1]}
180 #                         for t in zip(slice_list, slice_cred_exp_list)]
181
182 #                 if 'N/A' not in acc_auth_cred:
183 #                     for key, value in acc_auth_cred.iteritems():
184 #                         auth_list.append(key)
185 #                         #get cred_exp date
186 #                         exp_date = re.search('<expires>(.*)</expires>', value)
187 #                         if exp_date:
188 #                             exp_date = exp_date.group(1)
189 #                             auth_cred_exp_list.append(exp_date)
190
191         
192         return render_to_response(self.template, env, context_instance=RequestContext(request))
193