Skip to content

Commit

Permalink
use uuid in endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos committed Jul 10, 2024
1 parent 2c2c2e1 commit 152039a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
11 changes: 9 additions & 2 deletions backend/project/api/serializers/observations.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",)

Expand Down
9 changes: 6 additions & 3 deletions backend/project/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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")
)

Expand All @@ -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)
19 changes: 19 additions & 0 deletions backend/project/observations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
9 changes: 9 additions & 0 deletions backend/project/observations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 152039a

Please sign in to comment.