Source code for cadrumo.domain.deadlines.setup_answer_projection
"""Registry-aware projection of persisted profile values into setup answers.The typed answer model lives in the user-profile domain. This domain module owns theprojection-specific field specification and composes registry declarationsinto the projection consumed by deadline profile construction. Core thereforeremains independent of registry authority while the registry catalogue remainsthe one source of these facts."""from__future__importannotationsfromcollections.abcimportMappingfromdataclassesimportdataclassfrom..user_profile.setup_answersimportSetupAnswers
[docs]@dataclass(frozen=True,slots=True)classSetupFieldSpec:"""Describe how one setup answer is projected from persisted profile data."""path:str"""Dotted profile-record path, e.g. ``identity.tax_id``."""answer_type:type[str]|type[bool]"""The stored token's type; only string and boolean answers are supported."""default:str|None=None"""Token to assume when the profile record has no value for ``path``. ``None`` leaves the answer to the model's own default. An empty string is a present, declared blank and remains distinct from an absent path. """
[docs]defsetup_answer_fields()->dict[str,SetupFieldSpec]:"""Return every setup-answer field that a persisted profile record can fill. The declarations are resolved from the registry authority in scope on each call, so a republished generation is never answered from a stale copy. """from..calculations.registry.setup_profile_bindingsimportsetup_answer_declarationsreturn{field:SetupFieldSpec(path,answer_type,default)forfield,(path,answer_type,default)insetup_answer_declarations().items()}
[docs]defproject_setup_answers(values:Mapping[str,str])->SetupAnswers:"""Build :class:`SetupAnswers` from canonical profile-record values. A present path wins even when its value is blank; only an absent path uses the registry declaration's default. Blank boolean tokens stay undeclared rather than being collapsed to ``False``. """typed:dict[str,object]={}forfield,specinsetup_answer_fields().items():raw=values.get(spec.path)ifrawisNone:raw=spec.defaultifrawisNone:continuetyped[field]=(raw=="true"ifrawelse"")ifspec.answer_typeisboolelserawreturnSetupAnswers.model_validate(typed)