Skip to content

Commit

Permalink
add __str__ to LinkDict
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Nov 10, 2024
1 parent deac3a4 commit fc3c0f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions djangocms_link/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions tests/test_link_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")

Expand All @@ -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")
Expand All @@ -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(
Expand All @@ -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)

0 comments on commit fc3c0f7

Please sign in to comment.