Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Filter on latest record of an object #364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/objects/api/v1/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class ObjectRecordFilterSet(FilterSet):
)
% {"operator_choices": display_choice_values_for_help_text(Operators)},
)
latest_record = filters.BooleanFilter(
method="filter_on_latest_record",
help_text=_("Search only in the latest record."),
)

class Meta:
model = ObjectRecord
Expand Down Expand Up @@ -113,3 +117,6 @@ def filter_date(self, queryset, name, value: date):

def filter_registration_date(self, queryset, name, value: date):
return queryset.filter_for_registration_date(value)

def filter_on_latest_record(self, queryset, name, value: bool):
return queryset.filter_on_latest_record(value)
7 changes: 7 additions & 0 deletions src/objects/api/v2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class ObjectRecordFilterSet(FilterSet):
method="filter_data_icontains",
help_text=_("Search in all `data` values of string properties."),
)
latest_record = filters.BooleanFilter(
method="filter_on_latest_record",
help_text=_("Search only in the latest record."),
)

class Meta:
model = ObjectRecord
Expand Down Expand Up @@ -128,3 +132,6 @@ def filter_date(self, queryset, name, value: date_):

def filter_registration_date(self, queryset, name, value: date_):
return queryset.filter_for_registration_date(value)

def filter_on_latest_record(self, queryset, name, value: bool):
return queryset.filter_on_latest_record(value)
10 changes: 10 additions & 0 deletions src/objects/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ def filter_for_registration_date(self, date):
perspective.
"""
return self.filter(registration_at__lte=date)

def filter_on_latest_record(self, value: bool):
"""
Filter only on data in latest record if True.

"""
if not value:
return self
else:
return self.filter(corrected__isnull=True)
81 changes: 81 additions & 0 deletions src/objects/tests/v2/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,24 @@ def test_filter_exclude_old_records(self):
data = response.json()["results"]
self.assertEqual(len(data), 0)

def test_filter_exclude_old_records_same_day_regression_test(self):
record_old = ObjectRecordFactory.create(
data={"diameter": 45},
object__object_type=self.object_type,
start_at=date.today() - timedelta(days=10),
end_at=date.today(),
)
record_new = ObjectRecordFactory.create(
data={"diameter": 50}, object=record_old.object, start_at=record_old.end_at
)

response = self.client.get(self.url, {"data_attrs": "diameter__exact__45"})

self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]
self.assertEqual(len(data), 1)

def test_filter_date_field_gte(self):
record = ObjectRecordFactory.create(
data={"dateField": "2000-10-10"}, object__object_type=self.object_type
Expand Down Expand Up @@ -652,3 +670,66 @@ def test_filter_unkown_version(self):

data = response.json()["results"]
self.assertEqual(len(data), 0)


class FilterLatestVersionTests(TokenAuthMixin, APITestCase):
url = reverse_lazy("object-list")

@classmethod
def setUpTestData(cls):
super().setUpTestData()

cls.object_type = ObjectTypeFactory(service__api_root=OBJECT_TYPES_API)
PermissionFactory.create(
object_type=cls.object_type,
mode=PermissionModes.read_only,
token_auth=cls.token_auth,
)

def test_filter_latest_version_true(self):
record_old = ObjectRecordFactory.create(
data={"diameter": 45},
object__object_type=self.object_type,
start_at=date.today() - timedelta(days=10),
end_at=date.today(),
)
record_new = ObjectRecordFactory.create(
data={"diameter": 50},
object=record_old.object,
start_at=record_old.end_at,
)
record_new.correct = record_old
record_new.save()

response = self.client.get(
self.url, {"data_attrs": "diameter__exact__45", "latest_record": True}
)

self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]
self.assertEqual(len(data), 0)

def test_filter_latest_version_false(self):
record_old = ObjectRecordFactory.create(
data={"diameter": 45},
object__object_type=self.object_type,
start_at=date.today() - timedelta(days=10),
end_at=date.today(),
)
record_new = ObjectRecordFactory.create(
data={"diameter": 50},
object=record_old.object,
start_at=record_old.end_at,
)
record_new.correct = record_old
record_new.save()

response = self.client.get(
self.url, {"data_attrs": "diameter__exact__45", "latest_version": False}
)

self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]
self.assertEqual(len(data), 1)