add/remove to/from project (authority)
[myslice.git] / portal / institution.py
1 import json
2
3 from django.core.context_processors import csrf
4 from django.http                    import HttpResponseRedirect
5 from django.contrib.auth            import authenticate, login, logout
6 from django.template                import RequestContext
7 from django.shortcuts               import render_to_response
8 from django.shortcuts               import render
9
10 from unfold.page                    import Page
11 from unfold.loginrequired           import LoginRequiredAutoLogoutView
12
13 from manifold.core.query            import Query
14 from manifoldapi.manifoldapi        import execute_query
15 from manifoldapi.manifoldresult     import ManifoldResult
16 from ui.topmenu                     import topmenu_items, the_user
17 from myslice.configengine           import ConfigEngine
18
19 from portal.actions                 import is_pi, authority_check_pis
20 from myslice.theme                  import ThemeView
21 from myslice.settings               import logger
22
23
24 class InstitutionView (LoginRequiredAutoLogoutView, ThemeView):
25     template_name = 'institution.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['request'] = request
37         return render_to_response(self.template, env, context_instance=RequestContext(request))
38
39     def get (self, request, authority_hrn=None, state=None):
40         env = self.default_env()
41         if request.user.is_authenticated(): 
42             env['person'] = self.request.user
43             if authority_hrn is None: 
44                 # CACHE PB with fields
45                 page = Page(request)
46                 metadata = page.get_metadata()
47                 user_md = metadata.details_by_object('user')
48                 user_fields = [column['name'] for column in user_md['column']]
49                 
50                 # REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
51                 user_query  = Query().get('myslice:user').select(user_fields).filter_by('user_hrn','==','$user_hrn')
52                 #user_query  = Query().get('myslice:user').select('user_hrn','parent_authority').filter_by('user_hrn','==','$user_hrn')
53                 user_details = execute_query(self.request, user_query)
54                 try:
55                     env['user_details'] = user_details[0]
56                 except Exception,e:
57                     # If the Query fails, check in local DB 
58                     try:
59                         user_local_query  = Query().get('local:user').select('config').filter_by('email','==',str(env['person']))
60                         user_local_details = execute_query(self.request, user_local_query)
61                         user_local = user_local_details[0]            
62                         user_local_config = user_local['config']
63                         user_local_config = json.loads(user_local_config)
64                         user_local_authority = user_local_config.get('authority')
65                         if 'user_details' not in env or 'parent_authority' not in env['user_details'] or env['user_details']['parent_authority'] is None:
66                             env['user_details'] = {'parent_authority': user_local_authority}
67                     except Exception,e:
68                         env['error'] = "Please check your Credentials"
69             else:
70                 env['project'] = True
71                 env['user_details'] = {'parent_authority': authority_hrn}
72
73             logger.debug("BEFORE  ####------####  is_pi")
74             logger.debug("is_pi = {}".format(is_pi))
75             pi = is_pi(self.request, '$user_hrn', env['user_details']['parent_authority']) 
76         else: 
77             env['person'] = None
78             pi = False
79         env['theme'] = self.theme
80         env['section'] = "Institution"
81         env['pi'] = pi 
82         env['username']=the_user(request)
83         env['topmenu_items'] = topmenu_items(None, request)
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         env['request'] = request
90         return render_to_response(self.template, env, context_instance=RequestContext(request))
91