diff --git a/backend/project/api/serializers/observations.py b/backend/project/api/serializers/observations.py index 38f0925..d5e0da6 100644 --- a/backend/project/api/serializers/observations.py +++ b/backend/project/api/serializers/observations.py @@ -38,12 +38,12 @@ class ThumbnailSerializer(serializers.Serializer): large = serializers.SerializerMethodField() def get_thumbnail_by_size( - self, obj, height=100, width=100, format="JPG", quality=70 + self, obj, height=100, width=100, format="JPEG", quality=70 ): if obj.media_type == Media.MediaType.IMAGE: return self.context["request"].build_absolute_uri( get_thumbnail( - obj.media_file, f"{height}x{width}", format=format, quality=quality + obj.media_file, f"{width}x{height}", format=format, quality=quality ).url ) return None @@ -69,12 +69,11 @@ class Meta: fields = ("id", "uuid", "legend", "media_file", "media_type", "thumbnails") -class ObservationListSerializer( - DynamicFieldsMixin, gis_serializers.GeoFeatureModelSerializer -): +class ObservationMixin(DynamicFieldsMixin, gis_serializers.GeoFeatureModelSerializer): source = serializers.SlugRelatedField("label", read_only=True) - subtype = serializers.SlugRelatedField("label", read_only=True) - # event_type = serializers.SlugRelatedField("event_subtype.event_type.label", read_only=True) + subtype = serializers.SlugRelatedField( + "label", source="observation_subtype", read_only=True + ) class Meta: model = Observation @@ -86,14 +85,19 @@ class Meta: "event_date", "source", "subtype", - "observation_subtype", - "location", ) write_only_fields = ("observation_subtype__id",) -class ObservationDetailSerializer(ObservationListSerializer): +class ObservationListSerializer(ObservationMixin): + first_photo = MediaSerializer(read_only=True) + + class Meta(ObservationMixin.Meta): + fields = ObservationMixin.Meta.fields + ("first_photo",) + + +class ObservationDetailSerializer(ObservationMixin): medias = MediaSerializer(many=True, read_only=True) - class Meta(ObservationListSerializer.Meta): - fields = ObservationListSerializer.Meta.fields + ("medias",) + class Meta(ObservationMixin.Meta): + fields = ObservationMixin.Meta.fields + ("medias",) diff --git a/backend/project/api/views.py b/backend/project/api/views.py index a15e6de..ac9d1dd 100644 --- a/backend/project/api/views.py +++ b/backend/project/api/views.py @@ -40,8 +40,10 @@ def get(self, request): class ObservationViewSet(viewsets.ReadOnlyModelViewSet): - queryset = Observation.objects.all().select_related( - "source", "observation_subtype__observation_type" + queryset = ( + Observation.objects.all() + .select_related("source", "observation_subtype__observation_type") + .prefetch_related("medias") ) filter_backends = (DjangoFilterBackend,) filterset_class = ObservationFilterSet @@ -79,7 +81,11 @@ class AccountObservationViewset(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] def get_queryset(self): - return self.request.user.observations.all() + return ( + self.request.user.observations.all() + .select_related("source", "observation_subtype__observation_type") + .prefetch_related("medias") + ) def get_serializer_class(self): if self.action == "list": diff --git a/backend/project/observations/models.py b/backend/project/observations/models.py index 6f2ed7d..27d37c9 100644 --- a/backend/project/observations/models.py +++ b/backend/project/observations/models.py @@ -68,6 +68,10 @@ class Observation(TimeStampMixin): ) location = models.PointField(srid=4326, verbose_name=_("Location")) + @property + def first_photo(self): + return self.medias.first() + def __str__(self): return str(self.uuid)