diff --git a/src/objects/api/v2/filters.py b/src/objects/api/v2/filters.py index cccdc19a..881e8e6c 100644 --- a/src/objects/api/v2/filters.py +++ b/src/objects/api/v2/filters.py @@ -124,7 +124,9 @@ def filter_data_icontains(self, queryset, name, value: str): return queryset.extra(where=[where_str], params=[value]) def filter_date(self, queryset, name, value: date_): - return queryset.filter_for_date(value) + """actual filtering is performed in the viewset""" + return queryset def filter_registration_date(self, queryset, name, value: date_): - return queryset.filter_for_registration_date(value) + """actual filtering is performed in the viewset""" + return queryset diff --git a/src/objects/api/v2/views.py b/src/objects/api/v2/views.py index d2d24ff1..8c044ec8 100644 --- a/src/objects/api/v2/views.py +++ b/src/objects/api/v2/views.py @@ -2,6 +2,7 @@ from django.conf import settings from django.db import models +from django.utils.dateparse import parse_date from drf_spectacular.utils import extend_schema, extend_schema_view from rest_framework import mixins, viewsets @@ -24,6 +25,7 @@ ObjectSerializer, PermissionSerializer, ) +from ..utils import is_date from .filters import ObjectRecordFilterSet @@ -89,22 +91,30 @@ def get_queryset(self): # show only allowed objects base = base.filter_for_token(token_auth) - - # show only actual objects - date = getattr(self.request, "query_params", {}).get("date", None) - registration_date = getattr(self.request, "query_params", {}).get( - "registrationDate", None - ) - if not date and not registration_date: - base = base.filter_for_date(datetime.date.today()) - return base def filter_queryset(self, queryset): - queryset = super().filter_queryset(queryset) + # show only actual objects + if self.action in ("list", "search", "retrieve"): + date = getattr(self.request, "query_params", {}).get("date", None) + registration_date = getattr(self.request, "query_params", {}).get( + "registrationDate", None + ) + + if date and is_date(date): + queryset = queryset.filter_for_date(parse_date(date)) + elif registration_date and is_date(registration_date): + queryset = queryset.filter_for_registration_date( + parse_date(registration_date) + ) + else: + queryset = queryset.filter_for_date(datetime.date.today()) # keep only records with max index per object - return queryset.keep_max_record_per_object() + queryset = queryset.keep_max_record_per_object() + + # filter on the rest of query params + return super().filter_queryset(queryset) def perform_destroy(self, instance): instance.object.delete()