Skip to content

Commit

Permalink
add first photo in observation API lists, change thumbnails to JPEG/70 (
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos authored Jul 2, 2024
1 parent 39c1b64 commit ddb9309
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
28 changes: 16 additions & 12 deletions backend/project/api/serializers/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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",)
12 changes: 9 additions & 3 deletions backend/project/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down
4 changes: 4 additions & 0 deletions backend/project/observations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ddb9309

Please sign in to comment.