Skip to content

Commit a523e9d

Browse files
committed
add string interpolators
1 parent 1169836 commit a523e9d

File tree

7 files changed

+151
-1
lines changed

7 files changed

+151
-1
lines changed

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Changelog
44
4.0.0a4 (unreleased)
55
--------------------
66

7-
- Nothing changed yet.
7+
- Add string interpolators for volto portal url and volto absolute url. Fixes #44
8+
[erral]
89

910

1011
4.0.0a3 (2022-02-04)

src/plone/volto/configure.zcml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<include package=".behaviors" />
1717
<include package=".browser" />
18+
<include package=".interpolators" />
1819

1920
<include file="profiles.zcml" />
2021
<include file="patches.zcml" />

src/plone/volto/interpolators/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<configure
2+
xmlns="http://namespaces.zope.org/zope"
3+
xmlns:i18n="http://namespaces.zope.org/i18n"
4+
i18n_domain="plone">
5+
6+
7+
<adapter
8+
for="*"
9+
provides="plone.stringinterp.interfaces.IStringSubstitution"
10+
factory=".volto_url_substitution.VoltoURLSubstitution"
11+
name="volto_absolute_url"
12+
/>
13+
14+
<adapter
15+
for="*"
16+
provides="plone.stringinterp.interfaces.IStringSubstitution"
17+
factory=".volto_portal_url.VoltoPortalURLSubstitution"
18+
name="volto_portal_url"
19+
/>
20+
21+
22+
23+
</configure>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Content rule string interpolator to return volto url of a given content item
3+
"""
4+
5+
from plone import api
6+
from plone.stringinterp.adapters import BaseSubstitution
7+
from plone.volto import _
8+
from Products.CMFCore.interfaces import IContentish
9+
from zope.component import adapter
10+
11+
12+
@adapter(IContentish)
13+
class VoltoAbsoluteURLSubstitution(BaseSubstitution):
14+
""" URL Substitution adapter """
15+
16+
category = _(u"All Content")
17+
description = _(u"Volto URL")
18+
19+
def safe_call(self):
20+
""" get the url """
21+
context_url = self.context.absolute_url()
22+
plone_domain = api.portal.get().absolute_url()
23+
frontend_domain = api.portal.get_registry_record("volto.frontend_domain")
24+
if frontend_domain.endswith("/"):
25+
frontend_domain = frontend_domain[:-1]
26+
27+
return context_url.replace(plone_domain, frontend_domain)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Content rule string interpolator to return volto url of a given content item
3+
"""
4+
5+
from plone import api
6+
from plone.stringinterp.adapters import BaseSubstitution
7+
from plone.volto import _
8+
from Products.CMFCore.interfaces import IContentish
9+
from zope.component import adapter
10+
11+
12+
@adapter(IContentish)
13+
class VoltoPortalURLSubstitution(BaseSubstitution):
14+
""" URL Substitution adapter """
15+
16+
category = _(u"All Content")
17+
description = _(u"Volto Portal URL")
18+
19+
def safe_call(self):
20+
""" get the url """
21+
frontend_domain = api.portal.get_registry_record("volto.frontend_domain")
22+
return frontend_domain
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
""" test string interpolation substitutions for content rules """
2+
# -*- coding: utf-8 -*-
3+
import unittest
4+
5+
from plone import api
6+
from plone.app.testing import TEST_USER_ID, setRoles
7+
from plone.stringinterp.interfaces import IStringInterpolator
8+
9+
from plone.volto.interpolators.volto_portal_url import (
10+
VoltoPortalURLSubstitution,
11+
)
12+
from plone.volto.interpolators.volto_absolute_url import (
13+
VoltoAbsoluteURLSubstitution,
14+
)
15+
from plone.volto.testing import (
16+
PLONE_VOLTO_CORE_INTEGRATION_TESTING,
17+
PLONE_VOLTO_CORE_FUNCTIONAL_TESTING,
18+
)
19+
20+
21+
class TestSubstitutions(unittest.TestCase):
22+
""" Test case for substitutions"""
23+
24+
layer = PLONE_VOLTO_CORE_INTEGRATION_TESTING
25+
26+
def setUp(self):
27+
self.portal = self.layer["portal"]
28+
self.request = self.layer["request"]
29+
30+
def test_volto_portal_url(self):
31+
""" test for volto_portal_url"""
32+
volto_url = api.portal.get_registry_record("volto.frontend_domain")
33+
substitution = VoltoPortalURLSubstitution(self.portal)()
34+
self.assertEqual(substitution, volto_url)
35+
36+
def test_string_interpolation_volto_portal_url(self):
37+
""" test interpolating as string """
38+
volto_url = api.portal.get_registry_record("volto.frontend_domain")
39+
string = "${volto_portal_url}"
40+
value = IStringInterpolator(self.portal)(string)
41+
self.assertEqual(value, volto_url)
42+
43+
44+
class TestSubstitutionsFunctional(unittest.TestCase):
45+
""" Test case for substitutions"""
46+
47+
layer = PLONE_VOLTO_CORE_FUNCTIONAL_TESTING
48+
49+
def setUp(self):
50+
""" test setup """
51+
self.portal = self.layer["portal"]
52+
self.request = self.layer["request"]
53+
setRoles(self.portal, TEST_USER_ID, ["Manager"])
54+
55+
self.portal.invokeFactory("Document", id="doc", title="My Document")
56+
self.document = self.portal.get("doc")
57+
58+
def test_volto_absolute_url(self):
59+
""" test for volto_absolute_url"""
60+
61+
volto_url = api.portal.get_registry_record("volto.frontend_domain")
62+
portal_url = self.portal.absolute_url()
63+
context_url = self.document.absolute_url()
64+
substitution = VoltoAbsoluteURLSubstitution(self.document)()
65+
self.assertEqual(substitution, context_url.replace(portal_url, volto_url))
66+
67+
def test_string_interpolation_volto_absolute_url(self):
68+
""" test as string interpolator"""
69+
70+
volto_url = api.portal.get_registry_record("volto.frontend_domain")
71+
portal_url = self.portal.absolute_url()
72+
context_url = self.document.absolute_url()
73+
74+
string = "${volto_absolute_url}"
75+
value = IStringInterpolator(self.document)(string)
76+
self.assertEqual(value, context_url.replace(portal_url, volto_url))

0 commit comments

Comments
 (0)