コード例 #1
0
ファイル: field_helpers.py プロジェクト: xinghun61/infra
def ValidateCustomField(mr, project, services, field_def, field_val):
  """Validate one custom field value and return an error string or None."""
  if field_def.field_type == tracker_pb2.FieldTypes.INT_TYPE:
    if (field_def.min_value is not None and
        field_val.int_value < field_def.min_value):
      return 'Value must be >= %d' % field_def.min_value
    if (field_def.max_value is not None and
        field_val.int_value > field_def.max_value):
      return 'Value must be <= %d' % field_def.max_value

  elif field_def.field_type == tracker_pb2.FieldTypes.STR_TYPE:
    if field_def.regex and field_val.str_value:
      try:
        regex = re.compile(field_def.regex)
        if not regex.match(field_val.str_value):
          return 'Value must match regular expression: %s' % field_def.regex
      except re.error:
        logging.info('Failed to process regex %r with value %r. Allowing.',
                     field_def.regex, field_val.str_value)
        return None

  elif field_def.field_type == tracker_pb2.FieldTypes.USER_TYPE:
    field_val_user = services.user.GetUser(mr.cnxn, field_val.user_id)
    auth = authdata.AuthData.FromUser(mr.cnxn, field_val_user, services)
    if auth.user_pb.user_id == INVALID_USER_ID:
      return 'User not found'
    if field_def.needs_member:
      user_value_in_project = framework_bizobj.UserIsInProject(
          project, auth.effective_ids)
      if not user_value_in_project:
        return 'User must be a member of the project'
      if field_def.needs_perm:
        user_perms = permissions.GetPermissions(
            auth.user_pb, auth.effective_ids, project)
        has_perm = user_perms.CanUsePerm(
            field_def.needs_perm, auth.effective_ids, project, [])
        if not has_perm:
          return 'User must have permission "%s"' % field_def.needs_perm
    return None

  elif field_def.field_type == tracker_pb2.FieldTypes.DATE_TYPE:
    # TODO(jrobbins): date validation
    pass

  elif field_def.field_type == tracker_pb2.FieldTypes.URL_TYPE:
    if field_val.url_value:
      if not (validate.IsValidURL(field_val.url_value)
              or autolink_constants.IS_A_SHORT_LINK_RE.match(
                  field_val.url_value)
              or autolink_constants.IS_A_NUMERIC_SHORT_LINK_RE.match(
                  field_val.url_value)
              or autolink_constants.IS_IMPLIED_LINK_RE.match(
                  field_val.url_value)):
        return 'Value must be a valid url'

  return None
コード例 #2
0
def Linkify(_mr, autolink_regex_match, _component_ref_artifacts):
    """Examine a textual reference and replace it with a hyperlink or not.

  This is a callback for use with the autolink feature.  The function
  parameters are standard for this type of callback.

  Args:
    _mr: unused information parsed from the HTTP request.
    autolink_regex_match: regex match for the textual reference.
    _component_ref_artifacts: unused result of call to GetReferencedIssues.

  Returns:
    A list of TextRuns with tag=a for all matched ftp, http, https and mailto
    links converted into HTML hyperlinks.
  """
    hyperlink = autolink_regex_match.group(0)

    trailing = ''
    for begin, end in _LINK_TRAILING_CHARS:
        if hyperlink.endswith(end):
            if not begin or hyperlink[:-len(end)].find(begin) == -1:
                trailing = end + trailing
                hyperlink = hyperlink[:-len(end)]

    tag_match = _CLOSING_TAG_RE.search(hyperlink)
    if tag_match:
        trailing = hyperlink[tag_match.start(0):] + trailing
        hyperlink = hyperlink[:tag_match.start(0)]

    href = hyperlink
    if not href.lower().startswith(('http', 'ftp', 'mailto')):
        # We use http because redirects for https are not all set up.
        href = 'http://' + href

    if (not validate.IsValidURL(href)
            and not (href.startswith('mailto')
                     and validate.IsValidEmail(href[7:]))):
        return [template_helpers.TextRun(autolink_regex_match.group(0))]

    result = [template_helpers.TextRun(hyperlink, tag='a', href=href)]
    if trailing:
        result.append(template_helpers.TextRun(trailing))

    return result
コード例 #3
0
def _Linkify(autolink_regex_match, shorthand=False):
    """Examine a textual reference and replace it with a hyperlink or not.

  This is a callback for use with the autolink feature.

  Args:
    autolink_regex_match: regex match for the textual reference.
    shorthand: Set to True to allow shorthand links without "http".

  Returns:
    A list of TextRuns with tag=a for all matched ftp, http, https and mailto
    links converted into HTML hyperlinks.
  """
    hyperlink = autolink_regex_match.group(0)

    trailing = ''
    for begin, end in _LINK_TRAILING_CHARS:
        if hyperlink.endswith(end):
            if not begin or hyperlink[:-len(end)].find(begin) == -1:
                trailing = end + trailing
                hyperlink = hyperlink[:-len(end)]

    tag_match = _CLOSING_TAG_RE.search(hyperlink)
    if tag_match:
        trailing = hyperlink[tag_match.start(0):] + trailing
        hyperlink = hyperlink[:tag_match.start(0)]

    href = hyperlink
    if shorthand and not href.lower().startswith('http'):
        # We use http because redirects for https are not all set up.
        href = 'http://' + href

    if (not validate.IsValidURL(href) and not validate.IsValidEmail(href)):
        return [template_helpers.TextRun(autolink_regex_match.group(0))]

    result = [template_helpers.TextRun(hyperlink, tag='a', href=href)]
    if trailing:
        result.append(template_helpers.TextRun(trailing))

    return result
コード例 #4
0
    def testURL(self):
        for url in self.GOOD_URLS:
            self.assertTrue(validate.IsValidURL(url), msg='Rejected:%r' % url)

        for url in self.BAD_URLS:
            self.assertFalse(validate.IsValidURL(url), msg='Accepted:%r' % url)