Sfoglia il codice sorgente

extract get_template method to allow overriding template on instances of blocks

to allow for customization and make the Block type mirror how the Page model allows for a get_template method to override it.

add per contrib guidelines
Chris Bledsoe 7 anni fa
parent
commit
3d945d0255

+ 1 - 0
CHANGELOG.txt

@@ -36,6 +36,7 @@ Changelog
  * FormBuilder class now uses bound methods for field generation, adding custom fields is now easier and documented (LB (Ben) Johnston)
  * Added `WAGTAILADMIN_NOTIFICATION_INCLUDE_SUPERUSERS` setting to determine whether superusers are included in moderation email notifications (Bruno Alla)
  * Added a basic Dockerfile to the project template (Tom Dyson)
+ * StreamField blocks now allow custom `get_template` methods for overriding templates in instances (Christopher Bledsoe)
  * Fix: Do not remove stopwords when generating slugs from non-ASCII titles, to avoid issues with incorrect word boundaries (Sævar Öfjörð Magnússon)
  * Fix: The PostgreSQL search backend now preserves ordering of the `QuerySet` when searching with `order_by_relevance=False` (Bertrand Bordage)
  * Fix: Using `modeladmin_register` as a decorator no longer replaces the decorated class with `None` (Tim Heap)

+ 1 - 0
CONTRIBUTORS.rst

@@ -267,6 +267,7 @@ Contributors
 * Philipp Bosch
 * misraX
 * Bruno Alla
+* Christopher Bledsoe (The Motley Fool)
 
 Translators
 ===========

+ 1 - 0
docs/releases/2.0.rst

@@ -53,6 +53,7 @@ Other features
  * FormBuilder class now uses bound methods for field generation, adding custom fields is now easier and documented (LB (Ben Johnston))
  * Added ``WAGTAILADMIN_NOTIFICATION_INCLUDE_SUPERUSERS`` setting to determine whether superusers are included in moderation email notifications (Bruno Alla)
  * Added a basic Dockerfile to the project template (Tom Dyson)
+ * StreamField blocks now allow custom ``get_template`` methods for overriding templates in instances (Christopher Bledsoe)
 
 
 Bug fixes

+ 8 - 1
wagtail/core/blocks/base.py

@@ -221,13 +221,20 @@ class Block(metaclass=BaseBlock):
         })
         return context
 
+    def get_template(self, context=None):
+        """
+        Return the template to use for rendering the block if specified on meta class.
+        This extraction was added to make dynamic templates possible if you override this method
+        """
+        return getattr(self.meta, 'template', None)
+
     def render(self, value, context=None):
         """
         Return a text rendering of 'value', suitable for display on templates. By default, this will
         use a template (with the passed context, supplemented by the result of get_context) if a
         'template' property is specified on the block, and fall back on render_basic otherwise.
         """
-        template = getattr(self.meta, 'template', None)
+        template = self.get_template(context=context)
         if not template:
             return self.render_basic(value, context=context)
 

+ 16 - 0
wagtail/core/tests/test_blocks.py

@@ -3143,3 +3143,19 @@ class TestIncludeBlockTag(TestCase):
             'language': 'fr',
         })
         self.assertIn('<body><h1 class="important">bonjour</h1></body>', result)
+
+
+class BlockUsingGetTemplateMethod(blocks.Block):
+
+    my_new_template = "my_super_awesome_dynamic_template.html"
+
+    def get_template(self):
+        return self.my_new_template
+
+
+class TestOverriddenGetTemplateBlockTag(TestCase):
+    def test_template_is_overriden_by_get_template(self):
+
+        block = BlockUsingGetTemplateMethod(template='tests/blocks/this_shouldnt_be_used.html')
+        template = block.get_template()
+        self.assertEquals(template, block.my_new_template)