Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (41 sloc) 1.65 KB
import functools
from django.views.generic.base import TemplateResponseMixin
def pjax(pjax_template=None):
def pjax_decorator(view):
def _view(request, *args, **kwargs):
resp = view(request, *args, **kwargs)
# this is lame. what else though?
# if not hasattr(resp, "is_rendered"):
# warnings.warn("@pjax used with non-template-response view")
# return resp
if request.META.get('HTTP_X_PJAX', False):
if pjax_template:
resp.template_name = pjax_template
resp.template_name = _pjaxify_template_var(resp.template_name)
return resp
return _view
return pjax_decorator
class PJAXResponseMixin(TemplateResponseMixin):
pjax_template_name = None
def get_template_names(self):
names = super(PJAXResponseMixin, self).get_template_names()
if self.request.META.get('HTTP_X_PJAX', False):
if self.pjax_template_name:
names = [self.pjax_template_name]
names = _pjaxify_template_var(names)
return names
def _pjaxify_template_var(template_var):
if isinstance(template_var, (list, tuple)):
template_var = type(template_var)(_pjaxify_template_name(name) for name in template_var)
elif isinstance(template_var, basestring):
template_var = _pjaxify_template_name(template_var)
return template_var
def _pjaxify_template_name(name):
if "." in name:
name = "%s-pjax.%s" % tuple(name.rsplit('.', 1))
name += "-pjax"
return name