From 58fcb54f1fbbdc0890a2f987e90a2df42498a632 Mon Sep 17 00:00:00 2001 From: Manuel Garcia Date: Wed, 3 Jul 2024 15:41:24 +0200 Subject: [PATCH] refactor: Add 'features' field to LocationCollectionSerializer --- citizenvoice/apiapp/serializers.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/citizenvoice/apiapp/serializers.py b/citizenvoice/apiapp/serializers.py index bc2f3bc0..2a069c18 100644 --- a/citizenvoice/apiapp/serializers.py +++ b/citizenvoice/apiapp/serializers.py @@ -163,12 +163,33 @@ class LocationCollectionSerializer(serializers.HyperlinkedModelSerializer): Serialises 'name', 'question', 'answer', 'points', 'lines', 'polygons' fields of the Location model for the API. """ + features = serializers.SerializerMethodField() class Meta: model = LocationCollection - fields = ('id', 'url', 'name', 'description') + fields = ('id', 'url', 'name', 'description', 'features') read_only_fields = ('id', 'url') + def get_features(self, obj): + """ + Returns a list of URLs of all the features (points, lines, polygons) + associated with the location collection. + """ + points = PointFeatureSerializer(PointFeature.objects.filter(location__id=obj.pk), + many=True, + context={'request': self.context.get('request')}).data + lines = LineFeatureSerializer(LineFeature.objects.filter(location__id=obj.pk), + many=True, + context={'request': self.context.get('request')}).data + polygons = PolygonFeatureSerializer(PolygonFeature.objects.filter(location__id=obj.pk), + many=True, + context={'request': self.context.get('request')}).data + + features = points + lines + polygons + feature_urls = [f['url'] for f in features] + + return feature_urls + class AnswerCSVSerializer(serializers.ModelSerializer): """