Example #1
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)
    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
Example #2
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
Example #3
0
def _get_assignable_dict(people, notif, ca_cache=None):
    """Get dict data for assignable object in notification.

  Args:
    people (List[Person]): List o people objects who should receive the
      notification.
    notif (Notification): Notification that should be sent.
    ca_cache (dict): prefetched CustomAttributeDefinition instances accessible
      by definition_type as a key
  Returns:
    dict: dictionary containing notification data for all people in the given
      list.
  """
    obj = get_notification_object(notif)
    data = {}

    # we do not use definitions data if notification name not assessment update
    if notif.notification_type.name == "assessment_updated":
        definitions = AttributeInfo.get_object_attr_definitions(
            obj.__class__, ca_cache=ca_cache)
    else:
        definitions = None
    roles = _get_assignable_roles(obj)

    for person in people:
        # We should default to today() if no start date is found on the object.
        start_date = getattr(obj, "start_date", datetime.date.today())
        data[person.email] = {
            "user": get_person_dict(person),
            notif.notification_type.name: {
                obj.id: {
                    "title":
                    obj.title,
                    "start_date_statement":
                    utils.get_digest_date_statement(start_date, "start", True),
                    "url":
                    get_object_url(obj),
                    "notif_created_at": {
                        notif.id: as_user_time(notif.created_at)
                    },
                    "notif_updated_at": {
                        notif.id: as_user_time(notif.updated_at)
                    },
                    "updated_data":
                    _get_updated_fields(obj, notif.created_at, definitions,
                                        roles) if notif.notification_type.name
                    == "assessment_updated" else None,
                }
            }
        }
    return data
Example #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
Example #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
Example #6
0
def _get_assignable_dict(people, notif):
  """Get dict data for assignable object in notification.

  Args:
    people (List[Person]): List o people objects who should receive the
      notification.
    notif (Notification): Notification that should be sent.
  Returns:
    dict: dictionary containing notification data for all people in the given
      list.
  """
  obj = get_notification_object(notif)
  data = {}

  definitions = AttributeInfo.get_object_attr_definitions(obj.__class__)
  roles = _get_assignable_roles(obj)

  for person in people:
    # We should default to today() if no start date is found on the object.
    start_date = getattr(obj, "start_date", datetime.date.today())
    data[person.email] = {
        "user": get_person_dict(person),
        notif.notification_type.name: {
            obj.id: {
                "title": obj.title,
                "start_date_statement": utils.get_digest_date_statement(
                    start_date, "start", True),
                "url": get_object_url(obj),
                "notif_created_at": {
                    notif.id: as_user_time(notif.created_at)},
                "notif_updated_at": {
                    notif.id: as_user_time(notif.updated_at)},
                "updated_fields": _get_updated_fields(obj,
                                                      notif.created_at,
                                                      definitions,
                                                      roles)
                if notif.notification_type.name == "assessment_updated"
                else None,
            }
        }
    }
  return data