From a633949d1f645e03ede3c0004bfaec032e2c67d8 Mon Sep 17 00:00:00 2001 From: hridesh Date: Mon, 26 Aug 2024 19:18:39 +0530 Subject: [PATCH] feat: added endpoint to add extra class --- api/serializers.py | 25 +++++++++++++++++++------ api/urls.py | 1 + api/views.py | 7 ++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 6f267e9..4868433 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -29,12 +29,6 @@ def create(self, validated_data): return User.objects.create_user(**validated_data) -class SessionSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = Session - fields = ["url", "date", "status"] - - class ScheduleSerializer(serializers.ModelSerializer): day_of_week = serializers.IntegerField(write_only=True) day_of_week_str = serializers.CharField( @@ -68,6 +62,25 @@ def create(self, validated_data): return course +class SessionSerializer(serializers.HyperlinkedModelSerializer): + status = serializers.ChoiceField(choices=Session.status_choices, required=False) + course = serializers.PrimaryKeyRelatedField( + queryset=Course.objects.all(), write_only=True + ) + + class Meta: + model = Session + fields = ["url", "date", "status", "course"] + + def validate_course(self, course): + request = self.context.get("request") + if request and course.collection.user != request.user: + raise serializers.ValidationError( + "You can only add sessions to your own courses" + ) + return course + + class CollectionSerializer(serializers.ModelSerializer): shared = serializers.BooleanField(required=False) courses_data = serializers.ListField(write_only=True) diff --git a/api/urls.py b/api/urls.py index 12a64ae..c44ff63 100644 --- a/api/urls.py +++ b/api/urls.py @@ -19,6 +19,7 @@ ), path("schedules", views.ScheduleListView.as_view(), name="schedule-list"), path("schedule/", views.ScheduleView.as_view(), name="schedule-detail"), + path("sessions", views.SessionView.as_view(), name="session-list"), path("session/", views.SessionView.as_view(), name="session-detail"), path("schedule_selector", views.ScheduleSelector.as_view()), path("courses", views.CourseView.as_view(), name="course-list"), diff --git a/api/views.py b/api/views.py index c23cce5..a86e88e 100644 --- a/api/views.py +++ b/api/views.py @@ -134,6 +134,7 @@ def get(self, request): for course in courses: result.append( { + "id": course.id, "name": course.name, "schedules_url": reverse( "course_schedules-list", @@ -223,7 +224,7 @@ def perform_create(self, serializer): Session.objects.create(course=schedule.course, date=date, status="present") -class SessionView(generics.RetrieveUpdateDestroyAPIView): +class SessionView(generics.RetrieveUpdateDestroyAPIView, generics.CreateAPIView): permissions = [permissions.IsAuthenticated] serializer_class = SessionSerializer @@ -231,6 +232,10 @@ def get_queryset(self): collection = get_object_or_404(Collection, user=self.request.user) return Session.objects.filter(course__collection=collection) + def perform_create(self, serializer): + status = serializer.validated_data.get("status", "present") + serializer.save(status=status) + class DateQuery(APIView): permissions = [permissions.IsAuthenticated]