Exemple #1
0
def is_resource_in_same_group(cib, resource_id_list):
    # We don't care about not found elements here, that is a job of another
    # validator. We do not care if the id doesn't belong to a resource either
    # for the same reason.
    element_list, _ = get_elements_by_ids(cib, set(resource_id_list))

    parent_list = []
    for element in element_list:
        parent = get_parent_resource(element)
        if parent is not None and group.is_group(parent):
            parent_list.append(parent)

    if len(set(parent_list)) != len(parent_list):
        raise LibraryError(
            ReportItem.error(
                reports.messages.
                CannotSetOrderConstraintsForResourcesInTheSameGroup()))
Exemple #2
0
    def _get_resource_relations(
            self, resource_el: Element) -> Sequence[RelationEntityDto]:
        resource_id = resource_el.attrib["id"]
        relations = [
            _get_ordering_constraint_relation(item)
            for item in self._get_ordering_coinstraints(resource_id)
        ] + [
            _get_ordering_set_constraint_relation(item)
            for item in self._get_ordering_set_constraints(resource_id)
        ]

        # special type of relation, group (note that a group can be a resource
        # and a relation)
        if common.is_wrapper_resource(resource_el):
            relations.append(_get_inner_resources_relation(resource_el))

        # handle resources in a wrapper resource (group/bundle/clone relation)
        parent_el = common.get_parent_resource(resource_el)
        if parent_el is not None:
            relations.append(_get_outer_resource_relation(parent_el))
        return relations
Exemple #3
0
 def assert_parent_resource(self, input_resource_id, output_resource_id):
     res_el = common.get_parent_resource(
         fixture_cib.find('.//*[@id="{0}"]'.format(input_resource_id)))
     self.assertEqual(output_resource_id,
                      res_el.get("id") if res_el is not None else None)
Exemple #4
0
def validate_move_resources_to_group(
    group_element: _Element,
    resource_element_list: List[_Element],
    adjacent_resource_element: Optional[_Element],
) -> ReportItemList:
    """
    Validates that existing resources can be moved into a group,
    optionally beside an adjacent_resource_element

    group_element -- the group to put resources into
    resource_element_list -- resources that are being moved into the group
    adjacent_resource_element -- put resources beside this one
    """
    report_list: ReportItemList = []

    # Validate types of resources and their parents
    for resource_element in resource_element_list:
        # Only primitive resources can be moved
        if not is_resource(resource_element):
            report_list.append(
                ReportItem.error(
                    reports.messages.IdBelongsToUnexpectedType(
                        str(resource_element.attrib["id"]),
                        ["primitive"],
                        resource_element.tag,
                    )))
        elif is_wrapper_resource(resource_element):
            report_list.append(
                ReportItem.error(
                    reports.messages.CannotGroupResourceWrongType(
                        str(resource_element.attrib["id"]),
                        resource_element.tag,
                        parent_id=None,
                        parent_type=None,
                    )))
        else:
            parent = get_parent_resource(resource_element)
            if parent is not None and not group.is_group(parent):
                # At the moment, moving resources out of bundles and clones
                # (or masters) is not possible
                report_list.append(
                    ReportItem.error(
                        reports.messages.CannotGroupResourceWrongType(
                            str(resource_element.attrib["id"]),
                            resource_element.tag,
                            parent_id=str(parent.attrib["id"]),
                            parent_type=parent.tag,
                        )))

    # Validate that elements can be added
    # Check if the group element is a group
    if group.is_group(group_element):
        report_list += validate_add_remove_items(
            [str(resource.attrib["id"]) for resource in resource_element_list],
            [],
            [
                str(resource.attrib["id"])
                for resource in get_inner_resources(group_element)
            ],
            ADD_REMOVE_CONTAINER_TYPE_GROUP,
            ADD_REMOVE_ITEM_TYPE_RESOURCE,
            str(group_element.attrib["id"]),
            str(adjacent_resource_element.attrib["id"])
            if adjacent_resource_element is not None else None,
            True,
        )
    else:
        report_list.append(
            ReportItem.error(
                reports.messages.IdBelongsToUnexpectedType(
                    str(group_element.attrib["id"]),
                    expected_types=[group.TAG],
                    current_type=group_element.tag,
                )))

    # Elements can always be removed from their old groups, except when the last
    # resource is removed but that is handled in resource.group_add for now, no
    # need to run the validation for removing elements
    return report_list