Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions opac/tests/test_main_custom_filters.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions opac/webapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions opac/webapp/main/custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
2 changes: 1 addition & 1 deletion opac/webapp/templates/article/includes/meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta property="og:type" content="article"/>
<meta property="og:title" content="{{ article.title }}"/>
<meta property="og:description" content="{{ article.abstract|escape|truncate(100) }}"/>
<meta property="og:image" content="{{request.url_root}}{{ journal.logo_url|replace("/", "" , 1)}}"/>
<meta property="og:image" content="{{ journal.logo_url|get_absolute_url(request.url_root) }}"/>
<!-- social share tags -->

{% if article.authors_meta -%}
Expand Down
2 changes: 1 addition & 1 deletion opac/webapp/templates/issue/includes/meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<meta property="og:title" content="{{ journal.title }}" />
<meta property="og:description" content="{{ journal.get_mission_by_lang(session.lang[:2])|safe|default(_(" Periódico sem
missão cadastrada"), true) }}" />
<meta property="og:image" content="{{request.url_root}}{{ journal.logo_url|replace("/", "" , 1) }}" />
<meta property="og:image" content="{{ journal.logo_url|get_absolute_url(request.url_root) }}" />
<!-- social share tags -->
2 changes: 1 addition & 1 deletion opac/webapp/templates/journal/includes/meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<meta property="og:type" content="website"/>
<meta property="og:title" content="{{ journal.title }}"/>
<meta property="og:description" content="{{ journal.get_mission_by_lang(session.lang[:2])|striptags|default(_("Periódico sem missão cadastrada"), true) }}"/>
<meta property="og:image" content="{{request.url_root}}{{ journal.logo_url|replace("/", "", 1) }}"/>
<meta property="og:image" content="{{ journal.logo_url|get_absolute_url(request.url_root) }}"/>
<!-- social share tags -->