Skip to content

Commit

Permalink
change event to Observation
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos committed Jun 28, 2024
1 parent 8758e0b commit db6c77e
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 217 deletions.
10 changes: 1 addition & 9 deletions backend/project/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.6 on 2024-06-12 20:15
# Generated by Django 5.0.6 on 2024-06-28 13:23

import django.db.models.functions.datetime
import django.utils.timezone
Expand Down Expand Up @@ -72,14 +72,6 @@ class Migration(migrations.Migration):
verbose_name="date joined",
),
),
(
"is_staff",
models.BooleanField(
default=False,
help_text="Designates whether the user can log into this admin site.",
verbose_name="staff status",
),
),
(
"is_active",
models.BooleanField(
Expand Down
17 changes: 0 additions & 17 deletions backend/project/accounts/migrations/0002_remove_user_is_staff.py

This file was deleted.

File renamed without changes.
6 changes: 6 additions & 0 deletions backend/project/accounts/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.test import TestCase


class AccountAdminTestCase(TestCase):
def test_superuser_ca(self):
"""Test that only superuser can login to the admin site."""
6 changes: 6 additions & 0 deletions backend/project/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.test import TestCase


class AccountTestCase(TestCase):
def test_is_staff(self):
"""Test that is_staff is same value as is_superuser"""
8 changes: 4 additions & 4 deletions backend/project/api/filters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django_filters.fields import DateRangeField
from django_filters.rest_framework import FilterSet, filters

from project.events.models import Event
from project.observations.models import Observation


class EventFilterSet(FilterSet):
class ObservationFilterSet(FilterSet):
event_date = DateRangeField()
fields = filters.CharFilter(
method="filter_fields", help_text="filter fields you want to get"
Expand All @@ -14,5 +14,5 @@ def filter_fields(self, qs):
return qs

class Meta:
model = Event
fields = ["event_date", "event_subtype", "fields"]
model = Observation
fields = ["event_date", "observation_subtype", "fields"]
9 changes: 5 additions & 4 deletions backend/project/api/serializers/common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from project.api.serializers.observations import ObservationTypeSerializer
from rest_framework import serializers

from project.api.serializers.events import EventTypeSerializer


class SettingsSerializer(serializers.Serializer):
event_types = EventTypeSerializer(many=True, read_only=True)
event_url = serializers.HyperlinkedIdentityField(view_name="api:events-list")
observation_types = ObservationTypeSerializer(many=True, read_only=True)
observation_public_url = serializers.HyperlinkedIdentityField(
view_name="api:observations-list"
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
from rest_framework_gis import serializers as gis_serializers
from sorl.thumbnail import get_thumbnail

from project.events.models import Event, EventSubType, EventType, Media
from project.observations.models import (
ObservationSubType,
ObservationType,
Media,
Observation,
)


class EventSubTypeSerializer(serializers.ModelSerializer):
class ObservationSubTypeSerializer(serializers.ModelSerializer):
class Meta:
model = EventSubType
model = ObservationSubType
fields = (
"id",
"label",
Expand All @@ -19,11 +24,11 @@ class Meta:
)


class EventTypeSerializer(serializers.ModelSerializer):
sub_types = EventSubTypeSerializer(many=True)
class ObservationTypeSerializer(serializers.ModelSerializer):
sub_types = ObservationSubTypeSerializer(many=True)

class Meta:
model = EventType
model = ObservationType
fields = ("id", "label", "description", "pictogram", "sub_types")


Expand All @@ -32,24 +37,28 @@ class ThumbnailSerializer(serializers.Serializer):
medium = serializers.SerializerMethodField()
large = serializers.SerializerMethodField()

def get_thumbnail_by_size(self, obj, height=100, width=100, format="PNG"):
def get_thumbnail_by_size(
self, obj, height=100, width=100, format="JPG", 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="PNG").url
get_thumbnail(
obj.media_file, f"{height}x{width}", format=format, quality=quality
).url
)
return None

@extend_schema_field(OpenApiTypes.URI)
def get_small(self, obj):
return self.get_thumbnail_by_size(obj, 100, 100)
return self.get_thumbnail_by_size(obj, 449, 599)

@extend_schema_field(OpenApiTypes.URI)
def get_medium(self, obj):
return self.get_thumbnail_by_size(obj, 300, 300)
return self.get_thumbnail_by_size(obj, 720, 1280)

@extend_schema_field(OpenApiTypes.URI)
def get_large(self, obj):
return self.get_thumbnail_by_size(obj, 600, 600)
return self.get_thumbnail_by_size(obj, 1080, 1920)


class MediaSerializer(serializers.ModelSerializer):
Expand All @@ -60,15 +69,15 @@ class Meta:
fields = ("id", "uuid", "legend", "media_file", "media_type", "thumbnails")


class EventListSerializer(
class ObservationListSerializer(
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)

class Meta:
model = Event
model = Observation
geo_field = "location"
fields = (
"id",
Expand All @@ -77,14 +86,14 @@ class Meta:
"event_date",
"source",
"subtype",
"event_subtype",
"observation_subtype",
"location",
)
write_only_fields = ("event_subtype__id",)
write_only_fields = ("observation_subtype__id",)


class EventDetailSerializer(EventListSerializer):
class ObservationDetailSerializer(ObservationListSerializer):
medias = MediaSerializer(many=True, read_only=True)

class Meta(EventListSerializer.Meta):
fields = EventListSerializer.Meta.fields + ("medias",)
class Meta(ObservationListSerializer.Meta):
fields = ObservationListSerializer.Meta.fields + ("medias",)
6 changes: 4 additions & 2 deletions backend/project/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

router = DefaultRouter()

router.register(r"events", api.EventViewSet, basename="events")
router.register(r"observations", api.ObservationViewSet, basename="observations")
router.register(
r"accounts/me/events", api.AccountEventViewset, basename="account-events"
r"accounts/me/observations",
api.AccountObservationViewset,
basename="account-observations",
)

urlpatterns = [
Expand Down
48 changes: 27 additions & 21 deletions backend/project/api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from django_filters.rest_framework import DjangoFilterBackend
from project.api.serializers.observations import (
ObservationDetailSerializer,
ObservationListSerializer,
ObservationTypeSerializer,
)
from project.observations.models import Observation, ObservationType
from rest_framework import permissions, status, viewsets
from rest_framework.decorators import action
from rest_framework.generics import GenericAPIView
Expand All @@ -12,40 +18,38 @@
from rest_framework.viewsets import GenericViewSet

from project.accounts.models import User
from project.api.filters import EventFilterSet
from project.api.filters import ObservationFilterSet
from project.api.serializers.accounts import AccountSerializer
from project.api.serializers.common import SettingsSerializer
from project.api.serializers.events import (
EventDetailSerializer,
EventListSerializer,
EventTypeSerializer,
)
from project.events.models import Event, EventType


class SettingsApiView(GenericAPIView):
serializer_class = SettingsSerializer

def get(self, request):
data = {}
event_types = EventType.objects.prefetch_related("sub_types").all()
event_types_serialized = EventTypeSerializer(
event_types, many=True, context={"request": request}
observation_types = ObservationType.objects.prefetch_related("sub_types").all()
observation_types_serialized = ObservationTypeSerializer(
observation_types, many=True, context={"request": request}
)
data["observation_types"] = observation_types_serialized.data
data["observation_public_url"] = reverse(
"api:observations-list", request=request
)
data["event_types"] = event_types_serialized.data
data["event_url"] = reverse("api:events-list", request=request)
return Response(data)


class EventViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Event.objects.all().select_related("source", "event_subtype__event_type")
class ObservationViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Observation.objects.all().select_related(
"source", "observation_subtype__observation_type"
)
filter_backends = (DjangoFilterBackend,)
filterset_class = EventFilterSet
filterset_class = ObservationFilterSet

def get_serializer_class(self):
if self.action == "list":
return EventListSerializer
return EventDetailSerializer
return ObservationListSerializer
return ObservationDetailSerializer


class AccountViewSet(
Expand All @@ -69,16 +73,18 @@ def signup(self, request, *args, **kwargs):
return Response(serializer.data, status=status.HTTP_201_CREATED)


class AccountEventViewset(viewsets.ModelViewSet):
class AccountObservationViewset(viewsets.ModelViewSet):
filter_backends = (DjangoFilterBackend,)
filterset_class = ObservationFilterSet
permission_classes = [permissions.IsAuthenticated]

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

def get_serializer_class(self):
if self.action == "list":
return EventListSerializer
return EventDetailSerializer
return ObservationListSerializer
return ObservationDetailSerializer

def perform_create(self, serializer):
serializer.save(owner=self.request.user)
20 changes: 0 additions & 20 deletions backend/project/events/migrations/0002_alter_media_uuid.py

This file was deleted.

This file was deleted.

File renamed without changes.
Loading

0 comments on commit db6c77e

Please sign in to comment.