diff --git a/opac/tests/test_main_custom_filters.py b/opac/tests/test_main_custom_filters.py new file mode 100644 index 000000000..1552acaf6 --- /dev/null +++ b/opac/tests/test_main_custom_filters.py @@ -0,0 +1,57 @@ +# coding: utf-8 +from webapp.main.custom_filters import get_absolute_url + +from .base import BaseTestCase + + +class CustomFiltersTestCase(BaseTestCase): + """Test cases for custom Jinja2 filters in webapp.main.custom_filters""" + + def test_get_absolute_url_with_absolute_http_url(self): + """Test that absolute HTTP URLs are returned as-is""" + url = "http://example.com/logo.png" + base_url = "http://scielo.do/" + result = get_absolute_url(url, base_url) + self.assertEqual(result, "http://example.com/logo.png") + + def test_get_absolute_url_with_absolute_https_url(self): + """Test that absolute HTTPS URLs are returned as-is""" + url = "https://example.com/logo.png" + base_url = "http://scielo.do/" + result = get_absolute_url(url, base_url) + self.assertEqual(result, "https://example.com/logo.png") + + def test_get_absolute_url_with_relative_url_with_slash(self): + """Test that relative URLs with leading slash are concatenated correctly""" + url = "/media/logo.png" + base_url = "http://scielo.do/" + result = get_absolute_url(url, base_url) + self.assertEqual(result, "http://scielo.do/media/logo.png") + + def test_get_absolute_url_with_relative_url_without_slash(self): + """Test that relative URLs without leading slash are concatenated correctly""" + url = "media/logo.png" + base_url = "http://scielo.do/" + result = get_absolute_url(url, base_url) + self.assertEqual(result, "http://scielo.do/media/logo.png") + + def test_get_absolute_url_with_empty_url(self): + """Test that empty URLs return empty string""" + url = "" + base_url = "http://scielo.do/" + result = get_absolute_url(url, base_url) + self.assertEqual(result, "") + + def test_get_absolute_url_with_none_url(self): + """Test that None URLs return empty string""" + url = None + base_url = "http://scielo.do/" + result = get_absolute_url(url, base_url) + self.assertEqual(result, "") + + def test_get_absolute_url_with_empty_base_url(self): + """Test that relative URLs with empty base_url work""" + url = "/media/logo.png" + base_url = "" + result = get_absolute_url(url, base_url) + self.assertEqual(result, "media/logo.png") diff --git a/opac/webapp/__init__.py b/opac/webapp/__init__.py index e25bd0505..633d3e790 100644 --- a/opac/webapp/__init__.py +++ b/opac/webapp/__init__.py @@ -150,6 +150,7 @@ def create_app(): # Registrando os filtros app.jinja_env.filters["trans_alpha2"] = custom_filters.trans_alpha2 app.jinja_env.filters["datetimefilter"] = custom_filters.datetimefilter + app.jinja_env.filters["get_absolute_url"] = custom_filters.get_absolute_url # i18n babel.init_app(app) diff --git a/opac/webapp/main/custom_filters.py b/opac/webapp/main/custom_filters.py index 5a5aca914..0da9f3163 100644 --- a/opac/webapp/main/custom_filters.py +++ b/opac/webapp/main/custom_filters.py @@ -17,3 +17,28 @@ def trans_alpha2(value): def datetimefilter(value, format="%Y-%m-%d %H:%M"): return utc_to_local(value).strftime(format) + + +def get_absolute_url(url, base_url=""): + """ + Returns an absolute URL. If the URL already starts with http:// or https://, + returns it as-is. Otherwise, concatenates base_url with url. + + Args: + url: The URL to process (may be relative or absolute) + base_url: The base URL to prepend if url is relative (default: "") + + Returns: + An absolute URL string + """ + if not url: + return "" + + # Check if URL is already absolute + if url.startswith("http://") or url.startswith("https://"): + return url + + # URL is relative, concatenate with base_url + # Remove leading slash from url if present, as base_url should handle it + url_cleaned = url.lstrip("/") + return f"{base_url}{url_cleaned}" diff --git a/opac/webapp/templates/article/includes/meta.html b/opac/webapp/templates/article/includes/meta.html index b77476482..767bbfea4 100644 --- a/opac/webapp/templates/article/includes/meta.html +++ b/opac/webapp/templates/article/includes/meta.html @@ -4,7 +4,7 @@ - + {% if article.authors_meta -%} diff --git a/opac/webapp/templates/issue/includes/meta.html b/opac/webapp/templates/issue/includes/meta.html index 2c7255ef2..5f9d68c27 100644 --- a/opac/webapp/templates/issue/includes/meta.html +++ b/opac/webapp/templates/issue/includes/meta.html @@ -5,5 +5,5 @@ - + diff --git a/opac/webapp/templates/journal/includes/meta.html b/opac/webapp/templates/journal/includes/meta.html index 87dc17c0c..0fbc95f99 100644 --- a/opac/webapp/templates/journal/includes/meta.html +++ b/opac/webapp/templates/journal/includes/meta.html @@ -4,5 +4,5 @@ - +