- Author:
- AgustinLado
- Posted:
- October 5, 2015
- Language:
- Python
- Version:
- 1.7
- Score:
- 1 (after 1 ratings)
What the docstring says. To not use some functionality, e.g. managing the value in the User's Profile model, delete the corresponding lines (when getting the page_size and when saving it.
Add the Mixin before the View class. e.g.: class ItemList(PaginationMixin, generic.ListView):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | class PaginationMixin(object):
"""
Get the paginate_by value from either (in order of priority):
- the querystring
- the session
- the User's profile in the database
- the default value
Limit the chosen paginate_by value to avoid abuse (for example,
setting it to 0 makes Django return all the items).
Save the value in the session so that the User doesn't have to specity it.
"""
DEFAULT_PAGE_SIZE = 10
MIN_PAGE_SIZE = 1
MAX_PAGE_SIZE = 50
def get_paginate_by(self, queryset):
# Get the page_size value
page_size = (int(self.request.GET.get('page_size', 0)) or
self.request.session.get('page_size') or
self.request.user.profile.page_size or
self.DEFAULT_PAGE_SIZE)
# Limit the page_size to lie between the set range
if page_size < self.MIN_PAGE_SIZE or page_size > self.MAX_PAGE_SIZE:
page_size = self.DEFAULT_PAGE_SIZE
# Save the page_size in the session
self.request.session['page_size'] = page_size
# Save the page_size in the User's Profile model, but only if it changed.
if page_size != self.request.user.profile.page_size:
self.request.user.profile.page_size = page_size
self.request.user.profile.save()
# Return the page_size
return page_size
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.