Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added endpoint to add extra class #11

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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