From 99baa220747b88b1375450c37b5b8bb479479db7 Mon Sep 17 00:00:00 2001 From: Josh Date: Fri, 26 Jan 2024 16:26:42 -0600 Subject: [PATCH 1/3] add `object_list_date_range` property --- src/django_twc_toolbox/paginator.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/django_twc_toolbox/paginator.py b/src/django_twc_toolbox/paginator.py index 81b55be..8da7962 100644 --- a/src/django_twc_toolbox/paginator.py +++ b/src/django_twc_toolbox/paginator.py @@ -64,15 +64,7 @@ def date_segments(self) -> list[tuple[datetime.datetime, datetime.datetime]]: elif not self.object_list: return [] - if isinstance(self.object_list, QuerySet): # type: ignore[misc] - first_obj = self.object_list.first() - last_obj = self.object_list.last() - else: - first_obj = self.object_list[0] - last_obj = self.object_list[-1] - - first_date = getattr(first_obj, self.date_field) - last_date = getattr(last_obj, self.date_field) + first_date, last_date = self.object_list_date_range segments = [] current_start_date = first_date @@ -188,6 +180,15 @@ def _is_chronological(self) -> bool: if self.count == 1: return True + first_date, last_date = self.object_list_date_range + + return first_date < last_date + + def _get_page(self, *args, **kwargs) -> DatePage: + return DatePage(*args, **kwargs) + + @cached_property + def object_list_date_range(self) -> tuple[datetime.datetime, datetime.datetime]: if isinstance(self.object_list, QuerySet): # type: ignore[misc] first_obj = self.object_list.first() last_obj = self.object_list.last() @@ -198,10 +199,7 @@ def _is_chronological(self) -> bool: first_date = getattr(first_obj, self.date_field) last_date = getattr(last_obj, self.date_field) - return first_date < last_date - - def _get_page(self, *args, **kwargs) -> DatePage: - return DatePage(*args, **kwargs) + return (first_date, last_date) @cached_property def num_pages(self) -> int: From 9fddd2241574824479dc86baa8a1a93b9819c320 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 29 Jan 2024 10:12:10 -0600 Subject: [PATCH 2/3] wip --- tests/test_paginator.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/test_paginator.py b/tests/test_paginator.py index fce2ab1..2b7d7b0 100644 --- a/tests/test_paginator.py +++ b/tests/test_paginator.py @@ -392,6 +392,44 @@ def test_paginator_explicit_date_range(self, objects): with pytest.warns(DeprecationWarning): DatePaginator(objects, "date", date_range=date_range) + @pytest.mark.parametrize( + "model_data_queryset", + [ + ModelClassParams(model_class=DateOrderableModel, number_of_days=90), + ModelClassParams(model_class=DateTimeOrderableModel, number_of_days=180), + ], + indirect=["model_data_queryset"], + ) + def test_object_list_date_range(self, objects): + paginator = DatePaginator(objects, "date", datetime.timedelta(days=10)) + + first_date = paginator.page(1).min_date + print("pagniator.page(1).min_date", paginator.page(1).min_date) + print("pagniator.page(1).max_date", paginator.page(1).max_date) + print("paginator.page(1).start_date", paginator.page(1).start_date) + print("paginator.page(1).end_date", paginator.page(1).end_date) + last_date = paginator.page(paginator.num_pages).max_date + print("pagniator.page(paginator.num_pages).min_date", paginator.page(paginator.num_pages).min_date) + print("pagniator.page(paginator.num_pages).max_date", paginator.page(paginator.num_pages).max_date) + print("paginator.page(paginator.num_pages).start_date", paginator.page(paginator.num_pages).start_date) + print("paginator.page(paginator.num_pages).end_date", paginator.page(paginator.num_pages).end_date) + + if isinstance(objects, QuerySet): # type: ignore[misc] + print("objects.first().date", objects.first()) + print("objects.last().date", objects.last()) + print("paginator.object_list.first().date", paginator.object_list.first()) + print("paginator.object_list.last().date", paginator.object_list.last()) + else: + print("objects[0].date", objects[0]) + print("objects[-1].date", objects[-1]) + print("paginator.object_list[0].date", paginator.object_list[0]) + print("paginator.object_list[-1].date", paginator.object_list[-1]) + print("paginator.object_list_date_range[0]", paginator.object_list_date_range[0]) + print("paginator.object_list_date_range[1]", paginator.object_list_date_range[1]) + print("first_date", first_date) + print("last_date", last_date) + + assert paginator.object_list_date_range == (first_date, last_date) class TestDatePaginatorInheritance: From 28f44a1e02900f8205ca1b7c484388c210e57d9c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:12:30 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_paginator.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/tests/test_paginator.py b/tests/test_paginator.py index 2b7d7b0..47614ab 100644 --- a/tests/test_paginator.py +++ b/tests/test_paginator.py @@ -392,6 +392,7 @@ def test_paginator_explicit_date_range(self, objects): with pytest.warns(DeprecationWarning): DatePaginator(objects, "date", date_range=date_range) + @pytest.mark.parametrize( "model_data_queryset", [ @@ -409,10 +410,22 @@ def test_object_list_date_range(self, objects): print("paginator.page(1).start_date", paginator.page(1).start_date) print("paginator.page(1).end_date", paginator.page(1).end_date) last_date = paginator.page(paginator.num_pages).max_date - print("pagniator.page(paginator.num_pages).min_date", paginator.page(paginator.num_pages).min_date) - print("pagniator.page(paginator.num_pages).max_date", paginator.page(paginator.num_pages).max_date) - print("paginator.page(paginator.num_pages).start_date", paginator.page(paginator.num_pages).start_date) - print("paginator.page(paginator.num_pages).end_date", paginator.page(paginator.num_pages).end_date) + print( + "pagniator.page(paginator.num_pages).min_date", + paginator.page(paginator.num_pages).min_date, + ) + print( + "pagniator.page(paginator.num_pages).max_date", + paginator.page(paginator.num_pages).max_date, + ) + print( + "paginator.page(paginator.num_pages).start_date", + paginator.page(paginator.num_pages).start_date, + ) + print( + "paginator.page(paginator.num_pages).end_date", + paginator.page(paginator.num_pages).end_date, + ) if isinstance(objects, QuerySet): # type: ignore[misc] print("objects.first().date", objects.first()) @@ -424,8 +437,12 @@ def test_object_list_date_range(self, objects): print("objects[-1].date", objects[-1]) print("paginator.object_list[0].date", paginator.object_list[0]) print("paginator.object_list[-1].date", paginator.object_list[-1]) - print("paginator.object_list_date_range[0]", paginator.object_list_date_range[0]) - print("paginator.object_list_date_range[1]", paginator.object_list_date_range[1]) + print( + "paginator.object_list_date_range[0]", paginator.object_list_date_range[0] + ) + print( + "paginator.object_list_date_range[1]", paginator.object_list_date_range[1] + ) print("first_date", first_date) print("last_date", last_date)