"""Contract Flowthrough marshmallow schema.""" from decimal import Decimal from abacus_common_logic.marshalling.custom_fields import ma from marshmallow import ValidationError, validate from abacus_contract.constants.constants import ( CONTRACT_FLOWTHROUGH_PREVIOUS_STATUSES, CONTRACT_FLOWTHROUGH_STATUSES, ) def _validate_recoupment_cap_decimal_places(value): if abs(Decimal(str(value)).as_tuple().exponent) > 2: raise ValidationError('Ensure that there are no more than 2 decimal places.') _RECOUPMENT_CAP_VALIDATORS = [ validate.Range(min=0, min_inclusive=False), _validate_recoupment_cap_decimal_places, ] class BaseContractFlowthroughSchema(ma.Schema): """Shared contract_flowthrough fields.""" contract_id = ma.IntegerId(required=True) reference_flowthrough_calculation_id = ma.IntegerId(required=True) flowthrough_rate = ma.Decimal(as_string=True, required=True) flowthrough_status = ma.Enum( options=CONTRACT_FLOWTHROUGH_STATUSES, required=True, data_key='flowthrough_status', ) has_automatic_shutoff = ma.Boolean(required=False) recoupment_cap = ma.Float(required=False, validate=_RECOUPMENT_CAP_VALIDATORS) calculation_comment = ma.String(required=False, allow_none=True) previous_flowthrough_status = ma.Enum( options=CONTRACT_FLOWTHROUGH_PREVIOUS_STATUSES, required=False, data_key='previous_flowthrough_status', ) status_last_modified_by = ma.String(allow_none=True, required=False) status_last_modified = ma.FormattedDate(allow_none=True, required=False) class ContractFlowthroughDetailSchema(BaseContractFlowthroughSchema): """Schema for contract_flowthrough details.""" contract_flowthrough_id = ma.IntegerId(required=True) class ContractFlowthroughPutSchema(ma.Schema): """Schema for contract_flowthrough PUT request.""" reference_flowthrough_calculation_id = ma.IntegerId(required=False) flowthrough_rate = ma.Decimal(as_string=True, required=False) flowthrough_status = ma.Enum( options=CONTRACT_FLOWTHROUGH_STATUSES, required=False, data_key='flowthrough_status', ) has_automatic_shutoff = ma.Boolean(required=False) recoupment_cap = ma.Float( required=False, allow_none=True, validate=_RECOUPMENT_CAP_VALIDATORS ) calculation_comment = ma.String(required=False, allow_none=True) class ContractFlowthroughPostSchema(ma.Schema): """Schema for contract_flowthrough POST request.""" reference_flowthrough_calculation_id = ma.IntegerId(required=True) flowthrough_rate = ma.Decimal(as_string=True, required=True) has_automatic_shutoff = ma.Boolean(required=False) recoupment_cap = ma.Float( required=False, allow_none=True, validate=_RECOUPMENT_CAP_VALIDATORS ) calculation_comment = ma.String(required=False, allow_none=True)