Browse Source

Merge pull request #271 from CatalystCode/enhancement/clean-up-querysets

Enhancement/Simplify queryset handling in list APIs
pull/284/head
Hiroki Nakayama 5 years ago
committed by GitHub
parent
commit
4cc7abf0c9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 6 deletions
  1. 9
      app/api/views.py

9
app/api/views.py

@ -42,7 +42,6 @@ class Features(APIView):
class ProjectList(generics.ListCreateAPIView):
queryset = Project.objects.all()
serializer_class = ProjectPolymorphicSerializer
pagination_class = None
permission_classes = (IsAuthenticated, IsAdminUserAndWriteOnly)
@ -109,14 +108,13 @@ class ApproveLabelsAPI(APIView):
class LabelList(generics.ListCreateAPIView):
queryset = Label.objects.all()
serializer_class = LabelSerializer
pagination_class = None
permission_classes = (IsAuthenticated, IsProjectUser, IsAdminUserAndWriteOnly)
def get_queryset(self):
queryset = self.queryset.filter(project=self.kwargs['project_id'])
return queryset
project = get_object_or_404(Project, pk=self.kwargs['project_id'])
return project.labels
def perform_create(self, serializer):
project = get_object_or_404(Project, pk=self.kwargs['project_id'])
@ -131,7 +129,6 @@ class LabelDetail(generics.RetrieveUpdateDestroyAPIView):
class DocumentList(generics.ListCreateAPIView):
queryset = Document.objects.all()
serializer_class = DocumentSerializer
filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
search_fields = ('text', )
@ -143,7 +140,7 @@ class DocumentList(generics.ListCreateAPIView):
def get_queryset(self):
project = get_object_or_404(Project, pk=self.kwargs['project_id'])
queryset = self.queryset.filter(project=project)
queryset = project.documents
if project.randomize_document_order:
queryset = queryset.annotate(sort_id=F('id') % self.request.user.id).order_by('sort_id')

Loading…
Cancel
Save