From 152039ae87c6437d27c28205de1abadebaa459b3 Mon Sep 17 00:00:00 2001 From: J-E Castagnede Date: Wed, 10 Jul 2024 16:24:42 +0200 Subject: [PATCH] use uuid in endpoints --- .../project/api/serializers/observations.py | 11 +++++++++-- backend/project/api/views.py | 9 ++++++--- backend/project/observations/admin.py | 19 +++++++++++++++++++ backend/project/observations/models.py | 9 +++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/backend/project/api/serializers/observations.py b/backend/project/api/serializers/observations.py index 131920e..85d2956 100644 --- a/backend/project/api/serializers/observations.py +++ b/backend/project/api/serializers/observations.py @@ -1,3 +1,4 @@ +from django.utils.translation import gettext as _ from drf_dynamic_fields import DynamicFieldsMixin from drf_spectacular.types import OpenApiTypes from drf_spectacular.utils import extend_schema_field @@ -70,19 +71,25 @@ class Meta: class ObservationMixin(DynamicFieldsMixin, gis_serializers.GeoFeatureModelSerializer): - source = serializers.SlugRelatedField("label", read_only=True) + source = serializers.SerializerMethodField() + name = serializers.CharField(source="public_name") + observer = serializers.SlugRelatedField("nickname", read_only=True) + + def get_source(self, obj): + return obj.source.name if obj.source else _("Regard d'altitude") class Meta: model = Observation geo_field = "location" + id_field = "uuid" fields = ( - "id", "uuid", "name", "comments", "event_date", "source", "category", + "observer", ) write_only_fields = ("category__id",) diff --git a/backend/project/api/views.py b/backend/project/api/views.py index 4efbd46..745081e 100644 --- a/backend/project/api/views.py +++ b/backend/project/api/views.py @@ -39,11 +39,12 @@ def get(self, request): class ObservationViewSet(viewsets.ReadOnlyModelViewSet): queryset = ( Observation.objects.all() - .select_related("source", "category") + .select_related("source", "category", "observer") .prefetch_related("medias") ) filter_backends = (DjangoFilterBackend,) filterset_class = ObservationFilterSet + lookup_field = "uuid" def get_serializer_class(self): if self.action == "list": @@ -80,11 +81,13 @@ class AccountObservationViewset(viewsets.ModelViewSet): filter_backends = (DjangoFilterBackend,) filterset_class = ObservationFilterSet permission_classes = [permissions.IsAuthenticated] + lookup_field = "uuid" + lookup_url_kwarg = "uuid" def get_queryset(self): return ( self.request.user.observations.all() - .select_related("source", "category") + .select_related("source", "category", "observer") .prefetch_related("medias") ) @@ -94,4 +97,4 @@ def get_serializer_class(self): return ObservationDetailSerializer def perform_create(self, serializer): - serializer.save(owner=self.request.user) + serializer.save(observer=self.request.user) diff --git a/backend/project/observations/admin.py b/backend/project/observations/admin.py index 84a6c0e..c8775ea 100644 --- a/backend/project/observations/admin.py +++ b/backend/project/observations/admin.py @@ -72,6 +72,25 @@ class ObservationAdmin(GISModelAdmin): date_hierarchy = "event_date" readonly_fields = ("uuid",) inlines = [MediaInline] + fieldsets = ( + ( + None, + { + "fields": ( + ("category", "name"), + ("event_date", "observer"), + "comments", + "location", + ) + }, + ), + ( + _("Other"), + { + "fields": (("source", "uuid"),), + }, + ), + ) def get_queryset(self, request): return ( diff --git a/backend/project/observations/models.py b/backend/project/observations/models.py index 93f551e..bac2f79 100644 --- a/backend/project/observations/models.py +++ b/backend/project/observations/models.py @@ -86,6 +86,15 @@ class Observation(TimeStampMixin): def main_picture(self): return self.medias.filter(media_type=MediaType.IMAGE).first() + @property + def public_name(self): + """Return category label if name is empty.""" + return self.name or self.category.label + + @public_name.setter + def public_name(self, value): + self.name = value + def __str__(self): return str(self.uuid)