Пример #1
0
  def test_get_default(self):
    """test get default model handler"""

    test_handler = collections.namedtuple("TestHandler", [])
    test_class = collections.namedtuple("TestClass", [])
    test_custom_handler = collections.namedtuple("TestCustomHandler", [])
    test_custom_class = collections.namedtuple("TestCustomClass", [])

    tested_handlers_dict = {
        "default": {
            "col_a": test_handler,
            "col_b": test_handler,
        },
        test_custom_class.__name__: {
            "col_a": test_custom_handler,
        }
    }

    with mock.patch("ggrc.converters.column_handlers.COLUMN_HANDLERS",
                    tested_handlers_dict):
      self.assertEqual({"col_a": test_handler, "col_b": test_handler},
                       model_column_handlers(test_class))
      self.assertEqual(
          {"col_a": test_custom_handler, "col_b": test_handler},
          model_column_handlers(test_custom_class))
Пример #2
0
    def test_get_default(self):
        """test get default model handler"""

        test_handler = collections.namedtuple("TestHandler", [])
        test_class = collections.namedtuple("TestClass", [])
        test_custom_handler = collections.namedtuple("TestCustomHandler", [])
        test_custom_class = collections.namedtuple("TestCustomClass", [])

        tested_handlers_dict = {
            "default": {
                "col_a": test_handler,
                "col_b": test_handler,
            },
            test_custom_class.__name__: {
                "col_a": test_custom_handler,
            }
        }

        with mock.patch("ggrc.converters.column_handlers.COLUMN_HANDLERS",
                        tested_handlers_dict):
            self.assertEqual({
                "col_a": test_handler,
                "col_b": test_handler
            }, model_column_handlers(test_class))
            self.assertEqual(
                {
                    "col_a": test_custom_handler,
                    "col_b": test_handler
                }, model_column_handlers(test_custom_class))
Пример #3
0
def get_object_column_definitions(object_class,
                                  fields=None,
                                  ca_cache=None,
                                  include_hidden=False,
                                  for_template=False):
    """Attach additional info to attribute definitions.

  Fetches the attribute info (_aliases) for the given object class and adds
  additional data (handler class, validator function, default value) needed
  for imports. For better performance consider to provide `ca_cache` argument.

  Args:
    object_class (db.Model): Model whose column definitions to get for import.
    fields (iterable): Iterable of field names of the given object class to
      include in returned attribute definitions list. If None, all fields will
      be included. Defaults to None.
    ca_cache (dict): Dictionary containing custom attribute definitions grouped
      by their definitions_type. If None, definitions will be queried from the
      DB. Defaults to None.
    include_hidden (bool): Flag which specifies if we should include column
      handlers for hidden attributes (they marked as 'hidden'
      in _aliases dict).
    for_template (bool): Flag which specifies if we should exclude column
      handlers for attributes that marked as 'ship_in_template'
      in _aliases dict.

  Returns:
    A dict of attribute definitions.
  """
    attributes = AttributeInfo.get_object_attr_definitions(
        object_class,
        ca_fields=fields,
        include_hidden=include_hidden,
        for_template=for_template,
        ca_cache=ca_cache,
    )

    column_handlers = model_column_handlers(object_class)
    for key, attr in attributes.iteritems():
        handler_key = attr.get("handler_key", key)
        # check full handler keys
        handler = column_handlers.get(handler_key)
        if not handler:
            # check handler key prefixes
            handler = column_handlers.get(handler_key.split(":")[0])
        if not handler:
            # use default handler
            handler = handlers.ColumnHandler
        validator = None
        default = None
        if attr["type"] == AttributeInfo.Type.PROPERTY:
            validator = getattr(object_class, "validate_{}".format(key), None)
            default = getattr(object_class, "default_{}".format(key), None)

        attr["handler"] = attr.get("handler", handler)
        attr["validator"] = attr.get("validator", validator)
        attr["default"] = attr.get("default", default)
    return attributes
Пример #4
0
def get_object_column_definitions(object_class, fields=None,
                                  include_hidden=False):
  """Attach additional info to attribute definitions.

  Fetches the attribute info (_aliases) for the given object class and adds
  additional data (handler class, validator function, default value) )needed
  for imports.

  Args:
    object_class (db.Model): Model for which we want to get column definitions
      for imports.
    include_hidden (bool): Flag which specifies if we should include column
      handlers for hidden attributes (they marked as 'hidden'
      in _aliases dict).

  Returns:
    dict: Updated attribute definitions dict with additional data.
  """
  attributes = AttributeInfo.get_object_attr_definitions(
      object_class,
      fields=fields,
      include_hidden=include_hidden
  )
  column_handlers = model_column_handlers(object_class)
  for key, attr in attributes.iteritems():
    handler_key = attr.get("handler_key", key)
    # check full handler keys
    handler = column_handlers.get(handler_key)
    if not handler:
      # check handler key prefixes
      handler = column_handlers.get(handler_key.split(":")[0])
    if not handler:
      # use default handler
      handler = handlers.ColumnHandler
    validator = None
    default = None
    if attr["type"] == AttributeInfo.Type.PROPERTY:
      validator = getattr(object_class, "validate_{}".format(key), None)
      default = getattr(object_class, "default_{}".format(key), None)

    attr["handler"] = attr.get("handler", handler)
    attr["validator"] = attr.get("validator", validator)
    attr["default"] = attr.get("default", default)
  return attributes
Пример #5
0
def get_object_column_definitions(object_class,
                                  fields=None,
                                  include_hidden=False):
    """Attach additional info to attribute definitions.

  Fetches the attribute info (_aliases) for the given object class and adds
  additional data (handler class, validator function, default value) )needed
  for imports.

  Args:
    object_class (db.Model): Model for which we want to get column definitions
      for imports.
    include_hidden (bool): Flag which specifies if we should include column
      handlers for hidden attributes (they marked as 'hidden'
      in _aliases dict).

  Returns:
    dict: Updated attribute definitions dict with additional data.
  """
    attributes = AttributeInfo.get_object_attr_definitions(
        object_class, fields=fields, include_hidden=include_hidden)
    column_handlers = model_column_handlers(object_class)
    for key, attr in attributes.iteritems():
        handler_key = attr.get("handler_key", key)
        # check full handler keys
        handler = column_handlers.get(handler_key)
        if not handler:
            # check handler key prefixes
            handler = column_handlers.get(handler_key.split(":")[0])
        if not handler:
            # use default handler
            handler = handlers.ColumnHandler
        validator = None
        default = None
        if attr["type"] == AttributeInfo.Type.PROPERTY:
            validator = getattr(object_class, "validate_{}".format(key), None)
            default = getattr(object_class, "default_{}".format(key), None)

        attr["handler"] = attr.get("handler", handler)
        attr["validator"] = attr.get("validator", validator)
        attr["default"] = attr.get("default", default)
    return attributes
Пример #6
0
def get_object_column_definitions(object_class):
    """Attach additional info to attribute definitions.

  Fetches the attribute info (_aliases) for the given object class and adds
  additional data (handler class, validator function, default value) )needed
  for imports.

  Args:
    object_class (db.Model): Model for which we want to get column definitions
      for imports.

  Returns:
    dict: Updated attribute definitions dict with additional data.
  """
    attributes = AttributeInfo.get_object_attr_definitions(object_class,
                                                           include_oca=True)
    column_handlers = model_column_handlers(object_class)
    for key, attr in attributes.iteritems():
        handler_key = attr.get("handler_key", key)
        handler = column_handlers.get(handler_key, handlers.ColumnHandler)
        validator = None
        default = None
        if attr["type"] == AttributeInfo.Type.PROPERTY:
            validator = getattr(object_class, "validate_{}".format(key), None)
            default = getattr(object_class, "default_{}".format(key), None)
        elif attr["type"] == AttributeInfo.Type.MAPPING:
            handler = column_handlers.get(key, handlers.MappingColumnHandler)
        elif attr["type"] == AttributeInfo.Type.CUSTOM:
            handler = column_handlers.get(
                key, custom_attribute.CustomAttributeColumHandler)
        elif attr["type"] == AttributeInfo.Type.OBJECT_CUSTOM:
            handler = column_handlers.get(
                key, custom_attribute.ObjectCaColumnHandler)
        attr["handler"] = attr.get("handler", handler)
        attr["validator"] = attr.get("validator", validator)
        attr["default"] = attr.get("default", default)
    return attributes