decorators.py 933 Bytes
# -*- coding: utf-8 -*-

from functools import wraps
from django.core.urlresolvers import reverse


def paginated(func):
    """Decorator to be used on view functions that use pagination"""

    @wraps(func)
    def new_func(request, *args, **kwargs):
        output = func(request, *args, **kwargs)
        if not isinstance(output, dict):
            return output
        paginer = output['paginer']
        js_vars = output.get('js_vars', {})
        js_vars['paginer'] = dict((id, (d['type'], d['params'], d['action'],
        d['group_actions']))
            for (id, d) in paginer.dict.iteritems())
        js_vars['original_paginer'] = js_vars['paginer']
        js_vars['ajax_paginer'] = reverse('get_page_content')
        js_vars['ajax_new_filter_row'] = reverse('new_filter_row')
        js_vars['ajax_dynamic_field'] = reverse('field_form')
        output['js_vars'] = js_vars
        return output

    return new_func