"""View decorators for Twitter-style pagination."""

from __future__ import unicode_literals
from functools import wraps

from endless_pagination.settings import (
    PAGE_LABEL,
    TEMPLATE_VARNAME,
)


def page_template(template, key=PAGE_LABEL):
    """Return a view dynamically switching template if the request is Ajax.

    Decorate a view that takes a *template* and *extra_context* keyword
    arguments (like generic views).
    The template is switched to *page_template* if request is ajax and
    if *querystring_key* variable passed by the request equals to *key*.
    This allows multiple ajax paginations in the same page.
    The name of the page template is given as *page_template* in the
    extra context.
    """
    def decorator(view):
        @wraps(view)
        def decorated(request, *args, **kwargs):
            # Trust the developer: he wrote ``context.update(extra_context)``
            # in his view.
            extra_context = kwargs.setdefault('extra_context', {})
            extra_context['page_template'] = template
            # Switch template on ajax requests.
            querystring_key = request.REQUEST.get(
                'querystring_key', PAGE_LABEL)
            if request.is_ajax() and querystring_key == key:
                kwargs[TEMPLATE_VARNAME] = template
            return view(request, *args, **kwargs)
        return decorated

    return decorator


def _get_template(querystring_key, mapping):
    """Return the template corresponding to the given ``querystring_key``."""
    default = None
    try:
        template_and_keys = mapping.items()
    except AttributeError:
        template_and_keys = mapping
    for template, key in template_and_keys:
        if key is None:
            key = PAGE_LABEL
            default = template
        if key == querystring_key:
            return template
    return default


def page_templates(mapping):
    """Like the *page_template* decorator but manage multiple paginations.

    You can map multiple templates to *querystring_keys* using the *mapping*
    dict, e.g.::

        @page_templates({
            'page_contents1.html': None,
            'page_contents2.html': 'go_to_page',
        })
        def myview(request):
            ...

    When the value of the dict is None then the default *querystring_key*
    (defined in settings) is used. You can use this decorator instead of
    chaining multiple *page_template* calls.
    """
    def decorator(view):
        @wraps(view)
        def decorated(request, *args, **kwargs):
            # Trust the developer: he wrote ``context.update(extra_context)``
            # in his view.
            extra_context = kwargs.setdefault('extra_context', {})
            querystring_key = request.REQUEST.get(
                'querystring_key', PAGE_LABEL)
            template = _get_template(querystring_key, mapping)
            extra_context['page_template'] = template
            # Switch template on ajax requests.
            if request.is_ajax() and template:
                kwargs[TEMPLATE_VARNAME] = template
            return view(request, *args, **kwargs)
        return decorated

    return decorator

@@ -20,21 +20,20 @@
 for 
-Twitter-style
+Ajax powered
  pag
@@ -597,17 +597,17 @@
 ultiple 
-a
+A
 jax pagi
@@ -1062,32 +1062,36 @@
        # Switch 
+the 
 template on ajax
@@ -1083,32 +1083,40 @@
 emplate 
-on ajax requests
+when the request is Ajax
 .%0A      
@@ -2965,16 +2965,20 @@
  Switch 
+the 
 template
@@ -2982,24 +2982,32 @@
 ate 
-on ajax requests
+when the request is Ajax
 .%0A  

Fix decorators docstrings and comments.