6 from django.shortcuts import render
7 from django.contrib.sites.models import Site
9 from manifold.core.query import Query
10 from manifoldapi.manifoldapi import execute_admin_query, execute_query
12 from unfold.loginrequired import LoginRequiredAutoLogoutView
14 from portal.actions import create_pending_project, create_pending_join, sfa_add_authority, authority_add_pis, is_pi, getAuthorities
15 from portal.models import PendingProject, PendingJoin
17 from myslice.theme import ThemeView
18 from myslice.settings import logger
20 class ProjectRequestView(LoginRequiredAutoLogoutView, ThemeView):
21 template_name = 'projectrequest_view.html'
23 def getUserAuthority(self, request):
24 # Get user_email (XXX Would deserve to be simplified)
25 user_query = Query().get('local:user').select('email','config')
26 user_details = execute_query(request, user_query)
27 for user_detail in user_details:
28 user_config = json.loads(user_detail['config'])
29 user_authority = user_config.get('authority','N/A')
32 def getUserHrn(self, request):
35 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
36 account_details = execute_query(request, account_query)
38 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
39 platform_details = execute_query(request, platform_query)
41 # getting user_hrn from local:account
42 for account_detail in account_details:
43 for platform_detail in platform_details:
44 if platform_detail['platform_id'] == account_detail['platform_id']:
45 # taking user_hrn only from myslice account
46 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
47 if 'myslice' in platform_detail['platform']:
48 account_config = json.loads(account_detail['config'])
49 user_hrn = account_config.get('user_hrn','N/A')
52 def getUserEmail(self, request):
53 # Get user_email (XXX Would deserve to be simplified)
54 user_query = Query().get('local:user').select('email','config')
55 user_details = execute_query(request, user_query)
56 user_email = user_details[0].get('email')
59 def post(self, request):
60 return self.handle_request(request, 'POST')
62 def get(self, request):
63 return self.handle_request(request, 'GET')
65 def handle_request(self, wsgi_request, method):
70 #errors.append(wsgi_request.POST)
72 user_hrn = self.getUserHrn(wsgi_request)
74 user_email = self.getUserEmail(wsgi_request)
76 authorities = getAuthorities(wsgi_request)
78 user_authority = self.getUserAuthority(wsgi_request)
80 # getting the org from authority
81 #for authority in authorities:
82 # if authority['authority_hrn'] == user_authority:
83 # authority_name = authority['name']
87 project_name = wsgi_request.POST.get('project_name', '')
88 if not project_name or len(project_name) == 0 :
89 errors.append('Project name can\'t be empty')
91 # accept only lowercase names
92 project_name = project_name.lower()
94 if 'join' in wsgi_request.POST:
96 'user_hrn' : user_hrn,
98 'project_name' : project_name,
99 'authority_hrn' : project_name,
104 'user_hrn' : user_hrn,
105 'email' : user_email,
106 'authority_hrn' : wsgi_request.POST.get('authority_name', ''),
107 'project_name' : project_name,
108 'purpose' : wsgi_request.POST.get('purpose', ''),
109 'url' : wsgi_request.POST.get('url', ''),
112 # for new projects max project_name length is 10
113 if (len(post['project_name']) >10):
114 errors.append('Project name can be maximum 10 characters long')
116 #if (post['authority_hrn'] is None or post['authority_hrn'] == ''):
117 # errors.append('Organization is mandatory')
119 if post['purpose'] is None or post['purpose'] == '':
120 errors.append('Project purpose is mandatory')
122 if re.search(r'^[A-Za-z0-9_]*$', post['project_name']) is None:
123 errors.append('Project name may contain only letters, numbers, and underscore.')
125 # What kind of project name is valid?
126 if post['project_name'] is None or post['project_name'] == '':
127 errors.append('Project name is mandatory')
130 logger.info("is_pi on auth_hrn = {}".format(user_authority))
131 if is_pi(wsgi_request, user_hrn, post['authority_hrn']):
132 # PIs can directly create/join project in their own authority...
133 if 'join' in wsgi_request.POST:
134 # join existing project
135 authority_add_pis(wsgi_request, post['project_name'], user_hrn)
138 hrn = post['authority_hrn'] + '.' + post['project_name']
139 sfa_add_authority(wsgi_request, {'authority_hrn':hrn})
140 authority_add_pis(wsgi_request, hrn, user_hrn)
141 self.template_name = 'project-request-done-view.html'
143 # Otherwise a wsgi_request is sent to the PI
144 if 'join' in wsgi_request.POST:
145 create_pending_join(wsgi_request, post)
147 create_pending_project(wsgi_request, post)
148 self.template_name = 'project-request-ack-view.html'
150 # retrieves the pending projects creation list
151 pending_projects = PendingProject.objects.all().filter(user_hrn=user_hrn)
152 # retrieves the pending join a project list
153 pending_join_projects = PendingJoin.objects.all().filter(user_hrn=user_hrn)
155 root_authority = user_authority.split('.', 1)[0]
158 'username': wsgi_request.user,
160 'authorities': authorities,
161 'authority_hrn': user_authority,
162 'root_authority_hrn': root_authority,
163 'pending_projects': pending_projects,
164 'pending_join_projects': pending_join_projects,
166 return render(wsgi_request, self.template, env)