From d422abf81add3f01a331ce5d3c49c8ca3635a4f6 Mon Sep 17 00:00:00 2001 From: Jasper Janin Date: Wed, 15 Mar 2023 03:11:24 +0100 Subject: [PATCH 1/6] basic version --- backend/drtrottoir/serializers/__init__.py | 4 +++- .../serializers/schedule_serializer.py | 18 ++++++++++++++++++ backend/drtrottoir/urls.py | 3 ++- backend/drtrottoir/views/__init__.py | 4 +++- backend/drtrottoir/views/schedule_viewset.py | 15 +++++++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 backend/drtrottoir/serializers/schedule_serializer.py create mode 100644 backend/drtrottoir/views/schedule_viewset.py diff --git a/backend/drtrottoir/serializers/__init__.py b/backend/drtrottoir/serializers/__init__.py index 29011351..48b23e7c 100644 --- a/backend/drtrottoir/serializers/__init__.py +++ b/backend/drtrottoir/serializers/__init__.py @@ -4,6 +4,7 @@ from .user_serializer import UserSerializer from .user_partial import UserPartialSerializer from .visit_serializer import VisitSerializer +from .schedule_serializer import ScheduleSerializer __all__ = [ BuildingSerializer, @@ -11,5 +12,6 @@ RegionSerializer, UserSerializer, UserPartialSerializer, - VisitSerializer + VisitSerializer, + ScheduleSerializer ] diff --git a/backend/drtrottoir/serializers/schedule_serializer.py b/backend/drtrottoir/serializers/schedule_serializer.py new file mode 100644 index 00000000..c0e49b4d --- /dev/null +++ b/backend/drtrottoir/serializers/schedule_serializer.py @@ -0,0 +1,18 @@ +from drtrottoir.models import Schedule +from rest_framework import serializers +from .user_partial import UserPartialSerializer + + +class ScheduleSerializer(serializers.HyperlinkedModelSerializer): + """ + A serializer for schedules, showing tour info + """ + student = UserPartialSerializer(read_only=True) + tour = serializers.CharField( + source='tour.name', + read_only=True + ) + + class Meta: + model = Schedule + fields = ['date', 'student', 'tour', 'comment'] diff --git a/backend/drtrottoir/urls.py b/backend/drtrottoir/urls.py index b2230fc0..3a12a4b0 100644 --- a/backend/drtrottoir/urls.py +++ b/backend/drtrottoir/urls.py @@ -1,13 +1,14 @@ from django.urls import path from rest_framework import routers from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView -from .views import BuildingViewSet, RegionViewSet, VisitViewSet, UserViewSet +from .views import BuildingViewSet, RegionViewSet, VisitViewSet, UserViewSet, ScheduleViewSet router = routers.DefaultRouter() router.register(r'building', BuildingViewSet) router.register(r'region', RegionViewSet) router.register(r'visit', VisitViewSet) router.register(r'user', UserViewSet) +router.register(r'schedule', ScheduleViewSet) urlpatterns = [ path('user/auth/', TokenObtainPairView.as_view(), name='token_obtain_pair'), diff --git a/backend/drtrottoir/views/__init__.py b/backend/drtrottoir/views/__init__.py index fee65a6e..63a11c4a 100644 --- a/backend/drtrottoir/views/__init__.py +++ b/backend/drtrottoir/views/__init__.py @@ -2,10 +2,12 @@ from .region_viewset import RegionViewSet from .visit_viewset import VisitViewSet from .user_viewset import UserViewSet +from .schedule_viewset import ScheduleViewSet __all__ = [ BuildingViewSet, RegionViewSet, VisitViewSet, - UserViewSet + UserViewSet, + ScheduleViewSet ] diff --git a/backend/drtrottoir/views/schedule_viewset.py b/backend/drtrottoir/views/schedule_viewset.py new file mode 100644 index 00000000..d5bf4e75 --- /dev/null +++ b/backend/drtrottoir/views/schedule_viewset.py @@ -0,0 +1,15 @@ +from rest_framework import viewsets +from rest_framework.permissions import IsAuthenticated +from drtrottoir.models import Schedule +from drtrottoir.permissions.super_permission import SuperPermissionOrReadOnly +from drtrottoir.serializers import ScheduleSerializer + + +class ScheduleViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows regions to be viewed or edited. + """ + + queryset = Schedule.objects.all() + serializer_class = ScheduleSerializer + permission_classes = [IsAuthenticated & SuperPermissionOrReadOnly] From e244ce3d443c411bd5f040e9b54d9aba4662985a Mon Sep 17 00:00:00 2001 From: Jasper Janin Date: Wed, 15 Mar 2023 15:30:37 +0100 Subject: [PATCH 2/6] add tour info to schedule --- backend/drtrottoir/serializers/__init__.py | 4 +++- .../serializers/schedule_serializer.py | 8 +++----- backend/drtrottoir/serializers/tour_partial.py | 17 +++++++++++++++++ .../drtrottoir/serializers/visit_serializer.py | 2 ++ 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 backend/drtrottoir/serializers/tour_partial.py diff --git a/backend/drtrottoir/serializers/__init__.py b/backend/drtrottoir/serializers/__init__.py index 84b826c8..82079980 100644 --- a/backend/drtrottoir/serializers/__init__.py +++ b/backend/drtrottoir/serializers/__init__.py @@ -7,6 +7,7 @@ from .user_partial import UserPartialSerializer from .visit_serializer import VisitSerializer from .schedule_serializer import ScheduleSerializer +from .tour_partial import TourPartialSerializer __all__ = [ BuildingSerializer, @@ -17,5 +18,6 @@ VisitSerializer, ScheduleSerializer, TourSerializer, - BuildingInTourSerializer + BuildingInTourSerializer, + TourPartialSerializer ] diff --git a/backend/drtrottoir/serializers/schedule_serializer.py b/backend/drtrottoir/serializers/schedule_serializer.py index c0e49b4d..10265147 100644 --- a/backend/drtrottoir/serializers/schedule_serializer.py +++ b/backend/drtrottoir/serializers/schedule_serializer.py @@ -1,6 +1,7 @@ from drtrottoir.models import Schedule from rest_framework import serializers from .user_partial import UserPartialSerializer +from .tour_partial import TourPartialSerializer class ScheduleSerializer(serializers.HyperlinkedModelSerializer): @@ -8,11 +9,8 @@ class ScheduleSerializer(serializers.HyperlinkedModelSerializer): A serializer for schedules, showing tour info """ student = UserPartialSerializer(read_only=True) - tour = serializers.CharField( - source='tour.name', - read_only=True - ) + tour = TourPartialSerializer(read_only=True) class Meta: model = Schedule - fields = ['date', 'student', 'tour', 'comment'] + fields = ['url', 'date', 'student', 'tour', 'comment'] diff --git a/backend/drtrottoir/serializers/tour_partial.py b/backend/drtrottoir/serializers/tour_partial.py new file mode 100644 index 00000000..1b6b8523 --- /dev/null +++ b/backend/drtrottoir/serializers/tour_partial.py @@ -0,0 +1,17 @@ +from rest_framework import serializers +from drtrottoir.models import Tour + + +class TourPartialSerializer(serializers.HyperlinkedModelSerializer): + """ + A partial serializer for Tour, not showing recursive relations + """ + region_name = serializers.CharField(source='region.region_name', read_only=True) + + class Meta: + model = Tour + fields = [ + 'url', + 'name', + 'region_name' + ] diff --git a/backend/drtrottoir/serializers/visit_serializer.py b/backend/drtrottoir/serializers/visit_serializer.py index 6b72bd31..54692e37 100644 --- a/backend/drtrottoir/serializers/visit_serializer.py +++ b/backend/drtrottoir/serializers/visit_serializer.py @@ -2,6 +2,7 @@ from drtrottoir.models import Visit from .building_partial import BuildingPartialSerializer from .user_partial import UserPartialSerializer +from .tour_partial import TourPartialSerializer class VisitSerializer(serializers.HyperlinkedModelSerializer): @@ -11,6 +12,7 @@ class VisitSerializer(serializers.HyperlinkedModelSerializer): user = UserPartialSerializer() building = BuildingPartialSerializer() + tour = TourPartialSerializer() class Meta: model = Visit From a2d87e79e5f964e61f197d74077bb605979a7e75 Mon Sep 17 00:00:00 2001 From: Jasper Janin Date: Wed, 15 Mar 2023 20:56:38 +0100 Subject: [PATCH 3/6] consistency between model & api --- backend/drtrottoir/models/custom_user.py | 2 +- backend/drtrottoir/serializers/building_serializer.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/drtrottoir/models/custom_user.py b/backend/drtrottoir/models/custom_user.py index 3fca40c9..6cb9e2d7 100644 --- a/backend/drtrottoir/models/custom_user.py +++ b/backend/drtrottoir/models/custom_user.py @@ -49,7 +49,7 @@ class CustomUser(AbstractBaseUser): last_name = models.CharField(verbose_name="last name", max_length=256, default='default') region = models.ForeignKey(Region, verbose_name="Region of the user", on_delete=models.SET_NULL, null=True) role = models.IntegerField(choices=Roles.choices, default=Roles.STUDENT) - buildings = models.ManyToManyField(Building, related_name='users') + buildings = models.ManyToManyField(Building, related_name='owners') objects = CustomUserManager() diff --git a/backend/drtrottoir/serializers/building_serializer.py b/backend/drtrottoir/serializers/building_serializer.py index 62b878f4..c32e42c8 100644 --- a/backend/drtrottoir/serializers/building_serializer.py +++ b/backend/drtrottoir/serializers/building_serializer.py @@ -11,7 +11,7 @@ class BuildingSerializer(serializers.HyperlinkedModelSerializer): source='region.region_name', read_only=True ) - users = UserPartialSerializer(many=True) + owners = UserPartialSerializer(many=True) class Meta: model = Building From d7e44a861540e64381cb8d59c29dd5b820e69b0f Mon Sep 17 00:00:00 2001 From: Jasper Janin Date: Thu, 16 Mar 2023 20:48:38 +0100 Subject: [PATCH 4/6] no nested serializers to allow pushing --- backend/drtrottoir/serializers/schedule_serializer.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/drtrottoir/serializers/schedule_serializer.py b/backend/drtrottoir/serializers/schedule_serializer.py index 10265147..b93dd5e3 100644 --- a/backend/drtrottoir/serializers/schedule_serializer.py +++ b/backend/drtrottoir/serializers/schedule_serializer.py @@ -8,8 +8,6 @@ class ScheduleSerializer(serializers.HyperlinkedModelSerializer): """ A serializer for schedules, showing tour info """ - student = UserPartialSerializer(read_only=True) - tour = TourPartialSerializer(read_only=True) class Meta: model = Schedule From 656e84aa0c65898872167be4fdd5b513a285ce33 Mon Sep 17 00:00:00 2001 From: Jasper Janin Date: Thu, 16 Mar 2023 20:53:09 +0100 Subject: [PATCH 5/6] linter --- backend/drtrottoir/serializers/schedule_serializer.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/drtrottoir/serializers/schedule_serializer.py b/backend/drtrottoir/serializers/schedule_serializer.py index b93dd5e3..69a4e1bc 100644 --- a/backend/drtrottoir/serializers/schedule_serializer.py +++ b/backend/drtrottoir/serializers/schedule_serializer.py @@ -1,7 +1,5 @@ from drtrottoir.models import Schedule from rest_framework import serializers -from .user_partial import UserPartialSerializer -from .tour_partial import TourPartialSerializer class ScheduleSerializer(serializers.HyperlinkedModelSerializer): From 9ca034afb190350f684a81cfeed794304dab2ba5 Mon Sep 17 00:00:00 2001 From: Jasper Janin Date: Thu, 16 Mar 2023 20:54:00 +0100 Subject: [PATCH 6/6] small migration --- .../0015_alter_customuser_buildings.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 backend/drtrottoir/migrations/0015_alter_customuser_buildings.py diff --git a/backend/drtrottoir/migrations/0015_alter_customuser_buildings.py b/backend/drtrottoir/migrations/0015_alter_customuser_buildings.py new file mode 100644 index 00000000..558ee861 --- /dev/null +++ b/backend/drtrottoir/migrations/0015_alter_customuser_buildings.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.7 on 2023-03-16 19:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('drtrottoir', '0014_photo_image_alter_photo_visit'), + ] + + operations = [ + migrations.AlterField( + model_name='customuser', + name='buildings', + field=models.ManyToManyField(related_name='owners', to='drtrottoir.building'), + ), + ]