Skip to content

Commit

Permalink
serializer for intended use in lease & use SameServiceUnitValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
henrinie-nc committed Jun 11, 2024
1 parent d8fb8f8 commit bf70e77
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
3 changes: 3 additions & 0 deletions leasing/models/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class Meta(NameModel.Meta):
verbose_name = pgettext_lazy("Model name", "Intended use")
verbose_name_plural = pgettext_lazy("Model name", "Intended uses")

def get_service_unit(self):
return self.service_unit


class StatisticalUse(NameModel):
"""
Expand Down
29 changes: 9 additions & 20 deletions leasing/serializers/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.contrib.gis.db.models import Union
from django.db.models import DurationField, Q
from django.db.models.functions import Cast
from django.utils.translation import gettext
from django.utils.translation import gettext_lazy as _
from enumfields.drf import EnumField, EnumSupportSerializerMixin
from rest_framework import serializers
Expand Down Expand Up @@ -418,6 +417,7 @@ class LeaseSerializerBase(
tenants = TenantSerializer(many=True, required=False, allow_null=True)
lease_areas = LeaseAreaSerializer(many=True, required=False, allow_null=True)
lessor = ContactSerializer(required=False, allow_null=True)
intended_use = IntendedUseSerializer(required=False, allow_null=True)
contracts = ContractSerializer(many=True, required=False, allow_null=True)
decisions = DecisionSerializer(many=True, required=False, allow_null=True)
inspections = InspectionSerializer(many=True, required=False, allow_null=True)
Expand Down Expand Up @@ -609,13 +609,6 @@ def __call__(self, value, serializer_field):
)


def validate_intended_use_service_unit(intended_use_service_unit, lease_service_unit):
if intended_use_service_unit != lease_service_unit:
raise serializers.ValidationError(
gettext("Intended use's service unit must match lease's service unit")
)


class LeaseUpdateSerializer(
UpdateNestedMixin,
EnumSupportSerializerMixin,
Expand All @@ -636,6 +629,14 @@ class LeaseUpdateSerializer(
allow_null=True,
validators=[SameServiceUnitValidator()],
)
intended_use = InstanceDictPrimaryKeyRelatedField(
instance_class=IntendedUse,
queryset=IntendedUse.objects.all(),
related_serializer=IntendedUseSerializer,
required=False,
allow_null=True,
validators=[SameServiceUnitValidator()],
)
contracts = ContractCreateUpdateSerializer(
many=True, required=False, allow_null=True
)
Expand Down Expand Up @@ -686,12 +687,6 @@ def validate_service_unit(self, value):

return value

def validate_intended_use(self, value):
if self.instance and value.service_unit is not None:
validate_intended_use_service_unit(
value.service_unit, self.instance.service_unit
)

class Meta:
model = Lease
fields = "__all__"
Expand Down Expand Up @@ -722,12 +717,6 @@ def validate_service_unit(self, value):

return value

def validate_intended_use(self, value):
if self.instance and value.service_unit is not None:
validate_intended_use_service_unit(
value.service_unit, self.instance.service_unit
)

class Meta:
model = Lease
fields = "__all__"
Expand Down

0 comments on commit bf70e77

Please sign in to comment.