From 1984213b2a8d847f32b5ba4f39bbbd61740b04da Mon Sep 17 00:00:00 2001 From: Juho Kettunen Date: Thu, 21 Nov 2024 13:42:36 +0200 Subject: [PATCH] Remove early returns from validator to increase readability --- leasing/serializers/rent.py | 80 +++++++++++-------------------------- 1 file changed, 24 insertions(+), 56 deletions(-) diff --git a/leasing/serializers/rent.py b/leasing/serializers/rent.py index 4d86f616..7235ce53 100644 --- a/leasing/serializers/rent.py +++ b/leasing/serializers/rent.py @@ -21,6 +21,7 @@ RentAdjustment, RentDueDate, RentIntendedUse, + ServiceUnit, ) from leasing.models.rent import ( EqualizedRent, @@ -639,62 +640,39 @@ def full_validate_override_receivable_type( Raises: serializers.ValidationError """ rent = Rent.objects.select_related("lease__service_unit").get(pk=rent_id) - use_rent_override_receivable_type: bool = ( + rents_service_unit_uses_override: bool = ( rent.lease.service_unit.use_rent_override_receivable_type ) override_receivable_type: ReceivableType | None = rent_data.get( "override_receivable_type" ) - - if (not use_rent_override_receivable_type) and (not override_receivable_type): - # This service unit does not use the override receivabletype feature, - # and none was supplied -> all good. - # For example, service unit MaKe/Tontit. - return - - if use_rent_override_receivable_type and override_receivable_type: - if rent_type_uses_override: - # Override receivabletype is required, was supplied, and rent - # type is correct -> all good. - # For example, service units AKV and KuVa. - return - else: - raise serializers.ValidationError( - _( - f'Override receivabletype "{override_receivable_type.name}" was unexpected. ' - f"This rent type does not generate automatic invoices. " - "Please contact MVJ developers about this error." - ) - ) - - if use_rent_override_receivable_type and (not override_receivable_type): - if not rent_type_uses_override: - # This rent type does not utilize rent_override_receivable_type, - # even if the service unit generally uses it. - return - else: - raise serializers.ValidationError( - _( - f'Override receivabletype is required for service unit "{rent.lease.service_unit.name}", ' - f"and for this rent type." - ) - ) - - if (not use_rent_override_receivable_type) and override_receivable_type: + if override_receivable_type and (not rents_service_unit_uses_override): raise serializers.ValidationError( _( - f'Override receivabletype "{override_receivable_type.name}" was unexpected. ' + f'Override receivable type "{override_receivable_type.name}" was unexpected. ' f'Service unit "{rent.lease.service_unit.name}" does not use this feature. ' "Please contact MVJ developers about this error." ) ) - - raise serializers.ValidationError( - _( - "Unhandled case in override receivabletype validation. Rejecting just in case. " - "Please contact MVJ developers about this error." + if override_receivable_type and (not rent_type_uses_override): + raise serializers.ValidationError( + _( + f'Override receivable type "{override_receivable_type.name}" was unexpected. ' + f"This rent type does not generate automatic invoices. " + "Please contact MVJ developers about this error." + ) + ) + if ( + rents_service_unit_uses_override + and rent_type_uses_override + and (not override_receivable_type) + ): + raise serializers.ValidationError( + _( + "Override receivable type is required for this rent type in service unit " + f'"{rent.lease.service_unit.name}".' + ) ) - ) def minimal_validate_override_receivable_type( self, @@ -725,17 +703,8 @@ def minimal_validate_override_receivable_type( # Without it, we cannot make further validations about its properties. return - service_unit = override_receivable_type.service_unit - receivabletypes_service_unit_uses_override = ( - service_unit.use_rent_override_receivable_type - ) - - if receivabletypes_service_unit_uses_override and rent_type_uses_override: - # Service unit and rent types require an override receivabletype, and it was supplied - # --> all good. - return - - if not receivabletypes_service_unit_uses_override: + service_unit: ServiceUnit = override_receivable_type.service_unit + if not service_unit.use_rent_override_receivable_type: raise serializers.ValidationError( _( f'Override receivabletype "{override_receivable_type.name}" was unexpected. ' @@ -743,7 +712,6 @@ def minimal_validate_override_receivable_type( "Please contact MVJ developers about this error." ) ) - if not rent_type_uses_override: raise serializers.ValidationError( _(