Skip to content

Commit

Permalink
feat: added min year to newsletter response
Browse files Browse the repository at this point in the history
  • Loading branch information
rb-25 committed May 1, 2024
1 parent 68be5e7 commit bbd346e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from student_welfare_backend.core.models import Newsletter
from student_welfare_backend.core.api.serializers import NewsletterSerializer
from student_welfare_backend.customs.pagination import CustomPagination
from student_welfare_backend.customs.pagination import CustomPagination,NewsletterPagination
from student_welfare_backend.customs.permissions import IsDSW, IsADSW
from student_welfare_backend.customs.views import BaseBulkUploadView, BaseBulkDownloadView

Expand All @@ -25,7 +25,7 @@ class NewsletterViewSet(ReadOnlyModelViewSet):
permission_classes = []
queryset = Newsletter.objects.all()
serializer_class = NewsletterSerializer
pagination_class = CustomPagination
pagination_class = NewsletterPagination
filter_backends = [filters.SearchFilter, filters.OrderingFilter, DjangoFilterBackend]
filterset_fields = ["year", "month"]
search_fields = ["year", "month"]
Expand All @@ -38,7 +38,6 @@ def get_month_index(self, month_name):

def get_queryset(self):
queryset = super().get_queryset()

# Custom sorting logic: Convert month names to numeric values
queryset = queryset.annotate(
month_index=Case(
Expand All @@ -49,8 +48,7 @@ def get_queryset(self):

# Order queryset by year (descending) and month_index (descending)
queryset = queryset.order_by('-year', '-month_index')

return queryset
return (queryset)


class NewsletterAdminViewSet(ModelViewSet):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
from rest_framework.pagination import PageNumberPagination

from rest_framework.response import Response
from student_welfare_backend.core.models import Newsletter
from django.db.models import Min

class CustomPagination(PageNumberPagination):
page_size = 10
page_size_query_param = "size"
max_page_size = 1000

class NewsletterPagination(PageNumberPagination):
page_size = 10
page_size_query_param = "size"
max_page_size = 1000

def get_paginated_response(self, data):
min_year = Newsletter.objects.aggregate(Min('year')).get('year__min')
response = super().get_paginated_response(data)
return Response({
'min_year': min_year,
'count': self.page.paginator.count,
'next': self.get_next_link(),
'previous': self.get_previous_link(),
'results': data
})

0 comments on commit bbd346e

Please sign in to comment.