# -*- coding: utf-8 -*-

from django import template

register = template.Library()

@register.filter(name='slicepagination')
def slicepagination(value, page):
    limit = 5

    f = int(page) - limit
    if f < 0:
        f = 0

    t = int(page) + limit
    
    arg = str(f) + ":" + str(t)

    """
    Returns a slice of the list.

    Uses the same syntax as Python's list slicing; see
    http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
    for an introduction.
    """
    try:
        bits = []
        for x in arg.split(':'):
            if len(x) == 0:
                bits.append(None)
            else:
                bits.append(int(x))
        return value[slice(*bits)]

    except (ValueError, TypeError):
        return value # Fail silently.

