From 620e2752680354aa0ef6bcd470dc96da1f30800b Mon Sep 17 00:00:00 2001 From: minusf Date: Mon, 20 Nov 2017 11:31:56 +0100 Subject: [PATCH 1/2] allow css/js as urls --- src/fobi/base.py | 16 +++++----------- src/fobi/helpers.py | 22 ++++++++++------------ src/fobi/templates/fobi/generic/_base.html | 12 +++++------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/fobi/base.py b/src/fobi/base.py index 85f4256c..bb4e3c07 100644 --- a/src/fobi/base.py +++ b/src/fobi/base.py @@ -40,13 +40,13 @@ from .exceptions import ( ThemeDoesNotExist, ) from .helpers import ( + absolute_path, clean_dict, get_form_element_entries_for_form_wizard_entry, get_ignorable_form_values, map_field_name_to_label, safe_text, StrippedRequest, - uniquify_sequence, ) from .settings import ( CUSTOM_THEME_DATA, @@ -638,11 +638,8 @@ class BaseTheme(object): :return list: """ - media_css = self.media_css[:] - if self.plugin_media_css: - media_css += self.plugin_media_css - - media_css = uniquify_sequence(media_css) + media_css = [absolute_path(css) + for css in set(self.media_css + self.plugin_media_css)] return media_css @@ -651,11 +648,8 @@ class BaseTheme(object): :return list: """ - media_js = self.media_js[:] - if self.plugin_media_js: - media_js += self.plugin_media_js - - media_js = uniquify_sequence(media_js) + media_js = [absolute_path(js) + for js in set(self.media_js + self.plugin_media_js)] return media_js diff --git a/src/fobi/helpers.py b/src/fobi/helpers.py index 293bc690..31dd0322 100644 --- a/src/fobi/helpers.py +++ b/src/fobi/helpers.py @@ -18,6 +18,7 @@ from django.contrib.auth.models import AnonymousUser from django.core.files.base import File # from django.db.utils import DatabaseError from django.http import HttpResponse +from django.templatetags.static import static from django.test.client import RequestFactory from django.utils.encoding import force_text from django.utils.html import format_html_join @@ -52,6 +53,7 @@ __author__ = 'Artur Barseghyan ' __copyright__ = '2014-2017 Artur Barseghyan' __license__ = 'GPL 2.0/LGPL 2.1' __all__ = ( + 'absolute_path', 'admin_change_url', 'clean_dict', 'clone_file', @@ -78,7 +80,6 @@ __all__ = ( 'StrippedRequest', 'StrippedUser', 'two_dicts_to_string', - 'uniquify_sequence', 'update_plugin_data', 'validate_initial_for_choices', 'validate_initial_for_multiple_choices', @@ -188,18 +189,15 @@ def two_dicts_to_string(headers, data, html_element='p'): empty_string = text_type('') -def uniquify_sequence(sequence): - """Uniqify sequence. - - Makes sure items in the given sequence are unique, having the original - order preserved. - - :param iterable sequence: - :return list: +def absolute_path(path): """ - seen = set() - seen_add = seen.add - return [x for x in sequence if x not in seen and not seen_add(x)] + Given a relative or absolute path to a static asset, return an absolute + path. An absolute path will be returned unchanged while a relative path + will be passed to django.templatetags.static.static(). + """ + if path.startswith(('http://', 'https://', '/')): + return path + return static(path) def get_ignorable_form_values(): diff --git a/src/fobi/templates/fobi/generic/_base.html b/src/fobi/templates/fobi/generic/_base.html index 5c2103f7..3656bbea 100644 --- a/src/fobi/templates/fobi/generic/_base.html +++ b/src/fobi/templates/fobi/generic/_base.html @@ -21,8 +21,8 @@ {% block theme-stylesheets %} {##} - {% for css_file in fobi_theme.get_media_css %} - + {% for css in fobi_theme.get_media_css %} + {% endfor %} {% endblock theme-stylesheets %} @@ -189,7 +189,6 @@ {% endblock main-content-wrapper %} - {% endblock main-wrapper %} {% block javascripts %} @@ -199,11 +198,10 @@ {% block theme-javascripts %} {##} - {% for js_file in fobi_theme.get_media_js %} - + {% for js in fobi_theme.get_media_js %} + {% endfor %} - {% endblock theme-javascripts %} - + From 428a12c6223fc748f1f7ec1118cc8a1849738f76 Mon Sep 17 00:00:00 2001 From: minusf Date: Mon, 20 Nov 2017 14:37:38 +0100 Subject: [PATCH 2/2] use sets and keep order --- src/fobi/base.py | 8 +++----- src/fobi/helpers.py | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/fobi/base.py b/src/fobi/base.py index bb4e3c07..47753489 100644 --- a/src/fobi/base.py +++ b/src/fobi/base.py @@ -40,13 +40,13 @@ from .exceptions import ( ThemeDoesNotExist, ) from .helpers import ( - absolute_path, clean_dict, get_form_element_entries_for_form_wizard_entry, get_ignorable_form_values, map_field_name_to_label, safe_text, StrippedRequest, + uniquify_sequence, ) from .settings import ( CUSTOM_THEME_DATA, @@ -638,8 +638,7 @@ class BaseTheme(object): :return list: """ - media_css = [absolute_path(css) - for css in set(self.media_css + self.plugin_media_css)] + media_css = uniquify_sequence(self.media_css + self.plugin_media_css) return media_css @@ -648,8 +647,7 @@ class BaseTheme(object): :return list: """ - media_js = [absolute_path(js) - for js in set(self.media_js + self.plugin_media_js)] + media_js = uniquify_sequence(self.media_js + self.plugin_media_js) return media_js diff --git a/src/fobi/helpers.py b/src/fobi/helpers.py index 31dd0322..edf167ba 100644 --- a/src/fobi/helpers.py +++ b/src/fobi/helpers.py @@ -53,7 +53,6 @@ __author__ = 'Artur Barseghyan ' __copyright__ = '2014-2017 Artur Barseghyan' __license__ = 'GPL 2.0/LGPL 2.1' __all__ = ( - 'absolute_path', 'admin_change_url', 'clean_dict', 'clone_file', @@ -80,6 +79,7 @@ __all__ = ( 'StrippedRequest', 'StrippedUser', 'two_dicts_to_string', + 'uniquify_sequence', 'update_plugin_data', 'validate_initial_for_choices', 'validate_initial_for_multiple_choices', @@ -200,6 +200,21 @@ def absolute_path(path): return static(path) +def uniquify_sequence(sequence): + """Uniqify sequence. + + Makes sure items in the given sequence are unique, having the original + order preserved. + + :param iterable sequence: + :return list: + """ + seen = set() + seen_add = seen.add + return [absolute_path(x) + for x in sequence if x not in seen and not seen_add(x)] + + def get_ignorable_form_values(): """Get ignorable for form values.