So the existing Paginator class is great for the standard use case.
But recently I wanted to be have a Paginator that would return *at least
per_page *x items. I called it FlexPaginator. My use case was that I had a
group_by value that I wanted to keep together on the same page, and so the
rows per page needed to grow. When it was all said an done, I had a
completely different looking page function on my FlexPaginator.
Here's the default one for reference:
def page(self, number):
"""
Returns a Page object for the given 1-based page number.
"""
number = self.validate_number(number)
bottom = (number - 1) * self.per_page
top = bottom + self.per_page
if top + self.orphans >= self.count:
top = self.count
return self._get_page(self.object_list[bottom:top], number, self)
I'd like to refactor several things about it that would have made it more
abstract and easier for me, but still keep it backwards compatible:
- rename bottom and top variables, instead self.first and self.last
respectively
- turn their inline calculations into function calls to a new,
self.first() and self.last()
- move the entire self.object_list[bottom:top] into its own line
- move that specific logic into a function called self.get_page_objects
- store that function's value into self.page_objects
Any comments?
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/b11166ec-0800-48f6-a647-62f41b67631c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.