Skip to content

Commit

Permalink
feat: added endpoint to add extra class
Browse files Browse the repository at this point in the history
  • Loading branch information
hrideshmg committed Aug 26, 2024
1 parent 3342a47 commit a633949
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
25 changes: 19 additions & 6 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
),
path("schedules", views.ScheduleListView.as_view(), name="schedule-list"),
path("schedule/<int:pk>", views.ScheduleView.as_view(), name="schedule-detail"),
path("sessions", views.SessionView.as_view(), name="session-list"),
path("session/<int:pk>", views.SessionView.as_view(), name="session-detail"),
path("schedule_selector", views.ScheduleSelector.as_view()),
path("courses", views.CourseView.as_view(), name="course-list"),
Expand Down
7 changes: 6 additions & 1 deletion api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -223,14 +224,18 @@ 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

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]
Expand Down

0 comments on commit a633949

Please sign in to comment.