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

add object_list_date_range property #18

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
24 changes: 11 additions & 13 deletions src/django_twc_toolbox/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down
55 changes: 55 additions & 0 deletions tests/test_paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,61 @@ 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:
"""
Expand Down
Loading