AiC and REST login
[myslice.git] / portal / projectrequestview.py
1 import requests
2 import json
3 import time
4 import re
5
6 from django.shortcuts           import render
7 from django.contrib.sites.models import Site
8
9 from manifold.core.query        import Query
10 from manifoldapi.manifoldapi    import execute_admin_query, execute_query
11
12 from unfold.loginrequired       import LoginRequiredAutoLogoutView
13
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
16
17 from myslice.theme import ThemeView
18 from myslice.settings import logger
19
20 class ProjectRequestView(LoginRequiredAutoLogoutView, ThemeView):
21     template_name = 'projectrequest_view.html'
22     
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')
30         return user_authority
31     
32     def getUserHrn(self, request):
33         user_hrn = None
34         
35         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
36         account_details = execute_query(request, account_query)
37
38         platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
39         platform_details = execute_query(request, platform_query)
40         
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')
50         return user_hrn        
51
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')
57         return user_email
58                    
59     def post(self, request):
60         return self.handle_request(request, 'POST')
61
62     def get(self, request):
63         return self.handle_request(request, 'GET')
64
65     def handle_request(self, wsgi_request, method):
66         errors = []
67         authority_hrn = None
68         authority_name = None
69         
70         #errors.append(wsgi_request.POST)
71
72         user_hrn = self.getUserHrn(wsgi_request)
73
74         user_email = self.getUserEmail(wsgi_request)
75         
76         authorities = getAuthorities(wsgi_request)
77         
78         user_authority = self.getUserAuthority(wsgi_request)
79         
80         # getting the org from authority
81         #for authority in authorities:
82         #    if authority['authority_hrn'] == user_authority:
83         #        authority_name = authority['name']
84         
85         if method == 'POST' :
86
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')
90
91             # accept only lowercase names
92             project_name = project_name.lower()
93
94             if 'join' in wsgi_request.POST:
95                 post = {
96                     'user_hrn'          : user_hrn,
97                     'email'             : user_email,
98                     'project_name'      : project_name,
99                     'authority_hrn'     : project_name,
100                 }
101
102             else:
103                 post = {
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', ''),
110                 }
111
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')
115
116                 #if (post['authority_hrn'] is None or post['authority_hrn'] == ''):
117                 #    errors.append('Organization is mandatory')
118     
119                 if post['purpose'] is None or post['purpose'] == '':
120                     errors.append('Project purpose is mandatory')
121
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.')
124
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')   
128             
129             if not errors:
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)
136                     else:
137                         # Create project
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'
142                 else:
143                     # Otherwise a wsgi_request is sent to the PI
144                     if 'join' in wsgi_request.POST:
145                         create_pending_join(wsgi_request, post)
146                     else:
147                         create_pending_project(wsgi_request, post)
148                     self.template_name = 'project-request-ack-view.html'
149
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)
154
155         root_authority = user_authority.split('.', 1)[0]                  
156         env = {
157                'errors':        errors,
158                'username':      wsgi_request.user,
159                'theme':         self.theme,
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,
165         }
166         return render(wsgi_request, self.template, env)