Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add string interpolators #48

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions news/44.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added string interpolators to create volto URLs for context and portal based on an existing setting
[erral]
1 change: 1 addition & 0 deletions src/plone/volto/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<include package=".behaviors" />
<include package=".browser" />
<include package=".interpolators" />

<include file="profiles.zcml" />
<include file="patches.zcml" />
Expand Down
Empty file.
24 changes: 24 additions & 0 deletions src/plone/volto/interpolators/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:i18n="http://namespaces.zope.org/i18n"
i18n_domain="plone"
>


<adapter
factory=".volto_absolute_url.VoltoAbsoluteURLSubstitution"
provides="plone.stringinterp.interfaces.IStringSubstitution"
for="*"
name="volto_absolute_url"
/>

<adapter
factory=".volto_portal_url.VoltoPortalURLSubstitution"
provides="plone.stringinterp.interfaces.IStringSubstitution"
for="*"
name="volto_portal_url"
/>



</configure>
25 changes: 25 additions & 0 deletions src/plone/volto/interpolators/volto_absolute_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-

from plone import api
from plone.stringinterp.adapters import BaseSubstitution
from plone.volto import _
from Products.CMFCore.interfaces import IContentish
from zope.component import adapter


@adapter(IContentish)
class VoltoAbsoluteURLSubstitution(BaseSubstitution):
"""URL Substitution adapter"""

category = _("All Content")
description = _("Volto URL")

def safe_call(self):
"""get the url"""
context_url = self.context.absolute_url()
plone_domain = api.portal.get().absolute_url()
frontend_domain = api.portal.get_registry_record("volto.frontend_domain")
if frontend_domain.endswith("/"):
frontend_domain = frontend_domain[:-1]

return context_url.replace(plone_domain, frontend_domain)
20 changes: 20 additions & 0 deletions src/plone/volto/interpolators/volto_portal_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-

from plone import api
from plone.stringinterp.adapters import BaseSubstitution
from plone.volto import _
from Products.CMFCore.interfaces import IContentish
from zope.component import adapter


@adapter(IContentish)
class VoltoPortalURLSubstitution(BaseSubstitution):
"""URL Substitution adapter"""

category = _("All Content")
description = _("Volto Portal URL")

def safe_call(self):
"""get the url"""
frontend_domain = api.portal.get_registry_record("volto.frontend_domain")
return frontend_domain
75 changes: 75 additions & 0 deletions src/plone/volto/tests/test_interpolators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
import unittest

from plone import api
from plone.app.testing import TEST_USER_ID, setRoles
from plone.stringinterp.interfaces import IStringInterpolator

from plone.volto.interpolators.volto_portal_url import (
VoltoPortalURLSubstitution,
)
from plone.volto.interpolators.volto_absolute_url import (
VoltoAbsoluteURLSubstitution,
)
from plone.volto.testing import (
PLONE_VOLTO_CORE_INTEGRATION_TESTING,
PLONE_VOLTO_CORE_FUNCTIONAL_TESTING,
)


class TestSubstitutions(unittest.TestCase):
"""Test case for substitutions"""

layer = PLONE_VOLTO_CORE_INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer["portal"]
self.request = self.layer["request"]

def test_volto_portal_url(self):
"""test for volto_portal_url"""
volto_url = api.portal.get_registry_record("volto.frontend_domain")
substitution = VoltoPortalURLSubstitution(self.portal)()
self.assertEqual(substitution, volto_url)

def test_string_interpolation_volto_portal_url(self):
"""test interpolating as string"""
volto_url = api.portal.get_registry_record("volto.frontend_domain")
string = "${volto_portal_url}"
value = IStringInterpolator(self.portal)(string)
self.assertEqual(value, volto_url)


class TestSubstitutionsFunctional(unittest.TestCase):
"""Test case for substitutions"""

layer = PLONE_VOLTO_CORE_FUNCTIONAL_TESTING

def setUp(self):
"""test setup"""
self.portal = self.layer["portal"]
self.request = self.layer["request"]
setRoles(self.portal, TEST_USER_ID, ["Manager"])

self.portal.invokeFactory("Document", id="doc", title="My Document")
self.document = self.portal.get("doc")

def test_volto_absolute_url(self):
"""test for volto_absolute_url"""

volto_url = api.portal.get_registry_record("volto.frontend_domain")
portal_url = self.portal.absolute_url()
context_url = self.document.absolute_url()
substitution = VoltoAbsoluteURLSubstitution(self.document)()
self.assertEqual(substitution, context_url.replace(portal_url, volto_url))

def test_string_interpolation_volto_absolute_url(self):
"""test as string interpolator"""

volto_url = api.portal.get_registry_record("volto.frontend_domain")
portal_url = self.portal.absolute_url()
context_url = self.document.absolute_url()

string = "${volto_absolute_url}"
value = IStringInterpolator(self.document)(string)
self.assertEqual(value, context_url.replace(portal_url, volto_url))