trade-off title updated
[myslice.git] / portal / institution.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 InstitutionView (LoginRequiredAutoLogoutView, ThemeView):
20     template_name = 'institution.html'
21         
22     # expose this so we can mention the backend URL on the welcome page
23     def default_env (self):
24         return { 
25                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
26                  }
27
28     def post (self,request):
29         env = self.default_env()
30         env['theme'] = self.theme
31         return render_to_response(self.template, env, context_instance=RequestContext(request))
32
33     def get (self, request, state=None):
34         env = self.default_env()
35
36         if request.user.is_authenticated(): 
37             env['person'] = self.request.user
38             user_query  = Query().get('user').select('user_hrn','parent_authority').filter_by('user_hrn','==','$user_hrn')
39             user_details = execute_query(self.request, user_query)
40             try:
41                 env['user_details'] = user_details[0]
42             except Exception,e:
43                 env['error'] = "Please check your Credentials"
44             
45             try:
46                 user_local_query  = Query().get('local:user').select('config').filter_by('email','==',str(env['person']))
47                 user_local_details = execute_query(self.request, user_local_query)
48                 user_local = user_local_details[0]            
49                 user_local_config = user_local['config']
50                 user_local_config = json.loads(user_local_config)
51                 user_local_authority = user_local_config.get('authority')
52                 if 'user_details' not in env or 'parent_authority' not in env['user_details'] or env['user_details']['parent_authority'] is None:
53                     env['user_details'] = {'parent_authority': user_local_authority}
54             except Exception,e:
55                 env['error'] = "Please check your Manifold user config"
56             ## check user is pi or not
57             platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
58             account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
59             platform_details = execute_query(self.request, platform_query)
60             account_details = execute_query(self.request, account_query)
61             for platform_detail in platform_details:
62                 for account_detail in account_details:
63                     if platform_detail['platform_id'] == account_detail['platform_id']:
64                         if 'config' in account_detail and account_detail['config'] is not '':
65                             account_config = json.loads(account_detail['config'])
66                             if 'myslice' in platform_detail['platform']:
67                                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
68             # assigning values
69             if acc_auth_cred == {} or acc_auth_cred == 'N/A':
70                 pi = "is_not_pi"
71             else:
72                 pi = "is_pi"
73
74         else: 
75             env['person'] = None
76     
77         env['theme'] = self.theme
78         env['section'] = "Institution"
79         env['pi'] = pi 
80         env['username']=the_user(request)
81         env['topmenu_items'] = topmenu_items(None, request)
82         if state: env['state'] = state
83         elif not env['username']: env['state'] = None
84         # use one or two columns for the layout - not logged in users will see the login prompt
85         env['layout_1_or_2']="layout-unfold2.html" if not env['username'] else "layout-unfold1.html"
86         
87         
88         return render_to_response(self.template, env, context_instance=RequestContext(request))
89