Testing the template_context_prerender hook
authorChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 15 May 2013 16:40:28 +0000 (11:40 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 15 May 2013 16:40:28 +0000 (11:40 -0500)
This allows for modifying any context *right before render*, including
access to the variables that are passed in.  This test takes advantage
of that and takes one of the variables, "doubleme", and modifies
it (doubles it!)

In our case it turns "happy" and "joy" into "happyhappy" and "joyjoy".

This commit sponsored by Mark Holmquist.  Thank you!

mediagoblin/tests/test_pluginapi.py
mediagoblin/tests/testplugins/modify_context/__init__.py
mediagoblin/tests/testplugins/modify_context/templates/contextplugin/general.html
mediagoblin/tests/testplugins/modify_context/templates/contextplugin/specific.html
mediagoblin/tests/testplugins/modify_context/views.py

index 74a00ce0b44c418598604118915c9a3f5a92d29d..73ad235ec5bff65d09d4ab9be1dfda32b4bfd2a2 100644 (file)
@@ -348,11 +348,13 @@ def test_modify_context(context_modified_app):
 
 specific thing: in yer specificpage
 global thing: globally appended!
-something: orother"""
+something: orother
+doubleme: happyhappy"""
 
     # General test, should have global context variable only
     result = context_modified_app.get("/modify_context/")
     assert result.body.strip() == """General page!
 
 global thing: globally appended!
-lol: cats"""
+lol: cats
+doubleme: joyjoy"""
index 6ddcd6528a3b2aa5f70572b1e398aafed9a18b42..164e66c1cf1b33aeff475ec8186d16e321016b82 100644 (file)
@@ -26,6 +26,11 @@ def append_to_global_context(context):
     context['global_append'] = 'globally appended!'
     return context
 
+def double_doubleme(context):
+    if 'doubleme' in context:
+        context['doubleme'] = context['doubleme'] * 2
+    return context
+
 
 def setup_plugin():
     routes = [
@@ -46,4 +51,5 @@ hooks = {
     'setup': setup_plugin,
     ('modify_context.specific_page',
      'contextplugin/specific.html'): append_to_specific_context,
-    'template_global_context': append_to_global_context}
+    'template_global_context': append_to_global_context,
+    'template_context_prerender': double_doubleme}
index 7b7261c643b20b2a90a08119756314ac98a7a4a9..9cf96d3e2fa3c1c9337deed95c87fe81fa2aac0b 100644 (file)
@@ -2,3 +2,4 @@ General page!
 
 global thing: {{ global_append }}
 lol: {{ lol }}
+doubleme: {{ doubleme }}
index 25bea3e2d4e3b853e1dcb9337d1c116cada1df8a..5b1b4c4a207f42517c14117aa6e264f2ef063c18 100644 (file)
@@ -3,3 +3,4 @@ Specific page!
 specific thing: {{ specific_page_append }}
 global thing: {{ global_append }}
 something: {{ something }}
+doubleme: {{ doubleme }}
index 025c84d6c4d39bf7644aa958470bcc494bd478f2..701ec6f9c451ca0833ad0178cd694dac2fb68f15 100644 (file)
@@ -21,11 +21,13 @@ def specific(request):
     return render_to_response(
         request,
         'contextplugin/specific.html',
-        {"something": "orother"})
+        {"something": "orother",
+         "doubleme": "happy"})
 
 
 def general(request):
     return render_to_response(
         request,
         'contextplugin/general.html',
-        {"lol": "cats"})
+        {"lol": "cats",
+         "doubleme": "joy"})