diff --git a/djangocms_link/helpers.py b/djangocms_link/helpers.py index c0463364..b9bd7715 100644 --- a/djangocms_link/helpers.py +++ b/djangocms_link/helpers.py @@ -26,6 +26,8 @@ def get_rel_obj(internal_link: str) -> models.Model | None: def get_link(link_field_value: dict, site_id: int | None = None) -> str | None: + if not link_field_value: + return None if "external_link" in link_field_value: if link_field_value["external_link"].startswith("tel:"): return link_field_value["external_link"].replace(" ", "") diff --git a/djangocms_link/templatetags/djangocms_link_tags.py b/djangocms_link/templatetags/djangocms_link_tags.py index b8c3a1b1..63c72a80 100644 --- a/djangocms_link/templatetags/djangocms_link_tags.py +++ b/djangocms_link/templatetags/djangocms_link_tags.py @@ -8,12 +8,9 @@ @register.filter def to_url(value): - if not value: - return "" - return get_link(value) or "" @register.simple_tag -def get_url(context, value): +def get_url(value): return to_url(value) diff --git a/tests/test_models.py b/tests/test_models.py index 85378a7d..4a646ecd 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -103,7 +103,7 @@ def test_get_url_template_tag(self): from djangocms_link.templatetags.djangocms_link_tags import get_url, to_url self.assertEqual( - get_url({}, self.internal_link.link), + get_url(self.internal_link.link), "//example.com" + self.page.get_absolute_url() + "#some_id" ) self.assertEqual(to_url(self.file_link.link), self.file.url) @@ -111,6 +111,8 @@ def test_get_url_template_tag(self): self.assertEqual(to_url(self.phone_link.link), "tel:+0123456789") self.assertEqual(to_url(self.mail_link.link), "mailto:test@email.com") self.assertEqual(to_url(self.anchor_link.link), "#some_id") + self.assertEqual(to_url(None), "") + self.assertEqual(to_url({}), "") def test_respect_link_is_optional(self): # by now the configuration is good again