Skip to content

Commit

Permalink
✨ [#324] change order of filtering of object records:
Browse files Browse the repository at this point in the history
  * filter on date,
  * group records by object and keep records with max index
  * filter on rest of query params
  • Loading branch information
annashamray committed Sep 6, 2024
1 parent 52b7027 commit 2c3ff52
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/objects/api/v2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 21 additions & 11 deletions src/objects/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +25,7 @@
ObjectSerializer,
PermissionSerializer,
)
from ..utils import is_date
from .filters import ObjectRecordFilterSet


Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2c3ff52

Please sign in to comment.