offline avatar offline committed 225d77a

initial

Comments (0)

Files changed (20)

+.*\.pyc
+Anderson <self.anderson@gmail.com>
+Installation instructions:
+
+* Copy simplepagination directory to your django project or put in on PYTHONPATH.
+* Add 'simplepagination' in settings.py file to INSTALLED_APPS tuple
+* Copy paginator.css to you static files folder and add it to base html template
+
+That's it.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+    * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+recursive-include simplepagination/templates *
+Django Simple Paginator Application
+========================
+
+This is django application with multiple type of pagination integrated
+
+For installation instructions see the file "INSTALL.txt" in this
+directory. You need last development version of django to use this application.
+a.digg_nextprev {
+    display: block;
+    border:1px solid #9AAFE5;
+    color:#999999;
+    float:left;
+    padding: 5px 5px;
+    background:#FFFFFF none repeat scroll 0 0;
+    text-decoration: none;
+    color:#105CB6;
+    font: 11px arial;
+    margin-right:0.1em;
+    font-weight:bold;
+}
+
+a.digg_nextprev:hover {
+    border-color:#2E6AB1;
+    color:#000033;
+}
+
+span.digg_nextprev_disabled {
+    display: block;
+    border:1px solid #DDDDDD;
+    color:#999999;
+    float:left;
+    padding: 5px 5px;
+    background:#FFFFFF none repeat scroll 0 0;
+    text-decoration: none;
+    font: 11px arial;
+    margin-right:0.1em;
+}
+
+a.digg_pages {
+    float: left;
+    border:1px solid #9AAFE5;
+    text-decoration:none;
+    background:#FFFFFF none repeat scroll 0 0;
+    display:block;
+    padding: 5px 5px;
+    margin-right:0.1em;
+    font: 11px arial;
+    color:#105CB6;
+}
+
+a.digg_pages:hover {
+    border-color:#2E6AB1;
+    color:#000033;
+}
+
+a.digg_pages:focus {
+    color:#000033;
+}
+
+span.digg_pages_disabled {
+    float: left;
+    background:#2E6AB1 none repeat scroll 0 0;
+    border:1px solid #9AAFE5;
+    text-decoration:none;
+    display:block;
+    padding: 5px 8px;
+    margin-right:0.1em;
+    font: 11px arial;
+    color:#FFFFFF;
+    font-weight:bold;
+}
+
+span.digg_seperator {
+    display: block;
+    float: left;
+    font-size: 8px;
+    padding: 10px 5px 0px 5px;
+}
+
+
+a.filmfeed_pages {
+    border: 1px solid #2B2B2B;
+    background-color: #000000;
+    color: #808080;
+    display: block;
+    float: left;
+    font-size: 20px;
+    margin-bottom: 0.5em;
+    margin-right: 0.3em;
+    margin-top: 0.15em;
+    padding: 0.1em 0.5em 0.1em 0.4em;
+}
+
+
+span.filmfeed_pages_disabled {
+    background-color:#3B3B3B;
+    border-color:#3B3B3B;
+    color:#000000;
+    border:1px solid #2B2B2B;
+    display:block;
+    float:left;
+    font-size:20px;
+    margin-bottom:0.5em;
+    margin-right:0.3em;
+    margin-top:0.15em;
+    padding:0.1em 0.5em 0.1em 0.4em;
+}
+
+
+#muzx_paginator {
+    overflow: hidden;
+}
+
+#muzx_paginator_left {
+    float: left;
+}
+
+#muzx_paginator_left a.muzx_pages_current {
+   background-color:#CDDF00; 
+   padding:4px;
+   color:#000000;
+}
+
+#muzx_paginator_left a.muzx_pages {
+    color:#000000;
+}
+
+#muzx_paginator_right {
+    float: right;
+}
+from setuptools import setup, find_packages
+
+
+setup(
+    name = "django-simplepagination",
+    version = "0.0.1",
+    packages = find_packages(),
+    author = "Anderson",
+    author_email = "self.anderson@gmail.com",
+    description = "Django application with multiple type of pagination integrated",
+    license = "BSD",
+    keywords = "django",
+    url = "http://bitbucket.org/offline/django-simplepagination/wiki/",
+    install_requires = ["django-annoying"],
+    include_package_data = True,
+    zip_safe = False
+)
+

simplepagination/__init__.py

+# coding: utf-8
+
+from functools import partial, update_wrapper
+
+from django.utils.datastructures import MultiValueDictKeyError
+from django.core.paginator import Paginator, EmptyPage
+from django.http import Http404
+
+from . import settings
+from .utils import unicode_urlencode, get_instance_from_path
+
+
+class SimplePaginator(object):
+    """
+    Class based decorator.
+
+    SimplePagination decorator must be used along with 'render_to' 
+    decorator from django-annoying application
+    http://bitbucket.org/offline/django-annoying/wiki/Home
+    """
+
+    def __init__(self, object_list_item_name='object_list', style=None, per_page=10, 
+                frame_size=8, allow_user_per_page=False, fixed_user_per_page=None, 
+                template=None, anchor=None):
+        """
+        Decorator parameters 
+
+        object_list_item_name - Name of the variable with objects that we paginate.
+        style - name of pagination backend.
+        per_page - number of objects to show on page.
+        frame_size - max pages numbers to show.
+        """
+
+        self.object_list_item_name = object_list_item_name
+        style = style or getattr(settings, "PAGINATION_STYLE", "digg")
+        self.backend = get_instance_from_path(settings.PAGINATION_BACKENDS[style])
+        self.per_page = per_page
+        self.frame_size = frame_size
+        self.allow_user_per_page = allow_user_per_page
+        self.template = template or 'paginator_%s.html' % style
+        self.fixed_user_per_page = fixed_user_per_page
+        self.anchor = anchor
+        
+    def __call__(self, function):
+        """
+        Receive decorated function and return
+        function decorated with decorate method
+        """
+        decorated = partial(self.decorate, function)
+        return update_wrapper(decorated, self.decorate)
+    
+    def decorate(self, function, request, *args, **kwargs):
+
+        # execute view 
+        output = function(request, *args, **kwargs)
+        
+        # only try to paginate if view returned dictionary,
+        # in all other cases just return view output.
+        if not isinstance(output, dict):
+            return output
+        
+        params = request.GET.copy()
+        
+        try:
+            page_num = int(params.pop('page')[0])
+        except (ValueError, KeyError):
+            page_num = 1
+        
+        per_page = self.per_page
+        if self.allow_user_per_page and 'per_page' in params:
+            try:
+                user_per_page = int(params['per_page'])
+                if not self.fixed_user_per_page or user_per_page not in self.fixed_user_per_page:
+                    per_page = user_per_page
+            except (ValueError, KeyError):
+                params['per_page'] = self.per_page
+        elif 'per_page' in params:
+            params.pop('per_page')
+        
+        try:
+            paginate_qs = output.pop(self.object_list_item_name)
+        except KeyError:
+            raise KeyError("Variable with name '%s', not found in view returned dictionary" % self.object_list_item_name)
+        paginator = Paginator(paginate_qs, per_page)
+        
+        try:
+            page = paginator.page(page_num)
+        except EmptyPage:
+            raise Http404()
+        
+        data = {}
+        data['page_num'] = page_num # active page number
+        data['per_page'] = per_page # items per page
+        data['params'] = unicode_urlencode(params) # get parameters
+        data['anchor'] = self.anchor # ancor
+        data['pages'] = pages = paginator.num_pages # number of pages
+        data['paginator_template'] = self.template
+        data['allow_user_per_page'] = self.allow_user_per_page
+
+        output[self.object_list_item_name] = page.object_list
+        data.update(self.backend.paginate(self.frame_size, pages, page_num))
+        output['paginator'] = data
+        
+        return output
+
+    
+paginate = SimplePaginator
Add a comment to this file

simplepagination/backends/__init__.py

Empty file added.

simplepagination/backends/digg.py

+class DiggPaginator(object):
+
+    def paginate(self, frame_size, pages, page_num):
+        output = {}
+        if page_num > 1:
+            output['PREVIOUS'] = page_num -1
+        if page_num < pages:
+            output['NEXT'] = page_num + 1
+        if pages > frame_size and pages <= frame_size +2:
+            output['left_page_numbers'] = range(1, pages + 1)
+        elif pages <= frame_size:
+            output['left_page_numbers'] = range(1, pages + 1)
+        elif pages > frame_size and page_num < frame_size - 1:
+            output['left_page_numbers'] = range(1, frame_size + 1)
+            output['right_page_numbers'] = range(pages -1, pages +1)
+        elif pages > frame_size and page_num > frame_size - 2 and pages - (frame_size / 2) <= page_num + 1:
+            output['left_page_numbers'] = range(1, 3)
+            output['middle_page_numbers'] = range(pages - frame_size + 1, pages +1)
+        elif pages > frame_size and page_num > frame_size - 2:
+            output['left_page_numbers'] = range(1, 3)
+            output['middle_page_numbers'] = range(page_num - (frame_size / 2) +1, page_num + (frame_size / 2))
+            output['right_page_numbers'] = range(pages -1, pages +1)
+        return output
+

simplepagination/backends/filmfeed.py

+class FilmfeedPaginator(object):
+    def paginate(self, frame_size, pages, page_num):
+        output = {}
+        if pages < frame_size:
+            output['page_numbers'] = range(1, pages + 1)
+        elif page_num < (frame_size / 2) + 1:
+            output['page_numbers'] = range(1, frame_size + 1)
+        elif page_num >= (frame_size / 2) + 1 and pages - (frame_size / 2) <= page_num:
+            output['page_numbers'] = range(pages - frame_size + 1, pages + 1)
+        elif page_num >= (frame_size / 2) + 1:
+            start = page_num - (frame_size / 2)
+            end = page_num + (frame_size / 2)
+            output['page_numbers'] = range(start, end + 1)
+        return output
+

simplepagination/settings.py

+from django.conf import settings
+
+
+PAGINATION_BACKENDS = getattr(settings, 'PAGINATION_BACKENDS', {
+    'digg': 'simplepagination.backends.digg.DiggPaginator',
+    'filmfeed': 'simplepagination.backends.filmfeed.FilmfeedPaginator',
+})
+
+PAGINATION_STYLE = getattr(settings, 'PAGINATION_STYLE', 'digg')

simplepagination/templates/paginator.html

+{% load paginator %}
+
+{% include paginator_template %}

simplepagination/templates/paginator_digg.html

+{% load paginator %}
+
+{% if paginator.PREVIOUS %}
+  <a href='{% page_link paginator.PREVIOUS paginator.params paginator.anchor %}' class="digg_nextprev">&lt;&lt; Previous</a>
+{% else %}
+  <span class="digg_nextprev_disabled">&lt;&lt; Previous</span>
+{% endif %}
+
+{% if paginator.left_page_numbers %}
+  {% for i in paginator.left_page_numbers %}
+    {% ifequal i paginator.page_num %}
+      <span class="digg_pages_disabled">{{ i }}</span>
+    {% else %}
+      <a class="digg_pages" href='{% page_link i paginator.params paginator.anchor %}'>{{ i }}</a>
+    {% endifequal %}
+  {% endfor %}
+{% endif %}
+
+{% if paginator.middle_page_numbers %}
+  <span class="digg_seperator">...</span>
+  {% for i in paginator.middle_page_numbers %}
+    {% ifequal i paginator.page_num %}
+      <span class="digg_pages_disabled">{{ i }}</span>
+    {% else %}
+      <a class="digg_pages" href='{% page_link i paginator.params paginator.anchor %}'>{{ i }}</a>
+    {% endifequal %}
+  {% endfor %}
+{% endif %}
+
+{% if paginator.right_page_numbers %}
+  <span class="digg_seperator">...</span>
+    {% for i in paginator.right_page_numbers %}
+      {% ifequal i paginator.page_num %}
+        <span class="digg_pages_disabled">{{ i }}</span>
+      {% else %}
+        <a class="digg_pages" href='{% page_link i paginator.params paginator.anchor %}'>{{ i }}</a>
+      {% endifequal %}
+  {% endfor %}
+{% endif %}
+
+{% if paginator.NEXT %}
+  <a href='{% page_link paginator.NEXT paginator.params paginator.anchor %}' class="digg_nextprev">Next &gt;&gt;</a>
+{% else %}
+  <span class="digg_nextprev_disabled">Next &gt;&gt;</span>
+{% endif %}

simplepagination/templates/paginator_filmfeed.html

+{% load paginator %}
+
+{% for i in page_numbers %}
+    {% ifequal i page_num %}
+        <span class="filmfeed_pages_disabled">{{ i }}</span>
+    {% else %}
+        <a href='{% page_link i params anchor %}' class="filmfeed_pages">{{ i }}</a>
+    {% endifequal %}
+{% endfor %}

simplepagination/templates/paginator_muzx.html

+{% load paginator %}
+
+<div id="muzx_paginator">
+  <div id="muzx_paginator_left">
+    {% for i in page_numbers %}
+      {% ifequal i page_num %}
+        <a href='{% page_link i params anchor %}' class="muzx_pages_current">{{ i }}</a>
+      {% else %}
+        <a href='{% page_link i params anchor %}' class="muzx_pages">{{ i }}</a>
+      {% endifequal %}
+    {% endfor %}
+  </div>
+  {% if allow_user_per_page %}
+    <div id="muzx_paginator_right">
+      <select>
+        <option value="?per_page=10">10</option>
+        <option value="?per_page=20">20</option>
+        <option value="?per_page=50">50</option>
+      </select>
+    </div>
+  {% endif %}
+</div>
Add a comment to this file

simplepagination/templatetags/__init__.py

Empty file added.

simplepagination/templatetags/paginator.py

+# -*- coding: utf-8 -*-
+
+from django.template import Library
+
+register = Library()
+
+
+@register.simple_tag
+def page_link(num_page, params, anchor):
+    link = '?page=%s' % num_page
+    if params:
+        link = '%s&amp;%s' % (link, params.replace('&', '&amp;'))
+    if anchor:
+        link += '#%s' % anchor
+    return link
+
+
+@register.inclusion_tag('paginator.html')
+def paginate(obj):
+    return obj

simplepagination/utils.py

+from urllib import urlencode
+
+from django.core.exceptions import ImproperlyConfigured
+from django.utils.importlib import import_module
+
+
+def unicode_urlencode(params):
+    """
+    A unicode aware version of urllib.urlencode
+    """
+
+    if isinstance(params, dict):
+        params = params.items()
+    return urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v) for k, v in params])
+
+
+def get_instance_from_path(path, *args, **kwargs):
+    """
+    Return an instance of a class, given the dotted
+    Python import path (as a string) to the backend class.
+
+    If the backend cannot be located (e.g., because no such module
+    exists, or because the module does not contain a class of the
+    appropriate name), ``django.core.exceptions.ImproperlyConfigured``
+    is raised.
+
+    """
+    i = path.rfind('.')
+    module, attr = path[:i], path[i+1:]
+    try:
+        mod = import_module(module)
+    except ImportError, e:
+        raise ImproperlyConfigured('Error loading registration backend %s: "%s"' % (module, e))
+    try:
+        backend_class = getattr(mod, attr)
+    except AttributeError:
+        raise ImproperlyConfigured('Module "%s" does not define a registration backend named "%s"' % (module, attr))
+
+    return backend_class(*args, **kwargs)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.