Skip to content

Commit

Permalink
refactor: Add 'features' field to LocationCollectionSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
manuGil committed Jul 3, 2024
1 parent 0ec8695 commit 58fcb54
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion citizenvoice/apiapp/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 58fcb54

Please sign in to comment.