Browse Source

Add endpoints for assignment

pull/2261/head
Hironsan 1 year ago
parent
commit
dab945b974
3 changed files with 46 additions and 1 deletions
  1. 9
      backend/examples/serializers.py
  2. 3
      backend/examples/urls.py
  3. 35
      backend/examples/views/assignment.py

9
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

3
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/<uuid:assignment_id>", view=AssignmentDetail.as_view(), name="assignment_detail"),
path(route="examples", view=ExampleList.as_view(), name="example_list"),
path(route="examples/<int:example_id>", view=ExampleDetail.as_view(), name="example_detail"),
path(route="comments", view=CommentList.as_view(), name="comment_list"),

35
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)]
Loading…
Cancel
Save