def get_custom_attribute_definitions(cls):
   """Get all applicable CA definitions (even ones without a value yet)."""
   from ggrc.models.custom_attribute_definition import \
       CustomAttributeDefinition as cad
   if cls.__name__ == "Assessment":
     return cad.query.filter(or_(
         cad.definition_type == utils.underscore_from_camelcase(cls.__name__),
         cad.definition_type == "assessment_template",
     )).all()
   else:
     return cad.query.filter(
         cad.definition_type == utils.underscore_from_camelcase(cls.__name__)
     ).all()
Beispiel #2
0
  def get_custom_attribute_definitions(cls, field_names=None):
    """Get all applicable CA definitions (even ones without a value yet)."""
    from ggrc.models.custom_attribute_definition import \
        CustomAttributeDefinition as cad

    if cls.__name__ == "Assessment":
      query = cad.query.filter(or_(
          cad.definition_type == utils.underscore_from_camelcase(cls.__name__),
          cad.definition_type == "assessment_template",
      ))
    else:
      query = cad.query.filter(
          cad.definition_type == utils.underscore_from_camelcase(cls.__name__)
      )
    if field_names:
      query = query.filter(
          sqlalchemy.or_(cad.title.in_(field_names), cad.mandatory)
      )
    return query.options(
        orm.undefer_group('CustomAttributeDefinition_complete')
    )
Beispiel #3
0
  def get_custom_attr_definitions(cls, object_class, ca_cache=None,
                                  include_oca=True):
    """Get column definitions for custom attributes on object_class.

    Args:
      object_class: Model for which we want the attribute definitions.
      ca_cache: dictionary containing custom attribute definitions. If it's set
        this function will not look for CAD in the database. This should be
        used for bulk operations, and eventually replaced with memcache.
      include_oca: Flag for including object level custom attributes. This
        should be true only for defenitions needed for csv imports.

    returns:
      dict of custom attribute definitions.
    """
    definitions = {}
    if not hasattr(object_class, "get_custom_attribute_definitions"):
      return definitions
    object_name = underscore_from_camelcase(object_class.__name__)
    if isinstance(ca_cache, dict) and object_name:
      custom_attributes = ca_cache.get(object_name, [])
    else:
      custom_attributes = object_class.get_custom_attribute_definitions()
    for attr in custom_attributes:
      description = attr.helptext or u""
      if (attr.attribute_type == attr.ValidTypes.DROPDOWN and
              attr.multi_choice_options):
        if description:
          description += "\n\n"
        description += u"Accepted values are:\n{}".format(
            attr.multi_choice_options.replace(",", "\n")
        )
      if attr.definition_id:
        ca_type = cls.Type.OBJECT_CUSTOM
        attr_name = u"{}{}".format(
            cls.OBJECT_CUSTOM_ATTR_PREFIX, attr.title).lower()
      else:
        ca_type = cls.Type.CUSTOM
        attr_name = u"{}{}".format(cls.CUSTOM_ATTR_PREFIX, attr.title).lower()

      definition_ids = definitions.get(attr_name, {}).get("definition_ids", [])
      definition_ids.append(attr.id)

      definitions[attr_name] = {
          "display_name": attr.title,
          "attr_name": attr.title,
          "mandatory": attr.mandatory,
          "unique": False,
          "description": description,
          "type": ca_type,
          "definition_ids": definition_ids,
      }
    return definitions
Beispiel #4
0
def init_globals(models):
  """Load all ACRs and CADs into memory from db"""
  with app.app_context():
    with factories.single_commit():
      for model in models:
        AC_ROLES[model] = {
            name: id for id, name in
            get_custom_roles_for(model).items()
        }

        CADS[model] = {
            CAD_PERSON_TYPE: factories.CustomAttributeDefinitionFactory(
                title=CAD_PERSON_TITLE,
                definition_type=utils.underscore_from_camelcase(model),
                attribute_type=CAD_PERSON_TYPE,
            ).id
        }
Beispiel #5
0
 def table_singular(self):
   return utils.underscore_from_camelcase(self.model_singular)
Beispiel #6
0
 def table_singular(self):
     return utils.underscore_from_camelcase(self.model_singular)
Beispiel #7
0
    def get_custom_attr_definitions(cls,
                                    object_class,
                                    ca_cache=None,
                                    fields=None):
        """Get column definitions for custom attributes on object_class.

    Args:
      object_class (db.Model): Model whose attribute definitions to get.
      ca_cache (dict): Dictionary containing custom attribute definitions. If
        it's set this function will not look for CAD in the database. This
        should be used for bulk operations, and eventually replaced with
        memcache.
      fields (iterable): Iterable containing names of custom attribute
        definitions to get. If None, all definitions for passed object class
        will be returned. Defaults to None.

    Returns:
      A dict of custom attribute definitions.
    """
        from ggrc.models import mixins

        definitions = {}
        if not issubclass(
                object_class,
            (mixins.CustomAttributable, mixins.ExternalCustomAttributable)):
            return definitions

        object_name = underscore_from_camelcase(object_class.__name__)
        if isinstance(ca_cache, dict) and object_name:
            custom_attributes = ca_cache.get(object_name, [])
        else:
            custom_attributes = object_class.get_custom_attribute_definitions(
                fields)
        for attr in custom_attributes:
            description = attr.helptext or u""
            if attr.multi_choice_options:
                if description:
                    description += "\n\n"
                description += u"Allowed values are:\n{}".format(
                    attr.multi_choice_options.replace(",", "\n"))
            elif attr.attribute_type == attr.ValidTypes.CHECKBOX:
                description += u"Allowed values are:\nyes\nno"
            elif attr.ValidTypes.MAP in attr.attribute_type:
                description += u"Allowed values are emails"
            if attr.definition_id:
                ca_type = cls.Type.OBJECT_CUSTOM
                attr_name = u"{}{}".format(cls.OBJECT_CUSTOM_ATTR_PREFIX,
                                           attr.title).lower()
            else:
                ca_type = cls.Type.CUSTOM
                attr_name = u"{}{}".format(cls.CUSTOM_ATTR_PREFIX,
                                           attr.title).lower()

            definition_ids = definitions.get(attr_name,
                                             {}).get("definition_ids", [])
            definition_ids.append(attr.id)
            if fields is None or attr.title.lower() in fields:
                definitions[attr_name] = {
                    "display_name": attr.title,
                    "attr_name": attr.title,
                    "mandatory": attr.mandatory,
                    "unique": False,
                    "description": description,
                    "type": ca_type,
                    "definition_ids": definition_ids,
                }
        return definitions
Beispiel #8
0
 def join_expr():
     return sa.and_(
         orm.foreign(orm.remote(cad.definition_id)) == cls.id,
         cad.definition_type == utils.underscore_from_camelcase(
             current_type),
     )
Beispiel #9
0
 def attribute_definitions_for_type(ttype):
     return db.session.query(CustomAttributeDefinition)\
       .filter(CustomAttributeDefinition.definition_type ==
               underscore_from_camelcase(ttype))\
       .order_by(CustomAttributeDefinition.title)\
       .all()
 def attribute_definitions_for_type(ttype):
   return db.session.query(CustomAttributeDefinition)\
     .filter(CustomAttributeDefinition.definition_type ==
             underscore_from_camelcase(ttype))\
     .order_by(CustomAttributeDefinition.title)\
     .all()
Beispiel #11
0
 def _ca_cache(self):
     """Get custom attributes definitions in ca_cache-compatible format."""
     object_name = utils.underscore_from_camelcase(
         self.object_class.__name__)
     return {object_name: self.ca_definitions_cache.values()}