fix theme is defined as self.theme in the view
[myslice.git] / portal / projectrequestview.py
1 import json
2 import time
3 import re
4
5 from django.shortcuts           import render
6 from django.contrib.sites.models import Site
7
8 from manifold.core.query        import Query
9 from manifoldapi.manifoldapi    import execute_admin_query, execute_query
10
11 from unfold.loginrequired       import LoginRequiredAutoLogoutView
12
13 from portal.actions import create_pending_project, create_pending_join, sfa_add_authority, authority_add_pis, is_pi
14 from portal.models import PendingProject, PendingJoin
15
16 from myslice.theme import ThemeView
17 from myslice.settings import logger
18
19 class ProjectRequestView(LoginRequiredAutoLogoutView, ThemeView):
20     template_name = 'projectrequest_view.html'
21     
22     def getAuthorities(self, request):
23         if self.theme == 'fed4fire':
24             authorities_query = Query.get('myslice:authority').select('name', 'authority_hrn')
25         else:
26             authorities_query = Query.get('authority').select('name', 'authority_hrn')
27         authorities = execute_admin_query(request, authorities_query)
28         if authorities is not None:
29             # Remove the root authority from the list
30             matching = [s for s in authorities if "." in s['authority_hrn']]
31             authorities = sorted(matching, key=lambda k: k['authority_hrn'])
32             authorities = sorted(matching, key=lambda k: k['name'])
33         return authorities
34     
35     def getUserAuthority(self, request):
36         # Get user_email (XXX Would deserve to be simplified)
37         user_query  = Query().get('local:user').select('email','config')
38         user_details = execute_query(request, user_query)
39         for user_detail in user_details:
40             user_config = json.loads(user_detail['config'])
41             user_authority = user_config.get('authority','N/A')
42         return user_authority
43     
44     def getUserHrn(self, request):
45         user_hrn = None
46         
47         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
48         account_details = execute_query(request, account_query)
49
50         platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
51         platform_details = execute_query(request, platform_query)
52         
53         # getting user_hrn from local:account
54         for account_detail in account_details:
55             for platform_detail in platform_details:
56                 if platform_detail['platform_id'] == account_detail['platform_id']:
57                     # taking user_hrn only from myslice account
58                     # NOTE: we should later handle accounts filter_by auth_type= managed OR user
59                     if 'myslice' in platform_detail['platform']:
60                         account_config = json.loads(account_detail['config'])
61                         user_hrn = account_config.get('user_hrn','N/A')
62         return user_hrn        
63
64     def getUserEmail(self, request):
65         # Get user_email (XXX Would deserve to be simplified)
66         user_query  = Query().get('local:user').select('email','config')
67         user_details = execute_query(request, user_query)
68         user_email = user_details[0].get('email')
69         return user_email
70                    
71     def post(self, request):
72         return self.handle_request(request, 'POST')
73
74     def get(self, request):
75         return self.handle_request(request, 'GET')
76
77     def handle_request(self, wsgi_request, method):
78         errors = []
79         authority_hrn = None
80         authority_name = None
81         
82         #errors.append(wsgi_request.POST)
83
84         user_hrn = self.getUserHrn(wsgi_request)
85
86         user_email = self.getUserEmail(wsgi_request)
87         
88         authorities = self.getAuthorities(wsgi_request)
89         
90         user_authority = self.getUserAuthority(wsgi_request)
91         
92         # getting the org from authority
93         for authority in authorities:
94             if authority['authority_hrn'] == user_authority:
95                 authority_name = authority['name']
96         
97         if method == 'POST' :
98
99             project_name = wsgi_request.POST.get('project_name', '')
100             if not project_name or len(project_name) == 0 :
101                 errors.append('Project name can\'t be empty')
102
103             # accept only lowercase names
104             project_name = project_name.lower()
105
106             if 'join' in wsgi_request.POST:
107                 post = {
108                     'user_hrn'          : user_hrn,
109                     'email'             : user_email,
110                     'project_name'      : project_name,
111                     'authority_hrn'     : project_name,
112                 }
113
114             else:
115                 post = {
116                     'user_hrn'          : user_hrn,
117                     'email'             : user_email,
118                     'authority_hrn'     : wsgi_request.POST.get('authority_name', ''),
119                     'project_name'      : project_name,
120                     'purpose'           : wsgi_request.POST.get('purpose', ''),
121                 }
122
123                 # for new projects max project_name length is 10
124                 if (len(post['project_name']) >10):
125                     errors.append('Project name can be maximum 10 characters long')
126
127                 #if (post['authority_hrn'] is None or post['authority_hrn'] == ''):
128                 #    errors.append('Organization is mandatory')
129     
130                 if post['purpose'] is None or post['purpose'] == '':
131                     errors.append('Project purpose is mandatory')
132
133                 if re.search(r'^[A-Za-z0-9_]*$', post['project_name']) is None:
134                     errors.append('Project name may contain only letters, numbers, and underscore.')
135
136             # What kind of project name is valid?
137             if post['project_name'] is None or post['project_name'] == '':
138                 errors.append('Project name is mandatory')   
139             
140             if not errors:
141                 logger.info("is_pi on auth_hrn = {}".format(user_authority))
142                 if is_pi(wsgi_request, user_hrn, user_authority):
143                     # PIs can directly create/join project in their own authority...
144                     if 'join' in wsgi_request.POST:
145                         # join existing project
146                         authority_add_pis(wsgi_request, post['project_name'], user_hrn)
147                     else:
148                         # Create project
149                         hrn = post['authority_hrn'] + '.' + post['project_name']
150                         sfa_add_authority(wsgi_request, {'authority_hrn':hrn})
151                         authority_add_pis(wsgi_request, hrn, user_hrn)
152                     self.template_name = 'project-request-done-view.html'
153                 else:
154                     # Otherwise a wsgi_request is sent to the PI
155                     if 'join' in wsgi_request.POST:
156                         create_pending_join(wsgi_request, post)
157                     else:
158                         create_pending_project(wsgi_request, post)
159                     self.template_name = 'project-request-ack-view.html'
160
161         # retrieves the pending projects creation list
162         pending_projects = PendingProject.objects.all().filter(user_hrn=user_hrn)
163         # retrieves the pending join a project list
164         pending_join_projects = PendingJoin.objects.all().filter(user_hrn=user_hrn)
165
166         root_authority = user_authority.split('.', 1)[0]                  
167         env = {
168                'errors':        errors,
169                'username':      wsgi_request.user,
170                'theme':         self.theme,
171                'authorities':   authorities,
172                'authority_hrn': user_authority,
173                'root_authority_hrn': root_authority,
174                'pending_projects': pending_projects,
175                'pending_join_projects': pending_join_projects,
176         }
177         return render(wsgi_request, self.template, env)