From 47882728ee3ef524983f895694a9595ea87a20c0 Mon Sep 17 00:00:00 2001 From: Rohan Moniz Date: Thu, 2 Nov 2023 15:28:57 -0400 Subject: [PATCH] add filtering on product and end time --- backend/announcements/urls.py | 2 +- backend/announcements/views.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/backend/announcements/urls.py b/backend/announcements/urls.py index f78968c..321364d 100644 --- a/backend/announcements/urls.py +++ b/backend/announcements/urls.py @@ -4,5 +4,5 @@ app_name = "announcements" router = routers.SimpleRouter() -router.register("", AnnouncementsViewSet) +router.register("", AnnouncementsViewSet, basename="announcements") urlpatterns = router.urls diff --git a/backend/announcements/views.py b/backend/announcements/views.py index 9813e2d..6427307 100644 --- a/backend/announcements/views.py +++ b/backend/announcements/views.py @@ -1,14 +1,31 @@ from announcements.models import Announcement from announcements.permissions import IsSuperuser from announcements.serializers import AnnouncementSerializer +from django.db.models import Q +from django.utils import timezone from rest_framework import viewsets class AnnouncementsViewSet(viewsets.ModelViewSet): serializer_class = AnnouncementSerializer - queryset = Announcement.objects.all() def get_permissions(self): if self.request.method != "GET": self.permission_classes = [IsSuperuser] return super(AnnouncementsViewSet, self).get_permissions() + + def get_queryset(self): + queryset = Announcement.objects.all() + active = self.request.query_params.get("active") + audiences = self.request.query_params.get("audience") + + if active == "true": + queryset = queryset.filter(Q(end_time__gte=timezone.now()) | Q(end_time__isnull=True)) + + if audiences: + audience_names = audiences.split(",") + queryset = queryset.filter( + audiences__name__in=[name.strip().upper() for name in audience_names] + ) + + return queryset.distinct()