Skip to content

Commit

Permalink
Add device, site and data source to Deployment responses
Browse files Browse the repository at this point in the history
(untested!)
  • Loading branch information
mihow committed Dec 11, 2023
1 parent 1f26335 commit 29265a0
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions ami/main/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,42 @@ class Meta:
]


class DeviceNestedSerializer(DefaultSerializer):
class Meta:
model = Device
fields = [
"id",
"name",
"details",
]


class SiteNestedSerializer(DefaultSerializer):
class Meta:
model = Site
fields = [
"id",
"name",
"details",
]


class StorageSourceNestedSerializer(DefaultSerializer):
class Meta:
model = S3StorageSource
fields = [
"id",
"name",
"details",
]


class DeploymentListSerializer(DefaultSerializer):
events = serializers.SerializerMethodField()
occurrences = serializers.SerializerMethodField()
project = ProjectNestedSerializer(read_only=True)
device = DeviceNestedSerializer(read_only=True)
site = SiteNestedSerializer(read_only=True)

class Meta:
model = Deployment
Expand All @@ -108,6 +140,8 @@ class Meta:
"longitude",
"first_date",
"last_date",
"device",
"site",
]

def get_events(self, obj):
Expand Down Expand Up @@ -285,24 +319,51 @@ class DeploymentSerializer(DeploymentListSerializer):
events = DeploymentEventNestedSerializer(many=True, read_only=True)
occurrences = serializers.SerializerMethodField()
example_captures = DeploymentCaptureNestedSerializer(many=True, read_only=True)
data_source = serializers.SerializerMethodField(read_only=True)
project_id = serializers.PrimaryKeyRelatedField(
write_only=True,
queryset=Project.objects.all(),
source="project",
)
device_id = serializers.PrimaryKeyRelatedField(
write_only=True,
queryset=Device.objects.all(),
source="device",
)
site_id = serializers.PrimaryKeyRelatedField(
write_only=True,
queryset=Site.objects.all(),
source="site",
)
data_source = serializers.SerializerMethodField()
data_source_id = serializers.PrimaryKeyRelatedField(
write_only=True,
queryset=S3StorageSource.objects.all(),
source="data_source",
)

class Meta(DeploymentListSerializer.Meta):
fields = DeploymentListSerializer.Meta.fields + [
"project_id",
"description",
"device_id",
"site_id",
"data_source",
"data_source_id",
"description",
"example_captures",
# "capture_images",
]

def get_data_source(self, obj):
return obj.data_source_uri()
"""
Add uri to nested serializer of the data source
The data source is defined by both the StorageSource model
and the extra configuration in the Deployment model.
"""

data = StorageSourceNestedSerializer(obj.data_source, context=self.context).data
data["uri"] = obj.data_source_uri()
return data

def get_occurrences(self, obj):
"""
Expand Down

0 comments on commit 29265a0

Please sign in to comment.