From 967b731d1ea2cd32614d2f704c1eaa77847e00c5 Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Wed, 18 May 2016 10:28:09 -0700 Subject: [PATCH 1/2] Fix up template inclusion hooks. --- kolibri/core/templates/kolibri/base.html | 5 +++-- kolibri/core/webpack/hooks.py | 13 +++++++------ kolibri/plugins/example_plugin/kolibri_plugin.py | 6 ++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/kolibri/core/templates/kolibri/base.html b/kolibri/core/templates/kolibri/base.html index e32d184fd48..5f270bf1718 100644 --- a/kolibri/core/templates/kolibri/base.html +++ b/kolibri/core/templates/kolibri/base.html @@ -1,8 +1,9 @@ {% load i18n kolibri_tags webpack_tags %} {% block title %}{% endblock %} - {% trans "Kolibri" %} - {% webpack_assets 'default_frontend' %} - {% base_frontend_assets %} + {% webpack_asset 'default_frontend' %} + {% webpack_base_assets %} + {% webpack_base_async_assets %} diff --git a/kolibri/core/webpack/hooks.py b/kolibri/core/webpack/hooks.py index c4eda99752e..afe577d1f95 100644 --- a/kolibri/core/webpack/hooks.py +++ b/kolibri/core/webpack/hooks.py @@ -248,12 +248,13 @@ class WebpackInclusionHook(hooks.KolibriHook): def __init__(self, *args, **kwargs): super(WebpackInclusionHook, self).__init__(*args, **kwargs) - assert \ - self.bundle_class is not None,\ - "Must specify bundle property, this one did not: {} ({})".format( - type(self), - type(self.bundle) - ) + if not self._meta.abstract: + assert \ + self.bundle_class is not None,\ + "Must specify bundle_class property, this one did not: {} ({})".format( + type(self), + type(self.bundle_class) + ) def render_to_page_load_sync_html(self, extension=None): html = "" diff --git a/kolibri/plugins/example_plugin/kolibri_plugin.py b/kolibri/plugins/example_plugin/kolibri_plugin.py index 0277bebcfaa..2176944c7fc 100644 --- a/kolibri/plugins/example_plugin/kolibri_plugin.py +++ b/kolibri/plugins/example_plugin/kolibri_plugin.py @@ -69,8 +69,10 @@ class ExampleAsset(webpack_hooks.WebpackBundleHook): unique_slug = "example_plugin" src_file = "kolibri/plugins/example_plugin/assets/example/example_module.js" static_dir = "kolibri/plugins/example_plugin/static" + events = { + "hello": "hello_world" + } - -class ExampleInclusionHook(webpack_hooks.FrontEndBaseSyncHook): +class ExampleInclusionHook(webpack_hooks.FrontEndBaseASyncHook): bundle_class = ExampleAsset From cea717484eec940eba8f3daef0c21b1db8108d5e Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Wed, 18 May 2016 10:47:40 -0700 Subject: [PATCH 2/2] Make an abstract method for rendering template tag content to allow for other template tags to be created more easily. --- .../core/webpack/templatetags/webpack_tags.py | 15 +++------------ kolibri/core/webpack/utils.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/kolibri/core/webpack/templatetags/webpack_tags.py b/kolibri/core/webpack/templatetags/webpack_tags.py index bc14ae6a1e3..83069632c06 100644 --- a/kolibri/core/webpack/templatetags/webpack_tags.py +++ b/kolibri/core/webpack/templatetags/webpack_tags.py @@ -21,6 +21,7 @@ from django.utils.safestring import mark_safe from .. import hooks +from ..utils import webpack_asset_render register = template.Library() @@ -68,12 +69,7 @@ def webpack_base_assets(): :return: HTML of script tags to insert into base.html """ - tags = [] - for hook in hooks.FrontEndBaseSyncHook().registered_hooks: - tags.append( - hook.render_to_page_load_sync_html() - ) - return mark_safe('\n'.join(tags)) + return webpack_asset_render(hooks.FrontEndBaseSyncHook, async=False) @register.simple_tag() @@ -85,9 +81,4 @@ def webpack_base_async_assets(): :return: HTML of script tags to insert into base.html """ - tags = [] - for hook in hooks.FrontEndBaseASyncHook().registered_hooks: - tags.append( - hook.render_to_page_load_async_html() - ) - return mark_safe('\n'.join(tags)) + return webpack_asset_render(hooks.FrontEndBaseASyncHook, async=True) diff --git a/kolibri/core/webpack/utils.py b/kolibri/core/webpack/utils.py index d77553c63ff..3b94029ecd9 100644 --- a/kolibri/core/webpack/utils.py +++ b/kolibri/core/webpack/utils.py @@ -8,6 +8,8 @@ from __future__ import absolute_import, print_function, unicode_literals from django.conf import settings +from django.utils.safestring import mark_safe + def render_as_url(chunk): """ @@ -23,3 +25,18 @@ def render_as_url(chunk): static = getattr(settings, 'STATIC_URL') url = chunk.get('publicPath') or chunk['url'] return "{static}{url}".format(static=static, url=url) + +def webpack_asset_render(HookClass, async=False): + """ + This is produces content for a script tag for a WebpackInclusionHook subclass that implement + different render to html methods either sync or async. + :param HookClass: a subclass of WebpackInclusionHook + :param sync: Render sync or async. + :return: HTML of script tags to insert + """ + tags = [] + for hook in HookClass().registered_hooks: + tags.append( + hook.render_to_page_load_sync_html() if not async else hook.render_to_page_load_async_html() + ) + return mark_safe('\n'.join(tags))