-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add filtering on product and end time
- Loading branch information
Showing
2 changed files
with
19 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |