Skip to content

Commit

Permalink
Remove early returns from validator
Browse files Browse the repository at this point in the history
to increase readability
  • Loading branch information
juho-kettunen-nc committed Nov 21, 2024
1 parent b17691f commit 1984213
Showing 1 changed file with 24 additions and 56 deletions.
80 changes: 24 additions & 56 deletions leasing/serializers/rent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
RentAdjustment,
RentDueDate,
RentIntendedUse,
ServiceUnit,
)
from leasing.models.rent import (
EqualizedRent,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -725,25 +703,15 @@ 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. '
f'Override receivabletype is not used by service unit "{service_unit.name}". '
"Please contact MVJ developers about this error."
)
)

if not rent_type_uses_override:
raise serializers.ValidationError(
_(
Expand Down

0 comments on commit 1984213

Please sign in to comment.