From dab945b974b9e87104acb0edaefcce252410b97a Mon Sep 17 00:00:00 2001 From: Hironsan Date: Mon, 24 Jul 2023 15:06:47 +0900 Subject: [PATCH] Add endpoints for assignment --- backend/examples/serializers.py | 9 ++++++- backend/examples/urls.py | 3 +++ backend/examples/views/assignment.py | 35 ++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 backend/examples/views/assignment.py diff --git a/backend/examples/serializers.py b/backend/examples/serializers.py index eb6bf615..acdbcfef 100644 --- a/backend/examples/serializers.py +++ b/backend/examples/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from .models import Comment, Example, ExampleState +from .models import Assignment, Comment, Example, ExampleState class CommentSerializer(serializers.ModelSerializer): @@ -50,6 +50,13 @@ class ExampleSerializer(serializers.ModelSerializer): read_only_fields = ["filename", "is_confirmed", "upload_name"] +class AssignmentSerializer(serializers.ModelSerializer): + class Meta: + model = Assignment + fields = ("id", "assignee", "example", "created_at", "updated_at") + read_only_fields = ("id", "created_at", "updated_at") + + class ExampleStateSerializer(serializers.ModelSerializer): class Meta: model = ExampleState diff --git a/backend/examples/urls.py b/backend/examples/urls.py index 89d50561..bce25515 100644 --- a/backend/examples/urls.py +++ b/backend/examples/urls.py @@ -1,10 +1,13 @@ from django.urls import path +from .views.assignment import AssignmentDetail, AssignmentList from .views.comment import CommentDetail, CommentList from .views.example import ExampleDetail, ExampleList from .views.example_state import ExampleStateList urlpatterns = [ + path(route="assignments", view=AssignmentList.as_view(), name="assignment_list"), + path(route="assignments/", view=AssignmentDetail.as_view(), name="assignment_detail"), path(route="examples", view=ExampleList.as_view(), name="example_list"), path(route="examples/", view=ExampleDetail.as_view(), name="example_detail"), path(route="comments", view=CommentList.as_view(), name="comment_list"), diff --git a/backend/examples/views/assignment.py b/backend/examples/views/assignment.py new file mode 100644 index 00000000..51d58d1e --- /dev/null +++ b/backend/examples/views/assignment.py @@ -0,0 +1,35 @@ +from django.shortcuts import get_object_or_404 +from django_filters.rest_framework import DjangoFilterBackend +from rest_framework import filters, generics +from rest_framework.permissions import IsAuthenticated + +from examples.models import Assignment +from examples.serializers import AssignmentSerializer +from projects.models import Project +from projects.permissions import IsProjectAdmin, IsProjectStaffAndReadOnly + + +class AssignmentList(generics.ListCreateAPIView): + serializer_class = AssignmentSerializer + permission_classes = [IsAuthenticated & (IsProjectAdmin | IsProjectStaffAndReadOnly)] + filter_backends = (DjangoFilterBackend, filters.OrderingFilter) + ordering_fields = ("created_at", "updated_at") + model = Assignment + + @property + def project(self): + return get_object_or_404(Project, pk=self.kwargs["project_id"]) + + def get_queryset(self): + queryset = self.model.objects.filter(project=self.project, assignee=self.request.user) + return queryset + + def perform_create(self, serializer): + serializer.save(project=self.project) + + +class AssignmentDetail(generics.RetrieveUpdateDestroyAPIView): + queryset = Assignment.objects.all() + serializer_class = AssignmentSerializer + lookup_url_kwarg = "assignment_id" + permission_classes = [IsAuthenticated & (IsProjectAdmin | IsProjectStaffAndReadOnly)]