From 05377dc3e9cf43fa0d0a10a1b45c0afc0ca3e576 Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Wed, 29 Jan 2020 19:18:58 +0100 Subject: [PATCH] Add template tag for better widget inclusion --- aleksis/core/models.py | 3 +++ aleksis/core/templates/core/index.html | 6 ++---- aleksis/core/templatetags/dashboard.py | 13 +++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 aleksis/core/templatetags/dashboard.py diff --git a/aleksis/core/models.py b/aleksis/core/models.py index 0696979ce..8995d6d57 100644 --- a/aleksis/core/models.py +++ b/aleksis/core/models.py @@ -262,6 +262,9 @@ class DashboardWidget(PolymorphicModel): def get_context(self): raise NotImplementedError("A widget subclass needs to implement the get_context method.") + def get_template(self): + return self.template + def __str__(self): return self.title diff --git a/aleksis/core/templates/core/index.html b/aleksis/core/templates/core/index.html index 60b0e2ec9..22d0d4013 100644 --- a/aleksis/core/templates/core/index.html +++ b/aleksis/core/templates/core/index.html @@ -1,5 +1,5 @@ {% extends 'core/base.html' %} -{% load i18n static %} +{% load i18n static dashboard %} {% block browser_title %}{% blocktrans %}Home{% endblocktrans %}{% endblock %} @@ -27,9 +27,7 @@ <div class="row" id="live_load"> {% for widget in widgets %} <div class="col s12 m12 l6 xl4"> - {% with widget as d_widget %} - {% include widget.template %} - {% endwith %} + {% include_widget widget %} </div> {% endfor %} </div> diff --git a/aleksis/core/templatetags/dashboard.py b/aleksis/core/templatetags/dashboard.py new file mode 100644 index 000000000..b051084f8 --- /dev/null +++ b/aleksis/core/templatetags/dashboard.py @@ -0,0 +1,13 @@ +from django.template import Library, loader + +register = Library() + + +@register.simple_tag +def include_widget(widget) -> dict: + """ Render a template with context from a defined widget """ + + template = loader.get_template(widget.get_template()) + context = widget.get_context() + + return template.render(context) -- GitLab