diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 89efcca8..00cb6b6d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -36,3 +36,8 @@ repos: rev: 5.13.2 hooks: - id: isort + + - repo: https://github.com/tox-dev/pyproject-fmt + rev: v2.5.0 + hooks: + - id: pyproject-fmt diff --git a/djangocms_link/helpers.py b/djangocms_link/helpers.py index df281d75..00d4aeb4 100644 --- a/djangocms_link/helpers.py +++ b/djangocms_link/helpers.py @@ -109,3 +109,7 @@ def type(self): return "anchor" return "external_link" return "" + + def __str__(self): + """If inserted into a Django template, expand the url.""" + return self.url diff --git a/tests/test_link_dict.py b/tests/test_link_dict.py index 3bdc504b..07a79dbf 100644 --- a/tests/test_link_dict.py +++ b/tests/test_link_dict.py @@ -25,6 +25,8 @@ def test_external_link(self): self.assertEqual(link1.url, "https://www.example.com") self.assertEqual(link2.url, "https://www.django-cms.org") + self.assertEqual(str(link1), "https://www.example.com") + self.assertEqual(str(link2), "https://www.django-cms.org") self.assertEqual(link1.type, "external_link") self.assertEqual(link2.type, "external_link") @@ -36,6 +38,8 @@ def test_file_link(self): self.assertEqual(link1, link2) self.assertEqual(link1.url, file.url) self.assertEqual(link2.url, file.url) + self.assertEqual(str(link1), file.url) + self.assertEqual(str(link2), file.url) self.assertEqual(link1.type, "file_link") self.assertEqual(link2.type, "file_link") @@ -52,6 +56,9 @@ def test_internal_link(self): self.assertEqual(link1.url, obj.get_absolute_url()) self.assertEqual(link2.url, obj.get_absolute_url()) self.assertEqual(link3.url, f"{obj.get_absolute_url()}#test") + self.assertEqual(str(link1), obj.get_absolute_url()) + self.assertEqual(str(link2), obj.get_absolute_url()) + self.assertEqual(str(link3), f"{obj.get_absolute_url()}#test") self.assertEqual(link1.type, "internal_link") self.assertEqual(link2.type, "internal_link") self.assertEqual(link3.type, "internal_link") @@ -74,6 +81,7 @@ def test_db_queries(self): link = LinkDict(obj) with self.assertNumQueries(0): self.assertEqual(link.url, obj.get_absolute_url()) + self.assertEqual(str(link), obj.get_absolute_url()) def test_cache_no_written_to_db(self): obj = ThirdPartyModel.objects.create( @@ -87,4 +95,5 @@ def test_cache_no_written_to_db(self): link = Link.objects.get(pk=link.pk) # load from db + # Cache not saved to db self.assertNotIn("__cache__", link.link)