示例#1
0
def _get_inner_resources_relation(
        parent_resource_el: Element) -> RelationEntityDto:
    attrs = parent_resource_el.attrib
    return RelationEntityDto(
        INNER_RESOURCE_ID_TEMPLATE.format(attrs["id"]),
        ResourceRelationType.INNER_RESOURCES,
        [
            res.attrib["id"]
            for res in common.get_inner_resources(parent_resource_el)
        ],
        dict(attrs),
    )
示例#2
0
文件: relations.py 项目: kmalyjur/pcs
def _get_inner_resources_relation(
    parent_resource_el: _Element,
) -> RelationEntityDto:
    attrs = cast(Mapping[str, str], parent_resource_el.attrib)
    return RelationEntityDto(
        INNER_RESOURCE_ID_TEMPLATE.format(attrs["id"]),
        ResourceRelationType.INNER_RESOURCES,
        [
            str(res.attrib["id"])
            for res in common.get_inner_resources(parent_resource_el)
        ],
        dict(attrs),
    )
示例#3
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