manage institution: hide slices, show only in projects
[myslice.git] / portal / servicedirectory.py
1 from django.core.context_processors import csrf
2 from django.http                    import HttpResponseRedirect
3 from django.contrib.auth            import authenticate, login, logout
4 from django.template                import RequestContext
5 from django.shortcuts               import render_to_response
6 from django.shortcuts               import render
7
8 from unfold.loginrequired           import LoginRequiredAutoLogoutView
9
10 from manifold.core.query            import Query
11 from manifoldapi.manifoldapi        import execute_query
12 from manifoldapi.manifoldresult     import ManifoldResult
13 from ui.topmenu                     import topmenu_items, the_user
14 from myslice.configengine           import ConfigEngine
15
16 from myslice.theme                          import ThemeView
17 import json
18
19 class ServiceDirectoryView (LoginRequiredAutoLogoutView, ThemeView):
20     template_name = 'servicedirectory.html'
21     server_url = "http://157.193.215.125:4001/service-directory/"
22         
23     # expose this so we can mention the backend URL on the welcome page
24     def default_env (self):
25         return { 
26                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
27                  }
28
29     def post (self,request):
30         env = self.default_env()
31         env['theme'] = self.theme
32         return render_to_response(self.template, env, context_instance=RequestContext(request))
33
34     def get (self, request, state=None):
35         env = self.default_env()
36
37         if request.user.is_authenticated(): 
38             env['person'] = self.request.user
39             # user_query  = Query().get('user').select('user_hrn','parent_authority').filter_by('user_hrn','==','$user_hrn')
40             # user_details = execute_query(self.request, user_query)
41             # try:
42             #     env['user_details'] = user_details[0]
43             # except Exception,e:
44             #     env['error'] = "Please check your Credentials"
45             
46             # try:
47             #     user_local_query  = Query().get('local:user').select('config').filter_by('email','==',str(env['person']))
48             #     user_local_details = execute_query(self.request, user_local_query)
49             #     user_local = user_local_details[0]            
50             #     user_local_config = user_local['config']
51             #     user_local_config = json.loads(user_local_config)
52             #     user_local_authority = user_local_config.get('authority')
53             #     if 'user_details' not in env or 'parent_authority' not in env['user_details'] or env['user_details']['parent_authority'] is None:
54             #         env['user_details'] = {'parent_authority': user_local_authority}
55             # except Exception,e:
56             #     env['error'] = "Please check your Manifold user config"
57             # ## check user is pi or not
58             # platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
59             # account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
60             # platform_details = execute_query(self.request, platform_query)
61             # account_details = execute_query(self.request, account_query)
62             # for platform_detail in platform_details:
63             #     for account_detail in account_details:
64             #         if platform_detail['platform_id'] == account_detail['platform_id']:
65             #             if 'config' in account_detail and account_detail['config'] is not '':
66             #                 account_config = json.loads(account_detail['config'])
67             #                 if 'myslice' in platform_detail['platform']:
68             #                     acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
69             # # assigning values
70             # if acc_auth_cred == {}:
71             #     pi = "is_not_pi"
72             # else:
73             #     pi = "is_pi"
74
75         else: 
76             env['person'] = None
77     
78         env['theme'] = self.theme
79         env['section'] = "Institution"
80         # env['pi'] = pi 
81         env['username']=the_user(request)
82         env['topmenu_items'] = topmenu_items(None, request)
83         env['servdirurl'] = self.server_url
84         if state: env['state'] = state
85         elif not env['username']: env['state'] = None
86         # use one or two columns for the layout - not logged in users will see the login prompt
87         env['layout_1_or_2']="layout-unfold2.html" if not env['username'] else "layout-unfold1.html"
88         
89         
90         return render_to_response(self.template, env, context_instance=RequestContext(request))
91