diff --git a/README.md b/README.md new file mode 100644 index 0000000..36b30ee --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +django-pagination +================= + +A set of utilities for creating robust pagination tools throughout a django application. + +This modification includes paginate_by tag for multi choice objects per page. + +Installation: +================= +In 'settings.py' add following lines (if they aren't already added): + +```python +TEMPLATE_CONTEXT_PROCESSORS = ( + "django.contrib.auth.context_processors.auth", + "django.core.context_processors.media", + "django.core.context_processors.request", + "django.contrib.messages.context_processors.messages", + "django.core.context_processors.static", +) +``` + +In the MIDDLEWARE_CLASSES add ```'pagination.middleware.PaginationMiddleware'``` + +And in the INSTALLED_APPS add ```'pagination'``` + + +Example: +================= +```{% perpageselect 10 20 30 %}``` will show dropdown list with choices 10, 20 and 30 objects per page. +```{% perpageanchors 10 20 30 %}``` will show anchors with choices 10, 20 and 30 objects per page. + + +Usage: +================= +``` +{% load pagination_tags %} + + {% autopaginate object_list 10 %} + {% for post in object_list %} + bla-bla-bla + {% endfor %} + {% paginate %} +``` diff --git a/pagination/locale/uk/LC_MESSAGES/django.mo b/pagination/locale/uk/LC_MESSAGES/django.mo new file mode 100644 index 0000000..5ec6cd5 Binary files /dev/null and b/pagination/locale/uk/LC_MESSAGES/django.mo differ diff --git a/pagination/locale/uk/LC_MESSAGES/django.po b/pagination/locale/uk/LC_MESSAGES/django.po new file mode 100644 index 0000000..8cdcca7 --- /dev/null +++ b/pagination/locale/uk/LC_MESSAGES/django.po @@ -0,0 +1,25 @@ +# django-pagination Ukrainian translation. +# Copyright (C) 2012, Maxym Lynnyk +# This file is distributed under the WTFPL +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2012-08-29 16:50+0200\n" +"PO-Revision-Date: 2012-08-29 16:50+0200\n" +"Last-Translator: Maxym Lynnyk \n" +"Language-Team: TIPS \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: templates/pagination/pagination.html:5 +#: templates/pagination/pagination.html:7 +msgid "previous" +msgstr "попередня" + +#: templates/pagination/pagination.html:21 +#: templates/pagination/pagination.html:23 +msgid "next" +msgstr "наступна" diff --git a/pagination/middleware.py b/pagination/middleware.py index f8a2a6f..979152c 100644 --- a/pagination/middleware.py +++ b/pagination/middleware.py @@ -1,3 +1,8 @@ +from django.conf import settings + +DEFAULT_PAGINATION = getattr(settings, 'PAGINATION_DEFAULT_PAGINATION', 20) + + def get_page(self): """ A function which will be monkeypatched onto the request to get the current @@ -7,11 +12,26 @@ def get_page(self): return int(self.REQUEST['page']) except (KeyError, ValueError, TypeError): return 1 + + +def get_perpage(self): + try: + self.session['perpage'] = int(self.REQUEST['perpage']) + return self.session['perpage'] + except (KeyError, ValueError, TypeError): + pass + try: + return int(self.session['perpage']) + except (KeyError, ValueError, TypeError): + return DEFAULT_PAGINATION + + class PaginationMiddleware(object): """ Inserts a variable representing the current page onto the request object if it exists in either **GET** or **POST** portions of the request. """ def process_request(self, request): - request.__class__.page = property(get_page) \ No newline at end of file + request.__class__.page = property(get_page) + request.__class__.perpage = property(get_perpage) \ No newline at end of file diff --git a/pagination/templates/pagination/perpageanchors.html b/pagination/templates/pagination/perpageanchors.html new file mode 100644 index 0000000..f2edc82 --- /dev/null +++ b/pagination/templates/pagination/perpageanchors.html @@ -0,0 +1,12 @@ +{% load i18n %} +
+ {% for choice in choices %} + + {% if perpage == choice %} + {% if choice == 0 %}{% trans "All" %}{% else %}{{choice}}{% endif %} + {% else %} + {% if choice == 0 %}{% trans "All" %}{% else %}{{choice}}{% endif %} + {% endif %} + + {% endfor %} +
diff --git a/pagination/templates/pagination/perpageselect.html b/pagination/templates/pagination/perpageselect.html new file mode 100644 index 0000000..950864a --- /dev/null +++ b/pagination/templates/pagination/perpageselect.html @@ -0,0 +1,11 @@ +{% load i18n %} +
+
+ + +
+
diff --git a/pagination/templatetags/pagination_tags.py b/pagination/templatetags/pagination_tags.py index ae843b1..d0b4770 100644 --- a/pagination/templatetags/pagination_tags.py +++ b/pagination/templatetags/pagination_tags.py @@ -5,7 +5,7 @@ from django import template from django.http import Http404 -from django.core.paginator import Paginator, InvalidPage +from django.core.paginator import Paginator, InvalidPage from django.conf import settings register = template.Library() @@ -51,7 +51,7 @@ def do_autopaginate(parser, token): else: raise template.TemplateSyntaxError('%r tag takes one required ' + 'argument and one optional argument' % split[0]) - + class AutoPaginateNode(template.Node): """ Emits the required objects to allow for Digg-style pagination. @@ -69,10 +69,10 @@ class AutoPaginateNode(template.Node): tag. If you choose not to use *{% paginate %}*, make sure to display the list of available pages, or else the application may seem to be buggy. """ - def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION, + def __init__(self, queryset_var, paginate_by=None, orphans=DEFAULT_ORPHANS, context_var=None): self.queryset_var = template.Variable(queryset_var) - if isinstance(paginate_by, int): + if (paginate_by == None): self.paginate_by = paginate_by else: self.paginate_by = template.Variable(paginate_by) @@ -82,10 +82,13 @@ def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION, def render(self, context): key = self.queryset_var.var value = self.queryset_var.resolve(context) - if isinstance(self.paginate_by, int): - paginate_by = self.paginate_by + if (self.paginate_by == None): + paginate_by = int(context['request'].perpage) else: paginate_by = self.paginate_by.resolve(context) + if (paginate_by == 0): + context['page_obj'] = value + return u'' paginator = Paginator(value, paginate_by, self.orphans) try: page_obj = paginator.page(context['request'].page) @@ -99,7 +102,7 @@ def render(self, context): if self.context_var is not None: context[self.context_var] = page_obj.object_list else: - context[key] = page_obj.object_list + context[key] = page_obj.object_list context['paginator'] = paginator context['page_obj'] = page_obj return u'' @@ -222,9 +225,35 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''): else: to_return['getvars'] = '' return to_return - except KeyError, AttributeError: + except (KeyError, AttributeError): return {} - register.inclusion_tag('pagination/pagination.html', takes_context=True)( paginate) register.tag('autopaginate', do_autopaginate) + + +@register.inclusion_tag('pagination/perpageselect.html', takes_context='True') +def perpageselect (context, *args): + """ + Reads the arguments to the perpageselect tag and formats them correctly. + """ + try: + choices = [int(x) for x in args] + perpage = int(context['request'].perpage) + return {'choices': choices, 'perpage': perpage} + except(TypeError, ValueError): + raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % args) + + +@register.inclusion_tag('pagination/perpageanchors.html', takes_context='True') +def perpageanchors (context, *args): + """ + Reads the arguments to the perpageanchors tag and formats them correctly. + """ + try: + choices = [int(x) for x in args] + perpage = int(context['request'].perpage) + return {'choices': choices, 'perpage': perpage} + except(TypeError, ValueError): + raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % args) + diff --git a/setup.py b/setup.py index 751d914..305b0a1 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -version = '1.0.7' +version = '1.0.8' LONG_DESCRIPTION = """ How to use django-pagination