From 313a0ebf458dec3d0e1f39b598ccda5ee158fd18 Mon Sep 17 00:00:00 2001 From: Alex Hayes Date: Thu, 15 Mar 2012 00:33:18 +1100 Subject: [PATCH] Slight modification to pjaxtend method, now has defaults for all params. --- README.rst | 37 ++++++++++++++++++++++++++++++++----- djpjax.py | 6 +++--- tests.py | 23 +++++++++++++++++++++++ 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 15a7c3f..eb4134a 100644 --- a/README.rst +++ b/README.rst @@ -84,7 +84,7 @@ If the content in your ``template-pjax.html`` file is very similar to your from djpjax import pjaxtend - @pjaxtend('someapp/base.html') + @pjaxtend def my_view(request): return TemplateResponse(request, "template.html", {'my': 'context'}) @@ -94,11 +94,11 @@ Then, in your ``template.html`` file you can do the following:: ... ... -Note that the template will extend ``someapp/base.html`` unless its a pjax request +Note that the template will extend ``base.html`` unless its a pjax request in which case it will extend ``pjax.html``. -If you want to define which template to extend for a pjax request you can do so -as follows:: +If you want to define the parent for a standard http or pjax request, you can do +so as follows:: from djpjax import pjaxtend @@ -106,4 +106,31 @@ as follows:: def my_view(request): return TemplateResponse(request, "template.html", {'my': 'context'}) - Using this approach you don't need to create many ``*-pjax.html`` files. \ No newline at end of file +Using this approach you don't need to create many ``*-pjax.html`` files. + +If you have a collision with the variable name ``parent`` you can specify the +context variable to use as the third parameter to pjaxtexd, as follows:: + + from djpjax import pjaxtend + + @pjaxtend('someapp/base.html', 'my-pjax-extension.html', 'my_parent') + def my_view(request): + return TemplateResponse(request, "template.html", {'my': 'context'}) + +Which would require the following in your template: + + {% extends my_parent %} + ... + ... + + +Testing +------- + +Tests are run using nosetests. To install:: + + pip install nose + +And to run the tests:: + + nosetests tests.py \ No newline at end of file diff --git a/djpjax.py b/djpjax.py index bdefc07..6cc559c 100644 --- a/djpjax.py +++ b/djpjax.py @@ -20,7 +20,7 @@ def _view(request, *args, **kwargs): return _view return pjax_decorator -def pjaxtend(parent, pjax_parent='pjax.html'): +def pjaxtend(parent='base.html', pjax_parent='pjax.html', context_var='parent'): def pjaxtend_decorator(view): @functools.wraps(view) def _view(request, *args, **kwargs): @@ -30,9 +30,9 @@ def _view(request, *args, **kwargs): # warnings.warn("@pjax used with non-template-response view") # return resp if request.META.get('HTTP_X_PJAX', False): - resp.context_data['parent'] = pjax_parent + resp.context_data[context_var] = pjax_parent elif parent: - resp.context_data['parent'] = parent + resp.context_data[context_var] = parent return resp return _view return pjaxtend_decorator diff --git a/tests.py b/tests.py index 559e29f..6487ad0 100644 --- a/tests.py +++ b/tests.py @@ -58,6 +58,14 @@ def test_class_with_pjax_template(): resp = view(pjax_request) assert resp.template_name[0] == "pjax.html" +def test_pjaxtend_default(): + resp = view_default_pjaxtend(regular_request) + assert resp.template_name == "template.html" + assert resp.context_data['parent'] == "base.html" + resp = view_default_pjaxtend(pjax_request) + assert resp.template_name == "template.html" + assert resp.context_data['parent'] == "pjax.html" + def test_pjaxtend_default_parent(): resp = view_default_parent_pjaxtend(regular_request) assert resp.template_name == "template.html" @@ -74,6 +82,13 @@ def test_pjaxtend_custom_parent(): assert resp.template_name == "template.html" assert resp.context_data['parent'] == "parent-pjax.html" +def test_pjaxtend_custom_context(): + resp = view_custom_context_pjaxtend(regular_request) + assert resp.template_name == "template.html" + assert resp.context_data['my_parent'] == "parent.html" + resp = view_custom_context_pjaxtend(pjax_request) + assert resp.template_name == "template.html" + assert resp.context_data['my_parent'] == "parent-pjax.html" # The test "views" themselves. @@ -93,6 +108,10 @@ def view_with_pjax_template(request): def view_with_template_tuple(request): return TemplateResponse(request, ("template.html", "other_template.html"), {}) +@djpjax.pjaxtend() +def view_default_pjaxtend(request): + return TemplateResponse(request, "template.html", {}) + @djpjax.pjaxtend('parent.html') def view_default_parent_pjaxtend(request): return TemplateResponse(request, "template.html", {}) @@ -101,6 +120,10 @@ def view_default_parent_pjaxtend(request): def view_custom_parent_pjaxtend(request): return TemplateResponse(request, "template.html", {}) +@djpjax.pjaxtend('parent.html', 'parent-pjax.html', 'my_parent') +def view_custom_context_pjaxtend(request): + return TemplateResponse(request, "template.html", {}) + class NoPJAXTemplateVew(djpjax.PJAXResponseMixin, View): template_name = 'template.html'