Category combo builder¶
Declarative one-call helper that takes a typed CategoryComboBuildSpec (categories + their options) and materialises the entire Categories → CategoryCombo → CategoryOptionCombo tree against DHIS2. Wraps client.categories, client.category_options, client.category_combos, and (on v43) the COC-regen wait helper into a single idempotent call.
from dhis2w_client import CategoryComboBuildSpec, CategorySpec, CategoryOptionSpec, build_category_combo
spec = CategoryComboBuildSpec(
name="Sex x Age",
categories=[
CategorySpec(name="Sex", short_name="Sex", options=[
CategoryOptionSpec(name="Male", short_name="M"),
CategoryOptionSpec(name="Female", short_name="F"),
]),
CategorySpec(name="Age band", short_name="Age", options=[
CategoryOptionSpec(name="<1y", short_name="<1y"),
CategoryOptionSpec(name=">=1y", short_name=">=1y"),
]),
],
)
result: CategoryComboBuildResult = await build_category_combo(client, spec)
print(result.category_combo_uid, result.category_option_combo_uids)
Idempotent on name — if a category / option with the requested name already exists it's reused rather than duplicated. On v43 the helper waits for the COC matrix to materialise before returning (so callers can immediately start writing data values against it).
Worked example: examples/v43/client/category_combo_build.py.
category_combo_builder
¶
One-pass builder for the full Category dimension stack.
build_category_combo(client, spec) walks a declarative
CategoryComboBuildSpec and ensures the requested
CategoryOption -> Category -> CategoryCombo chain exists on the
target instance, creating only what's missing. Idempotent — re-running
the same spec is a no-op (modulo new options getting wired into
existing categories).
Lookup is by name (DHIS2 enforces unique names on each of the three
resource types). For each spec entry:
- Each
CategoryOptionSpecresolves to an existing CategoryOption (by name) or a fresh create. - Each
CategorySpecresolves to an existing Category (by name) — if present, missing option UIDs are appended viaadd_option. If absent, the Category is created with all option UIDs wired in one POST. - The top-level
CategoryComboBuildSpec.nameresolves to an existing CategoryCombo (by name) — if present, missing category UIDs are appended viaadd_category. If absent, the combo is created with all category UIDs in order. - The helper polls the COC matrix until the expected count (the cross-product of option counts) lands.
Returns a typed CategoryComboBuildResult carrying every UID and a
created-vs-reused breakdown so callers can render a "what changed"
summary.
Classes¶
CategoryOptionSpec
¶
Bases: BaseModel
One CategoryOption inside a CategorySpec.
Resolved by name against the target instance. short_name defaults
to name when omitted; DHIS2 caps shortName at 50 characters so
longer names are truncated.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/category_combo_builder.py
CategorySpec
¶
Bases: BaseModel
One Category axis of a CategoryComboBuildSpec.
options order is preserved on creation and shapes the COC matrix
layout. Existing categories are amended with any missing options
appended in spec order.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/category_combo_builder.py
CategoryComboBuildSpec
¶
Bases: BaseModel
Declarative spec for build_category_combo.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/category_combo_builder.py
CategoryBuildEntry
¶
Bases: BaseModel
Per-category outcome of a build run.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/category_combo_builder.py
CategoryComboBuildResult
¶
Bases: BaseModel
Outcome of build_category_combo — every UID + a created-vs-reused breakdown.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/category_combo_builder.py
Functions¶
build_category_combo(client, spec, *, timeout_seconds=120.0, poll_interval_seconds=1.0)
async
¶
Ensure spec exists end-to-end on the target instance, creating only what's missing.
See module docstring for the resolution algorithm. The default
timeout_seconds=120 doubles the per-combo default on
wait_for_coc_generation because rebuild on a fresh instance can
take longer than steady-state regen.