Source code for aeat.application.modelo._calculation_aggregation_context
"""Context loading for bucket-aggregation calculation actions."""from__future__importannotationsfrom...domain.calculations.registryimportRegistrySnapshotErrorfrom...domain.modelosimportWorkUnitCatalogueRepositoryProtocol,WorkUnitStatefrom._action_errorsimport(CalculationRegistryUnavailableError,WorkUnitMutationRefusedError,WorkUnitNotFoundError,WorkUnitRevisionDivergenceError,)from._profile_readiness_gateimportrequire_profile_ready_for_work_unitfrom._registry_resourcesimportauthority_via_resourcesas_authority_via_resourcesfrom._registry_resourcesimportregistry_rootas_registry_root
[docs]defload_bucket_aggregation_context(work_unit_id:str,*,work_unit_repository:WorkUnitCatalogueRepositoryProtocol,):"""Return ``(work_unit, snapshot)`` for aggregation calculation."""work_units=work_unit_repository.load()work_unit=work_units.get(work_unit_id)ifwork_unitisNone:raiseWorkUnitNotFoundError(translated_message="application.modelo.errors.work_unit_not_found",context={"work_unit_id":work_unit_id},)ifwork_unit.stateisWorkUnitState.DESCARTADO:raiseWorkUnitMutationRefusedError(translated_message="application.modelo.errors.work_unit_discarded_cannot_calculate",context={"work_unit_id":work_unit_id},)require_profile_ready_for_work_unit(work_unit)try:authority=_authority_via_resources()snapshot=authority.snapshot(work_unit.modelo,filing_year=work_unit.filing_year,period=work_unit.period.registry_token,)exceptFileNotFoundErrorasexc:raiseCalculationRegistryUnavailableError(translated_message="application.modelo.errors.calculation_registry_root_missing",context={"registry_root":_registry_root()},)fromexcexceptRegistrySnapshotErrorasexc:raiseCalculationRegistryUnavailableError(translated_message="application.modelo.errors.calculation_registry_snapshot_unresolved",context={"modelo":work_unit.modelo,"filing_year":work_unit.filing_year,"period":work_unit.period.registry_token,},)fromexcifsnapshot.revision.id!=work_unit.revision_id:raiseWorkUnitRevisionDivergenceError(f"work unit {work_unit.work_unit_id!r} was created against registry revision "f"{work_unit.revision_id!r}, but the law-determined revision for "f"modelo {work_unit.modelo!r}{work_unit.filing_year}{work_unit.period.registry_token!r} "f"is now {snapshot.revision.id!r}. "f"The registry's law-mapping was corrected after this work unit was created. "f"Re-create the work unit (discard this one and run `aeat app modelo work create`) "f"to bind it to the current law-determined revision.",)returnwork_unit,snapshot