def populate_cavs(self): """Setup cads in cav list if they are not presented in content but now they are associated to instance.""" from ggrc.models import custom_attribute_definition cads = custom_attribute_definition.get_custom_attributes_for( self.resource_type, self.resource_id) cavs = {int(i["custom_attribute_id"]): i for i in self._get_cavs()} for cad in cads: custom_attribute_id = int(cad["id"]) if custom_attribute_id in cavs: # Old revisions can contain falsy values for a Checkbox if cad["attribute_type"] == "Checkbox" \ and not cavs[custom_attribute_id]["attribute_value"]: cavs[custom_attribute_id]["attribute_value"] = cad["default_value"] continue if cad["attribute_type"] == "Map:Person": value = "Person" else: value = cad["default_value"] cavs[custom_attribute_id] = { "attribute_value": value, "attribute_object_id": None, "custom_attribute_id": custom_attribute_id, "attributable_id": self.resource_id, "attributable_type": self.resource_type, "display_name": "", "attribute_object": None, "type": "CustomAttributeValue", "context_id": None, } return {"custom_attribute_values": cavs.values(), "custom_attribute_definitions": cads}
def populate_cavs(self): """Setup cads in cav list if they are not presented in content but now they are associated to instance.""" from ggrc.models import custom_attribute_definition cads = custom_attribute_definition.get_custom_attributes_for( self.resource_type, self.resource_id) cavs = {int(i["custom_attribute_id"]): i for i in self._get_cavs()} for cad in cads: custom_attribute_id = int(cad["id"]) if custom_attribute_id in cavs: # Old revisions can contain falsy values for a Checkbox if cad["attribute_type"] == "Checkbox" \ and not cavs[custom_attribute_id]["attribute_value"]: cavs[custom_attribute_id]["attribute_value"] = cad["default_value"] continue if cad["attribute_type"] == "Map:Person": value = "Person" else: value = cad["default_value"] cavs[custom_attribute_id] = { "attribute_value": value, "attribute_object_id": None, "custom_attribute_id": custom_attribute_id, "attributable_id": self.resource_id, "attributable_type": self.resource_type, "display_name": "", "attribute_object": None, "type": "CustomAttributeValue", "context_id": None, } return {"custom_attribute_values": cavs.values(), "custom_attribute_definitions": cads}
def _get_cads(self): """Return cads definitions from content and new CADs from db.""" from ggrc.models import custom_attribute_definition all_cads = [] if "custom_attribute_definitions" in self._content and \ self._content["custom_attribute_definitions"]: all_cads.extend(self._content["custom_attribute_definitions"]) db_cads = custom_attribute_definition.get_custom_attributes_for( self.resource_type, self.resource_id) all_cads_ids = [cad['id'] for cad in all_cads] return all_cads + [ cad for cad in db_cads if cad['id'] not in all_cads_ids ]