You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

35 lines
1.3 KiB

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)]