From 06efd947ca0fa7b2bff9a6f052556540afe8a5e8 Mon Sep 17 00:00:00 2001 From: Jay Guo Date: Thu, 5 Dec 2024 14:26:24 -0500 Subject: [PATCH] Move footer_script_snippet helper function --- ckanext/opendata_theme/base/helpers.py | 16 ++++++++++++++++ .../opengov_custom_footer/plugin/__init__.py | 13 ------------- .../opengov_custom_theme/plugin.py | 1 + .../opendata_theme/tests/base/test_helpers.py | 7 ++++++- .../opengov_custom_footer/test_custom_footer.py | 5 ----- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/ckanext/opendata_theme/base/helpers.py b/ckanext/opendata_theme/base/helpers.py index afbf5c6..29abe7c 100644 --- a/ckanext/opendata_theme/base/helpers.py +++ b/ckanext/opendata_theme/base/helpers.py @@ -13,6 +13,11 @@ from ckanext.opendata_theme.base.compatibility_controller import BaseCompatibilityController from ckanext.opendata_theme.opengov_custom_homepage.constants import CUSTOM_NAMING +if toolkit.check_ckan_version(min_version='2.9.0'): + from ckan.lib.helpers import literal +else: + from webhelpers.html import literal + logger = logging.getLogger(__name__) @@ -300,3 +305,14 @@ def get_default_extent(): [-66.9513812,24.7433195],[-66.9513812,49.3457868], \ [-124.7844079,49.3457868],[-124.7844079,24.7433195]]] }' ) + + +def get_footer_script_snippet(): + pattern = r']*>(.*?)<\/script>' + script_snippet = toolkit.config.get('ckanext.opendata_theme.script_snippet', '') + if not script_snippet: + return False + match = re.match(pattern, script_snippet, re.IGNORECASE) + if not bool(match): + return False + return literal(script_snippet) diff --git a/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py b/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py index 40f21e8..45d04e6 100644 --- a/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py +++ b/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py @@ -1,6 +1,5 @@ import ckan.plugins as plugins import ckan.plugins.toolkit as toolkit -import re import ckanext.opendata_theme.base.helpers as helper from ckanext.opendata_theme.opengov_custom_footer.controller import CustomFooterController @@ -49,7 +48,6 @@ def get_validators(self): def get_helpers(self): return { 'opendata_theme_get_footer_data': get_footer_data, - 'opendata_theme_get_footer_script_snippet': get_footer_script_snippet, 'version': helper.version_builder, } @@ -61,17 +59,6 @@ def get_footer_data(section): return '' -def get_footer_script_snippet(): - pattern = r']*>(.*?)<\/script>' - script_snippet = toolkit.config.get('ckanext.opendata_theme.script_snippet', '') - if not script_snippet: - return False - match = re.match(pattern, script_snippet, re.IGNORECASE) - if not bool(match): - return False - return literal(script_snippet) - - def custom_footer_validator(value): layout_type = value.get('layout_type') if layout_type not in ['default', 'custom']: diff --git a/ckanext/opendata_theme/opengov_custom_theme/plugin.py b/ckanext/opendata_theme/opengov_custom_theme/plugin.py index 1d9f9c9..7c0bdb5 100644 --- a/ckanext/opendata_theme/opengov_custom_theme/plugin.py +++ b/ckanext/opendata_theme/opengov_custom_theme/plugin.py @@ -33,6 +33,7 @@ def get_helpers(self): 'opendata_theme_group_alias': helper.get_group_alias, 'opendata_theme_organization_alias': helper.get_organization_alias, 'opendata_theme_get_default_extent': helper.get_default_extent, + 'opendata_theme_get_footer_script_snippet': helper.get_footer_script_snippet, 'opendata_theme_is_data_dict_active': helper.is_data_dict_active, 'version': helper.version_builder, 'opendata_theme_segment_writekey': helper.get_segment_writekey, diff --git a/ckanext/opendata_theme/tests/base/test_helpers.py b/ckanext/opendata_theme/tests/base/test_helpers.py index a0812dc..1b76ab6 100644 --- a/ckanext/opendata_theme/tests/base/test_helpers.py +++ b/ckanext/opendata_theme/tests/base/test_helpers.py @@ -4,7 +4,8 @@ abbreviate_name, is_data_dict_active, get_group_alias, get_organization_alias, version_builder, check_characters, - sanityze_all_html + sanityze_all_html, + get_footer_script_snippet ) from packaging.version import InvalidVersion @@ -54,3 +55,7 @@ def test_sanityze_all_html(): assert sanityze_all_html('') == '<script>alert("test")</script>' assert sanityze_all_html('test') == '<a href="test">test</a>' assert sanityze_all_html('test') == 'test' + + +def test_invalid_get_footer_script_snippet(): + assert get_footer_script_snippet() is False diff --git a/ckanext/opendata_theme/tests/opengov_custom_footer/test_custom_footer.py b/ckanext/opendata_theme/tests/opengov_custom_footer/test_custom_footer.py index 2e3396c..082d97c 100644 --- a/ckanext/opendata_theme/tests/opengov_custom_footer/test_custom_footer.py +++ b/ckanext/opendata_theme/tests/opengov_custom_footer/test_custom_footer.py @@ -1,6 +1,5 @@ import pytest -from ckanext.opendata_theme.opengov_custom_footer.plugin import get_footer_script_snippet from ckanext.opendata_theme.tests.helpers import do_get, do_post CUSTOM_FOOTER_URL = "/ckan-admin/custom_footer" @@ -101,7 +100,3 @@ def test_reset_custom_footer_form_after_some_footer_modification(app): 'content_2': '' } check_custom_footer_page_html(reset_response, **expected_data) - - -def test_invalid_get_footer_script_snippet(): - assert get_footer_script_snippet() is False