Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add first photo in observation API lists, change thumbnails to JPEG/70 #12

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@


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 @@
permission_classes = [permissions.IsAuthenticated]

def get_queryset(self):
return self.request.user.observations.all()
return (

Check warning on line 84 in backend/project/api/views.py

View check run for this annotation

Codecov / codecov/patch

backend/project/api/views.py#L84

Added line #L84 was not covered by tests
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 @@
)
location = models.PointField(srid=4326, verbose_name=_("Location"))

@property
def first_photo(self):
return self.medias.first()

Check warning on line 73 in backend/project/observations/models.py

View check run for this annotation

Codecov / codecov/patch

backend/project/observations/models.py#L73

Added line #L73 was not covered by tests

def __str__(self):
return str(self.uuid)

Expand Down