mirror of
https://github.com/lemeow125/DocManagerBackend.git
synced 2025-02-22 14:08:13 +08:00
Implement proper pagination
This commit is contained in:
parent
84ff7011ea
commit
cdd120f33d
1 changed files with 51 additions and 2 deletions
|
@ -10,6 +10,15 @@ from rest_framework.permissions import IsAuthenticated
|
|||
from rest_framework.pagination import PageNumberPagination
|
||||
from accounts.permissions import IsStaff, IsHead
|
||||
from .models import Document
|
||||
from django.db.models import Q
|
||||
from operator import or_
|
||||
from functools import reduce
|
||||
|
||||
|
||||
class DocumentPagination(PageNumberPagination):
|
||||
page_size = 4
|
||||
page_size_query_param = 'page_size'
|
||||
max_page_size = 4
|
||||
|
||||
|
||||
class DocumentUpdateView(generics.UpdateAPIView):
|
||||
|
@ -63,6 +72,46 @@ class DocumentStaffListView(generics.ListAPIView):
|
|||
|
||||
http_method_names = ["get"]
|
||||
serializer_class = DocumentFileSerializer
|
||||
queryset = Document.objects.all()
|
||||
pagination_class = PageNumberPagination
|
||||
queryset = Document.objects.all().order_by("-date_uploaded")
|
||||
pagination_class = DocumentPagination
|
||||
permission_classes = [IsAuthenticated, IsStaff]
|
||||
|
||||
def get_queryset(self):
|
||||
# Get the base queryset
|
||||
queryset = super().get_queryset()
|
||||
|
||||
# Check if 'keywords' query parameter is present
|
||||
keyword = self.request.query_params.get('search', None)
|
||||
|
||||
if keyword:
|
||||
queries = []
|
||||
# Create a list to hold individual field conditions for this keyword
|
||||
field_queries = [
|
||||
Q(name__icontains=keyword),
|
||||
Q(document_type__icontains=keyword),
|
||||
Q(sent_from__icontains=keyword),
|
||||
Q(document_month__icontains=keyword),
|
||||
Q(document_year__icontains=keyword),
|
||||
Q(subject__icontains=keyword),
|
||||
Q(ocr_metadata__icontains=keyword)
|
||||
]
|
||||
# Combine the field conditions with OR
|
||||
combined_query = Q()
|
||||
for q in field_queries:
|
||||
if not combined_query:
|
||||
combined_query = q
|
||||
else:
|
||||
combined_query |= q
|
||||
queries.append(combined_query)
|
||||
|
||||
# Now combine all keyword conditions with OR
|
||||
final_query = Q()
|
||||
for q in queries:
|
||||
if not final_query:
|
||||
final_query = q
|
||||
else:
|
||||
final_query |= q
|
||||
|
||||
queryset = queryset.filter(final_query)
|
||||
|
||||
return queryset
|
||||
|
|
Loading…
Reference in a new issue