blob: 1e95b2b3dfbebfba93c1dc84f6259b3e80672054 [file] [log] [blame]
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is govered by a BSD-style
# license that can be found in the LICENSE file or at
"""A servlet for project owners to create a new component def."""
import logging
import time
from framework import framework_helpers
from framework import framework_views
from framework import jsonfeed
from framework import permissions
from framework import servlet
from framework import urls
from tracker import component_helpers
from tracker import tracker_bizobj
from tracker import tracker_constants
from tracker import tracker_views
from third_party import ezt
class ComponentCreate(servlet.Servlet):
"""Servlet allowing project owners to create a component."""
_PAGE_TEMPLATE = 'tracker/component-create-page.ezt'
def AssertBasePermission(self, mr):
"""Check whether the user has any permission to visit this page.
mr: commonly used info parsed from the request.
super(ComponentCreate, self).AssertBasePermission(mr)
if not self.CheckPerm(mr, permissions.EDIT_PROJECT):
raise permissions.PermissionException(
'User is not allowed to administer this project')
def GatherPageData(self, mr):
"""Build up a dictionary of data values to use when rendering the page.
mr: commonly used info parsed from the request.
Dict of values used by EZT for rendering the page.
config =, mr.project_id)
users_by_id = framework_views.MakeAllUserViews(
*[list(cd.admin_ids) + list(cd.cc_ids)
for cd in config.component_defs])
component_def_views = [
tracker_views.ComponentDefView(cd, users_by_id)
# TODO(jrobbins): future component-level view restrictions.
for cd in config.component_defs]
for cdv in component_def_views:
setattr(cdv, 'selected', None)
path = (cdv.parent_path + '>' + cdv.leaf_name).lstrip('>')
if path == mr.component_path:
setattr(cdv, 'selected', True)
return {
'parent_path': mr.component_path,
'admin_tab_mode': servlet.Servlet.PROCESS_TAB_COMPONENTS,
'component_defs': component_def_views,
'initial_leaf_name': '',
'initial_docstring': '',
'initial_deprecated': ezt.boolean(False),
'initial_admins': [],
'initial_cc': [],
def ProcessFormData(self, mr, post_data):
"""Validate and store the contents of the issues tracker admin page.
mr: commonly used info parsed from the request.
post_data: HTML form data from the request.
String URL to redirect the user to, or None if response was already sent.
config =, mr.project_id)
parent_path = post_data.get('parent_path', '')
parsed = component_helpers.ParseComponentRequest(
mr, post_data,
if parent_path:
parent_def = tracker_bizobj.FindComponentDef(parent_path, config)
if not parent_def:
self.abort(500, 'parent component not found')
allow_parent_edit = permissions.CanEditComponentDef(
mr.auth.effective_ids, mr.perms, mr.project, parent_def, config)
if not allow_parent_edit:
raise permissions.PermissionException(
'User is not allowed to add a subcomponent here')
path = '%s>%s' % (parent_path, parsed.leaf_name)
path = parsed.leaf_name
leaf_name_error_msg = LeafNameErrorMessage(
parent_path, parsed.leaf_name, config)
if leaf_name_error_msg:
mr.errors.leaf_name = leaf_name_error_msg
if mr.errors.AnyErrors():
mr, parent_path=parent_path,
created = int(time.time())
creator_id =
mr.cnxn,, autocreate=False)
mr.cnxn, mr.project_id, path, parsed.docstring, parsed.deprecated,
parsed.admin_ids, parsed.cc_ids, created, creator_id)
return framework_helpers.FormatAbsoluteURL(
mr, urls.ADMIN_COMPONENTS, saved=1, ts=int(time.time()))
class CheckComponentNameJSON(jsonfeed.JsonFeed):
"""JSON data for handling name checks when creating a component."""
def HandleRequest(self, mr):
"""Provide the UI with info about the availability of the component name.
mr: common information parsed from the HTTP request.
Results dictionary in JSON format.
parent_path = mr.GetParam('parent_path')
leaf_name = mr.GetParam('leaf_name')
config =, mr.project_id)
message = LeafNameErrorMessage(parent_path, leaf_name, config)
return {
'error_message': message,
def LeafNameErrorMessage(parent_path, leaf_name, config):
"""Return an error message for the given component name, or None."""
if not tracker_constants.COMPONENT_NAME_RE.match(leaf_name):
return 'Invalid component name'
if parent_path:
path = '%s>%s' % (parent_path, leaf_name)
path = leaf_name
if tracker_bizobj.FindComponentDef(path, config):
return 'That name is already in use.'
return None