def _ca_values_changed(obj):
  """Check if object's custom attribute values have been changed.

  The changes are determined by comparing the current object custom attributes
  with the CA values from object's last known revision. If the latter does not
  exist, it is considered that there are no changes - since the object has
  apparently just been created.

  Args:
    obj (models.mixins.Assignable): the object to check

  Returns:
    (bool) True if there is a change to any of the CA values, False otherwise.
  """
  revision = db.session.query(
      models.Revision
  ).filter_by(
      resource_id=obj.id,
      resource_type=obj.type
  ).order_by(models.Revision.id.desc()).first()
  if not revision:
    raise InternalServerError(errors.MISSING_REVISION)
  new_attrs = {
      "custom_attribute_values":
      [value.log_json() for value in obj.custom_attribute_values],
      "custom_attribute_definitions":
      [defn.log_json() for defn in obj.custom_attribute_definitions]
  }
  return any(notifications.get_updated_cavs(new_attrs, revision.content))
Beispiel #2
0
def _get_updated_fields(obj, created_at, definitions, roles):
  """Get dict of updated  attributes of assessment"""
  fields = []

  new_rev, old_rev = _get_revisions(obj, created_at)
  if not old_rev:
    return []

  new_attrs = new_rev.content
  old_attrs = old_rev.content
  for attr_name, new_val in new_attrs.iteritems():
    if attr_name in notifications.IGNORE_ATTRS:
      continue
    old_val = old_attrs.get(attr_name, None)
    if old_val != new_val:
      if not old_val and not new_val:
        continue
      if attr_name == u"recipients" and old_val and new_val and \
         sorted(old_val.split(",")) == sorted(new_val.split(",")):
        continue
      if attr_name == "access_control_list":
        fields.extend(_get_updated_roles(new_val, old_val, roles))
        continue
      fields.append(attr_name)

  fields.extend(list(notifications.get_updated_cavs(new_attrs, old_attrs)))
  updated_fields = []
  for field in fields:
    definition = definitions.get(field, None)
    if definition:
      updated_fields.append(definition["display_name"].upper())
    else:
      updated_fields.append(field.upper())
  return updated_fields
def _ca_values_changed(obj):
    """Check if object's custom attribute values have been changed.

  The changes are determined by comparing the current object custom attributes
  with the CA values from object's last known revision. If the latter does not
  exist, it is considered that there are no changes - since the object has
  apparently just been created.

  Args:
    obj (models.mixins.Assignable): the object to check

  Returns:
    (bool) True if there is a change to any of the CA values, False otherwise.
  """
    revision = db.session.query(models.Revision).filter_by(
        resource_id=obj.id,
        resource_type=obj.type).order_by(models.Revision.id.desc()).first()
    if not revision:
        raise InternalServerError(errors.MISSING_REVISION)
    new_attrs = {
        "custom_attribute_values":
        [value.log_json() for value in obj.custom_attribute_values],
        "custom_attribute_definitions":
        [defn.log_json() for defn in obj.custom_attribute_definitions]
    }
    return any(notifications.get_updated_cavs(new_attrs, revision.content))
Beispiel #4
0
def _get_updated_fields(obj, created_at, definitions, roles):
    """Get dict of updated  attributes of assessment"""
    fields = []

    new_rev, old_rev = _get_revisions(obj, created_at)
    if not old_rev:
        return []

    new_attrs = new_rev.content
    old_attrs = old_rev.content
    for attr_name, new_val in new_attrs.iteritems():
        if attr_name in notifications.IGNORE_ATTRS:
            continue
        old_val = old_attrs.get(attr_name, None)
        if old_val != new_val:
            if not old_val and not new_val:
                continue
            if attr_name == u"recipients" and old_val and new_val and \
               sorted(old_val.split(",")) == sorted(new_val.split(",")):
                continue
            if attr_name == "access_control_list":
                fields.extend(_get_updated_roles(new_val, old_val, roles))
                continue
            fields.append(attr_name)

    fields.extend(list(notifications.get_updated_cavs(new_attrs, old_attrs)))
    updated_fields = []
    for field in fields:
        definition = definitions.get(field, None)
        if definition:
            updated_fields.append(definition["display_name"].upper())
        else:
            updated_fields.append(field.upper())
    return updated_fields
Beispiel #5
0
def _get_updated_fields(obj, created_at, definitions, roles):  # noqa: C901
    """Get dict of updated  attributes of assessment"""
    # pylint: disable=too-many-locals
    fields = []

    new_rev, old_rev = _get_revisions(obj, created_at)
    if not old_rev:
        return []

    new_attrs = new_rev.content
    old_attrs = old_rev.content

    updated_roles = []
    updated_display_names = []
    for attr_name, new_val in new_attrs.iteritems():
        if attr_name in notifications.IGNORE_ATTRS:
            continue
        old_val = old_attrs.get(attr_name, None)
        if old_val != new_val:
            if not old_val and not new_val:
                continue
            if attr_name == u"recipients" and old_val and new_val and \
               sorted(old_val.split(",")) == sorted(new_val.split(",")):
                continue
            if attr_name == "access_control_list":
                updated_roles = _get_updated_roles(new_val, old_val, roles)
                continue
            if attr_name in ("evidences_url", "evidences_file", "labels"):
                updated_display_names.append(
                    _get_updated_display_names(
                        attr_name,
                        new_val,
                        old_val,
                    ))
                continue
            fields.append(attr_name)
    updated_data = {}
    for attr_name, new_val, old_val in updated_roles:
        updated_data.update(
            _get_displayed_updated_data(attr_name, new_val, old_val,
                                        definitions))
    for attr_name, new_val, old_val in updated_display_names:
        updated_data.update(
            _get_displayed_updated_data(attr_name, new_val, old_val,
                                        definitions))
    updated_cavs = notifications.get_updated_cavs(new_attrs, old_attrs)
    for attr_name, new_val, old_val in updated_cavs:
        updated_data.update(
            _get_displayed_updated_data(attr_name, new_val, old_val,
                                        definitions))
    for field in fields:
        new_val, old_val = new_attrs.get(field), old_attrs.get(field)
        updated_data.update(
            _get_displayed_updated_data(field, new_val, old_val, definitions))
    return updated_data