Skip to content

role_assignments

Role assignments model.

Corresponds to "List Role Assignments in a Dataset" endpoint: https://borealisdata.ca/guides/en/latest/api/native-api.html#list-role-assignments-in-a-dataset

RoleAssignment

Bases: BaseModel

A single role assignment.

Source code in src/dv_schema_models/role_assignments.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class RoleAssignment(BaseModel):
    """A single role assignment."""

    model_config = ConfigDict(extra="allow", populate_by_name=True)

    id: int | None = None
    assignee: str | None = None
    roleId: int | None = None
    roleName: str | None = None
    roleAlias: str | None = Field(default=None, alias="_roleAlias")
    definitionPointId: int | None = None

    def get_raw(self, key: str) -> object | None:
        """Get a raw top-level field not covered by the schema (requires extra='allow')."""
        return self.model_extra.get(key) if self.model_extra else None

get_raw(key)

Get a raw top-level field not covered by the schema (requires extra='allow').

Source code in src/dv_schema_models/role_assignments.py
23
24
25
def get_raw(self, key: str) -> object | None:
    """Get a raw top-level field not covered by the schema (requires extra='allow')."""
    return self.model_extra.get(key) if self.model_extra else None

RoleAssignments

Bases: BaseModel

Role assignments model.

Source code in src/dv_schema_models/role_assignments.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class RoleAssignments(BaseModel):
    """Role assignments model."""

    model_config = ConfigDict(extra="allow")

    status: str | None = None
    data: list[RoleAssignment] | None = None

    def count_field(self, field_name: str, value: str | int | None = None) -> int:
        """Count the number of occurrences of a field with a specific value. If value is None, counts all occurrences of the field regardless of value.

        Parameters
        ----------
        field_name
            The name of the field to count.
        value
            The value to look for and count. If None, counts all occurrences of the field regardless of value.

        Returns
        -------
        int
            The number of occurrences of the field with the specified value.

        """  # noqa: W505
        if not self.data:
            return 0

        if value is None:
            return sum(1 for ra in self.data if hasattr(ra, field_name))

        return sum(
            1 for ra in self.data if hasattr(ra, field_name) and getattr(ra, field_name) == value
        )

    def get_value(self, field_name: str) -> list[str | int | None]:
        """Get a list of all values for the given field name. Returns None for role assignments that don't have the field.

        Parameters
        ----------
        field_name
            The name of the field to retrieve values for.

        Returns
        -------
        list[str | int | None]
            A list of values for the specified field, or None for role assignments that don't have the field.

        """  # noqa: W505
        if not self.data:
            return []
        return [getattr(ra, field_name) if hasattr(ra, field_name) else None for ra in self.data]

count_field(field_name, value=None)

Count the number of occurrences of a field with a specific value. If value is None, counts all occurrences of the field regardless of value.

Parameters:

Name Type Description Default
field_name str

The name of the field to count.

required
value str | int | None

The value to look for and count. If None, counts all occurrences of the field regardless of value.

None

Returns:

Type Description
int

The number of occurrences of the field with the specified value.

Source code in src/dv_schema_models/role_assignments.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def count_field(self, field_name: str, value: str | int | None = None) -> int:
    """Count the number of occurrences of a field with a specific value. If value is None, counts all occurrences of the field regardless of value.

    Parameters
    ----------
    field_name
        The name of the field to count.
    value
        The value to look for and count. If None, counts all occurrences of the field regardless of value.

    Returns
    -------
    int
        The number of occurrences of the field with the specified value.

    """  # noqa: W505
    if not self.data:
        return 0

    if value is None:
        return sum(1 for ra in self.data if hasattr(ra, field_name))

    return sum(
        1 for ra in self.data if hasattr(ra, field_name) and getattr(ra, field_name) == value
    )

get_value(field_name)

Get a list of all values for the given field name. Returns None for role assignments that don't have the field.

Parameters:

Name Type Description Default
field_name str

The name of the field to retrieve values for.

required

Returns:

Type Description
list[str | int | None]

A list of values for the specified field, or None for role assignments that don't have the field.

Source code in src/dv_schema_models/role_assignments.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_value(self, field_name: str) -> list[str | int | None]:
    """Get a list of all values for the given field name. Returns None for role assignments that don't have the field.

    Parameters
    ----------
    field_name
        The name of the field to retrieve values for.

    Returns
    -------
    list[str | int | None]
        A list of values for the specified field, or None for role assignments that don't have the field.

    """  # noqa: W505
    if not self.data:
        return []
    return [getattr(ra, field_name) if hasattr(ra, field_name) else None for ra in self.data]

load_role_assignments(metadata)

Parse a role assignments JSON payload (already loaded as a dict) into a RoleAssignments.

Accepts the full {status, data: [...]} export envelope (including error responses with no data), or a bare single role assignment dict (no envelope).

Source code in src/dv_schema_models/role_assignments.py
81
82
83
84
85
86
87
88
89
def load_role_assignments(metadata: dict) -> RoleAssignments:
    """Parse a role assignments JSON payload (already loaded as a dict) into a RoleAssignments.

    Accepts the full `{status, data: [...]}` export envelope (including error
    responses with no `data`), or a bare single role assignment dict (no envelope).
    """
    if "data" in metadata or "status" in metadata:
        return RoleAssignments.model_validate(metadata)
    return RoleAssignments(status="OK", data=[RoleAssignment.model_validate(metadata)])