Skip to content

Commit

Permalink
You can pass a distribution_name to factory.addPloneSite.
Browse files Browse the repository at this point in the history
We then pass all other arguments and keyword arguments to the `plone.distribution` site api.
  • Loading branch information
mauritsvanrees committed Sep 12, 2024
1 parent ef89e53 commit bea0b3d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Products/CMFPlone/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,34 @@ def addPloneSite(
setup_content=None,
default_language="en",
portal_timezone="UTC",
distribution_name=None,
**kwargs,
):
"""Add a PloneSite to the context."""
if distribution_name:
from plone.distribution.api import site as site_api

# Pass all arguments and keyword arguments in the answers,
# But the 'distribution_name' is not needed there.
answers = {
"site_id": site_id,
"title": title,
"description": description,
"profile_id": profile_id,
"snapshot": snapshot,
"content_profile_id": content_profile_id,
"extension_ids": extension_ids,
"setup_content": setup_content,
"default_language": default_language,
"portal_timezone": portal_timezone,
}
answers.update(kwargs)
site = site_api._create_site(
context=context, distribution_name=distribution_name, answers=answers
)
setSite(site)
return site

if content_profile_id is not None:
warnings.warn(
"addPloneSite ignores the content_profile_id keyword argument "
Expand Down
12 changes: 12 additions & 0 deletions Products/CMFPlone/tests/configure.zcml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
xmlns:plone="http://namespaces.plone.org/plone"
xmlns:zcml="http://namespaces.zope.org/zcml"
i18n_domain="plone"
>

Expand All @@ -11,4 +13,14 @@
permission="zope2.View"
/>

<configure zcml:condition="installed plone.distribution">
<include package="plone.distribution" />
<plone:distribution
name="testdistro"
title="Plone Site"
headless="false"
post_handler="plone.distribution.handler.post_handler"
/>
</configure>

</configure>
25 changes: 25 additions & 0 deletions Products/CMFPlone/tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from importlib.metadata import distribution
from plone.dexterity.interfaces import IDexterityFTI
from plone.registry.interfaces import IRegistry
from Products.CMFPlone.factory import addPloneSite
Expand All @@ -9,6 +10,13 @@
import unittest


try:
distribution("plone.distribution")
HAS_DISTRIBUTION = True
except PackageNotFoundError:
HAS_DISTRIBUTION = False


class TestFactoryPloneSite(unittest.TestCase):
layer = PRODUCTS_CMFPLONE_INTEGRATION_TESTING

Expand Down Expand Up @@ -67,3 +75,20 @@ def test_site_creation_title_is_set_in_registry(self):
ploneSite = addPloneSite(self.app, "ploneFoo", title="Foo")
registry = getUtility(IRegistry, context=ploneSite)
self.assertEqual(registry["plone.site_title"], "Foo")

@unittest.skipIf(
not HAS_DISTRIBUTION,
"Passing a distribution_name needs plone.distribution.",
)
def test_site_creation_distribution(self):
"""Create a Plone Site using a distribution"""
ploneSite = addPloneSite(
self.app,
"ploneFoo",
title="Foo",
distribution_name="testdistro",
default_language="nl",
)
self.assertEqual(ploneSite.getId(), "ploneFoo")
self.assertEqual(ploneSite.title, "Foo")
self.assertEqual(ploneSite.Language(), "nl")
3 changes: 3 additions & 0 deletions news/3961.feature.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You can pass a `distribution_name` to `factory.addPloneSite`.
We then pass all other arguments and keyword arguments to the `plone.distribution` site api.
[maurits]

0 comments on commit bea0b3d

Please sign in to comment.