rest_flex_fields2.utils

Utility helpers for rest_flex_fields2.

Provides request-inspection functions (is_expanded, is_included) and the split_levels helper that partitions dot-notation field lists into current-level and next-level fragments.

Functions

is_expanded(→ bool)

Return whether field is requested for expansion.

is_included(→ bool)

Return whether field should be included in the response.

split_levels(→ tuple[list[str], dict[str, list[str]]])

Split a dot-notation field list into current-level and next-level parts.

Module Contents

rest_flex_fields2.utils.is_expanded(request, field: str) bool[source]

Return whether field is requested for expansion.

Inspects the expand query parameter on request. Returns True when field appears in the comma-separated expand list, or when a wildcard value (e.g. * or ~all) is present.

rest_flex_fields2.utils.is_included(request, field: str) bool[source]

Return whether field should be included in the response.

Returns False when the fields sparse-fieldset parameter is present and field is not listed, or when the omit parameter is present and field is listed. Returns True otherwise.

rest_flex_fields2.utils.split_levels(fields: str | collections.abc.Iterable[str]) tuple[list[str], dict[str, list[str]]][source]

Split a dot-notation field list into current-level and next-level parts.

Given an iterable such as ['a', 'a.b', 'a.d', 'c'], returns a tuple (first_level, next_level) where first_level is the deduplicated list of top-level names (e.g. ['a', 'c']) and next_level is a dict mapping each name to its remaining path fragments (e.g. {'a': ['b', 'd']}). A plain string is treated as a comma-separated field list.