Skip to content

Commit

Permalink
Add resolved_middleware JSON field to Asset Serializer.
Browse files Browse the repository at this point in the history
fixes #1740;
  • Loading branch information
rithviknishad committed Dec 4, 2023
1 parent 8bdbb42 commit f1bff5c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import transaction
from django.shortcuts import get_object_or_404
from django.utils.timezone import now
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.serializers import (
Expand Down Expand Up @@ -133,6 +134,34 @@ class AssetSerializer(ModelSerializer):
last_serviced_on = serializers.DateField(write_only=True, required=False)
note = serializers.CharField(write_only=True, required=False, allow_blank=True)

resolved_middleware = serializers.SerializerMethodField()

@extend_schema_field(
{
"type": "object",
"properties": {
"hostname": {"type": "string"},
"source": {"type": "string", "enum": ["asset", "location", "facility"]},
},
}
)
def get_resolved_middleware(self, instance):
if hostname := instance.meta.get("middleware_hostname"):
return {
"hostname": hostname,
"source": "asset",
}
if hostname := instance.current_location.middleware_address:
return {
"hostname": hostname,
"source": "location",
}
if hostname := instance.current_location.facility.middleware_address:
return {
"hostname": hostname,
"source": "facility",
}

class Meta:
model = Asset
exclude = ("deleted", "external_id", "current_location")
Expand Down

0 comments on commit f1bff5c

Please sign in to comment.