From 0d195add3dc5f55f7704736a616559bdfa325b5a Mon Sep 17 00:00:00 2001 From: Jay Guo Date: Thu, 7 Nov 2024 18:15:06 -0500 Subject: [PATCH 1/3] Fix issue with package_tracking_summary --- ckanext/opendata_theme/base/helpers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ckanext/opendata_theme/base/helpers.py b/ckanext/opendata_theme/base/helpers.py index 34aae52..afbf5c6 100644 --- a/ckanext/opendata_theme/base/helpers.py +++ b/ckanext/opendata_theme/base/helpers.py @@ -126,7 +126,10 @@ def package_tracking_summary(package): tracking_summary = result.get('tracking_summary') except Exception: logger.debug("[opendata_theme] Error getting dataset tracking_summary") - return {} + return { + 'total': 0, + 'recent': 0 + } return tracking_summary From 95297410a9747128ca943f45219d7330a74a0204 Mon Sep 17 00:00:00 2001 From: Jay Guo Date: Fri, 8 Nov 2024 16:31:28 -0500 Subject: [PATCH 2/3] Add custom script snippet to footer --- .../opengov_custom_footer/plugin/__init__.py | 13 +++++++++++++ .../templates/custom_footer.html | 4 ++++ .../templates/default_footer.html | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py b/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py index a716c0f..b963467 100644 --- a/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py +++ b/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py @@ -1,5 +1,6 @@ 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 @@ -48,6 +49,7 @@ 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, } @@ -59,6 +61,17 @@ 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_footer/templates/custom_footer.html b/ckanext/opendata_theme/opengov_custom_footer/templates/custom_footer.html index 1971b0c..89c41c8 100644 --- a/ckanext/opendata_theme/opengov_custom_footer/templates/custom_footer.html +++ b/ckanext/opendata_theme/opengov_custom_footer/templates/custom_footer.html @@ -24,4 +24,8 @@ {% endblock %} + {% set script_snippet = h.opendata_theme_get_footer_script_snippet() %} + {% if script_snippet %} + {{ script_snippet }} + {% endif %} {% endblock %} diff --git a/ckanext/opendata_theme/opengov_custom_footer/templates/default_footer.html b/ckanext/opendata_theme/opengov_custom_footer/templates/default_footer.html index a21c717..69d18c7 100644 --- a/ckanext/opendata_theme/opengov_custom_footer/templates/default_footer.html +++ b/ckanext/opendata_theme/opengov_custom_footer/templates/default_footer.html @@ -33,4 +33,8 @@ {% endblock %} +{% set script_snippet = h.opendata_theme_get_footer_script_snippet() %} +{% if script_snippet %} + {{ script_snippet }} +{% endif %} {% endblock %} From 8ca82c5cb075b1629f0bc56665badf4447b23f3b Mon Sep 17 00:00:00 2001 From: Jay Guo Date: Fri, 8 Nov 2024 19:40:41 -0500 Subject: [PATCH 3/3] Add test --- .../opengov_custom_footer/plugin/__init__.py | 2 +- .../opendata_theme/tests/base/test_helpers.py | 48 ++++++++++++++++--- .../test_custom_footer.py | 5 ++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py b/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py index b963467..40f21e8 100644 --- a/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py +++ b/ckanext/opendata_theme/opengov_custom_footer/plugin/__init__.py @@ -77,7 +77,7 @@ def custom_footer_validator(value): if layout_type not in ['default', 'custom']: raise toolkit.Invalid('Invalid footer layout') - # content is sanitized by controller soo only taught validation required + # content is sanitized by controller so only simple validation required content_0 = value.get('content_0') if helper.check_characters(content_0): raise toolkit.Invalid('Invalid characters in Column 1') diff --git a/ckanext/opendata_theme/tests/base/test_helpers.py b/ckanext/opendata_theme/tests/base/test_helpers.py index ece1aed..a0812dc 100644 --- a/ckanext/opendata_theme/tests/base/test_helpers.py +++ b/ckanext/opendata_theme/tests/base/test_helpers.py @@ -1,20 +1,56 @@ import pytest -from ckanext.opendata_theme.base.helpers import version_builder +from ckanext.opendata_theme.base.helpers import ( + abbreviate_name, is_data_dict_active, + get_group_alias, get_organization_alias, + version_builder, check_characters, + sanityze_all_html +) from packaging.version import InvalidVersion +def test_abbreviate_name(): + assert abbreviate_name('John Smith') == 'JS' + assert abbreviate_name('test') == 'T' + + +def test_is_data_dict_active(): + assert is_data_dict_active([{'info': {'label': 'test'}}]) + assert is_data_dict_active([{'info': {'notes': 'test'}}]) + assert is_data_dict_active([{'info': {'label': 'test', 'notes': 'test'}}]) + assert not is_data_dict_active([{'info': {}}]) + assert not is_data_dict_active([{}]) + assert not is_data_dict_active([]) + + +def test_get_group_alias(): + assert get_group_alias() == 'Group' + + +def test_get_organization_alias(): + assert get_organization_alias() == 'Organization' + + def test_version_builder_positive(): assert version_builder('2.7') < version_builder('2.9') assert version_builder('2.7.0') < version_builder('2.7.3') assert version_builder('2.7.3') < version_builder('2.7.12') - assert version_builder('2.7.3') < version_builder('2.9.0') - assert version_builder('2.7.3') < version_builder('2.9.7') - assert version_builder('2.7.12') < version_builder('2.9.0') - assert version_builder('2.7.12') < version_builder('2.9.7') - assert version_builder('2.7.12') < version_builder('2.10.0') + assert version_builder('2.7.3') < version_builder('2.9.11') + assert version_builder('2.7.12') < version_builder('2.9.11') + assert version_builder('2.9.11') < version_builder('2.10.0') + assert version_builder('2.9.11') < version_builder('2.11.0') def test_version_builder_failed_to_build(): with pytest.raises(InvalidVersion): assert version_builder('1.3.xy123') + + +def test_check_characters(): + assert check_characters('') is False + + +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' 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 082d97c..2e3396c 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,5 +1,6 @@ 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" @@ -100,3 +101,7 @@ 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