Skip to content

Commit

Permalink
add filtering on product and end time
Browse files Browse the repository at this point in the history
  • Loading branch information
rm03 committed Nov 2, 2023
1 parent 7014b93 commit 4788272
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/announcements/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

app_name = "announcements"
router = routers.SimpleRouter()
router.register("", AnnouncementsViewSet)
router.register("", AnnouncementsViewSet, basename="announcements")
urlpatterns = router.urls
19 changes: 18 additions & 1 deletion backend/announcements/views.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 4788272

Please sign in to comment.