blob: 37d02c81585519a80a1cbde20f8f01322330fb15 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Classes for users to create a new project."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11
12import logging
13from six import string_types
14import ezt
15
16import settings
17from businesslogic import work_env
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020018from framework import exceptions, flaskservlet
Copybara854996b2021-09-07 19:36:02 +000019from framework import filecontent
20from framework import framework_helpers
21from framework import gcs_helpers
22from framework import jsonfeed
23from framework import permissions
24from framework import servlet
25from framework import urls
26from project import project_constants
27from project import project_helpers
28from project import project_views
29from services import project_svc
30from tracker import tracker_bizobj
31from tracker import tracker_views
32
33
34_MSG_PROJECT_NAME_NOT_AVAIL = 'That project name is not available.'
35_MSG_MISSING_PROJECT_NAME = 'Missing project name'
36_MSG_INVALID_PROJECT_NAME = 'Invalid project name'
37_MSG_MISSING_PROJECT_SUMMARY = 'Missing project summary'
38
39
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020040class ProjectCreate(flaskservlet.FlaskServlet):
Copybara854996b2021-09-07 19:36:02 +000041 """Shows a page with a simple form to create a project."""
42
43 _PAGE_TEMPLATE = 'sitewide/project-create-page.ezt'
44
45 def AssertBasePermission(self, mr):
46 """Assert that the user has the permissions needed to view this page."""
47 super(ProjectCreate, self).AssertBasePermission(mr)
48
49 if not permissions.CanCreateProject(mr.perms):
50 raise permissions.PermissionException(
51 'User is not allowed to create a project')
52
53 def GatherPageData(self, _mr):
54 """Build up a dictionary of data values to use when rendering the page."""
55 available_access_levels = project_helpers.BuildProjectAccessOptions(None)
56 offer_access_level = len(available_access_levels) > 1
57 if settings.default_access_level:
58 access_view = project_views.ProjectAccessView(
59 settings.default_access_level)
60 else:
61 access_view = None
62
63 return {
64 'initial_name': '',
65 'initial_summary': '',
66 'initial_description': '',
67 'initial_project_home': '',
68 'initial_docs_url': '',
69 'initial_source_url': '',
70 'initial_logo_gcs_id': '',
71 'initial_logo_file_name': '',
72 'logo_view': tracker_views.LogoView(None),
73 'labels': [],
74 'max_project_name_length': project_constants.MAX_PROJECT_NAME_LENGTH,
75 'offer_access_level': ezt.boolean(offer_access_level),
76 'initial_access': access_view,
77 'available_access_levels': available_access_levels,
78 }
79
80 def ProcessFormData(self, mr, post_data):
81 """Process the posted form."""
82 # 1. Parse and validate user input.
83 # Project name is taken from post_data because we are creating it.
84 project_name = post_data.get('projectname')
85 if not project_name:
86 mr.errors.projectname = _MSG_MISSING_PROJECT_NAME
87 elif not project_helpers.IsValidProjectName(project_name):
88 mr.errors.projectname = _MSG_INVALID_PROJECT_NAME
89
90 summary = post_data.get('summary')
91 if not summary:
92 mr.errors.summary = _MSG_MISSING_PROJECT_SUMMARY
93 description = post_data.get('description', '')
94
95 access = project_helpers.ParseProjectAccess(None, post_data.get('access'))
96 home_page = post_data.get('project_home')
97 if home_page and not (
98 home_page.startswith('http://') or home_page.startswith('https://')):
99 mr.errors.project_home = 'Home page link must start with http(s)://'
100 docs_url = post_data.get('docs_url')
101 if docs_url and not (
102 docs_url.startswith('http:') or docs_url.startswith('https:')):
103 mr.errors.docs_url = 'Documentation link must start with http: or https:'
104
105 # These are not specified on via the ProjectCreate form,
106 # the user must edit the project after creation to set them.
107 committer_ids = []
108 contributor_ids = []
109
110 # Validate that provided logo is supported.
111 logo_provided = 'logo' in post_data and not isinstance(
112 post_data['logo'], string_types)
113 if logo_provided:
114 item = post_data['logo']
115 try:
116 gcs_helpers.CheckMimeTypeResizable(
117 filecontent.GuessContentTypeFromFilename(item.filename))
118 except gcs_helpers.UnsupportedMimeType, e:
119 mr.errors.logo = e.message
120
121 # 2. Call services layer to save changes.
122 if not mr.errors.AnyErrors():
123 with work_env.WorkEnv(mr, self.services) as we:
124 try:
125 project_id = we.CreateProject(
126 project_name, [mr.auth.user_id],
127 committer_ids, contributor_ids, summary, description,
128 access=access, home_page=home_page, docs_url=docs_url)
129
130 config = tracker_bizobj.MakeDefaultProjectIssueConfig(project_id)
131 self.services.config.StoreConfig(mr.cnxn, config)
132 # Note: No need to store any canned queries or rules yet.
133 self.services.issue.InitializeLocalID(mr.cnxn, project_id)
134
135 # Update project with logo if specified.
136 if logo_provided:
137 item = post_data['logo']
138 logo_file_name = item.filename
139 logo_gcs_id = gcs_helpers.StoreLogoInGCS(
140 logo_file_name, item.value, project_id)
141 we.UpdateProject(
142 project_id, logo_gcs_id=logo_gcs_id,
143 logo_file_name=logo_file_name)
144
145 except exceptions.ProjectAlreadyExists:
146 mr.errors.projectname = _MSG_PROJECT_NAME_NOT_AVAIL
147
148 # 3. Determine the next page in the UI flow.
149 if mr.errors.AnyErrors():
150 access_view = project_views.ProjectAccessView(access)
151 self.PleaseCorrect(
152 mr, initial_summary=summary, initial_description=description,
153 initial_name=project_name, initial_access=access_view)
154 else:
155 # Go to the new project's introduction page.
156 return framework_helpers.FormatAbsoluteURL(
157 mr, urls.ADMIN_INTRO, project_name=project_name)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200158
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200159 def GetCreateProject(self, **kwargs):
160 return self.handler(**kwargs)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200161
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200162 def PostCreateProject(self, **kwargs):
163 return self.handler(**kwargs)