Skip to content

Commit

Permalink
Use plone.app.multilingual conditionally (#1643)
Browse files Browse the repository at this point in the history
* Removed the hard code dependencies on plone.app.multilingual, now it is
being used conditionally

* Add changelog

* Code formatting

* Update src/plone/restapi/services/configure.zcml

Co-authored-by: Jens W. Klein <jk@kleinundpartner.at>

* Zprettied

* Update test_documentation.py

* cleanup

* remove leftovers for Plone 4 BBB while at it

---------

Co-authored-by: Jens W. Klein <jk@kleinundpartner.at>
Co-authored-by: Timo Stollenwerk <tisto@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 12, 2023
1 parent cff29fd commit 2100a45
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 36 deletions.
2 changes: 2 additions & 0 deletions news/1639.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove the hard code dependency by plone.app.multilingual, use it conditionaly instead
[@folix-01]
7 changes: 7 additions & 0 deletions src/plone/restapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
from Products.PluggableAuthService.PluggableAuthService import registerMultiPlugin
from zope.i18nmessageid import MessageFactory

import pkg_resources

try:
pkg_resources.get_distribution("plone.app.multilingual")
HAS_MULTILINGUAL = True
except pkg_resources.DistributionNotFound:
HAS_MULTILINGUAL = False

_ = MessageFactory("plone.restapi")
PROJECT_NAME = "plone.restapi"
Expand Down
7 changes: 3 additions & 4 deletions src/plone/restapi/services/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@
package=".workingcopy"
zcml:condition="installed plone.app.iterate"
/>
<include
package=".multilingual"
zcml:condition="have plone-5"
/>
<configure zcml:condition="installed plone.app.multilingual">
<include package=".multilingual" />
</configure>
<include
package=".email_notification"
zcml:condition="have plone-5"
Expand Down
10 changes: 7 additions & 3 deletions src/plone/restapi/services/content/add.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from AccessControl import getSecurityManager
from Acquisition import aq_base
from Acquisition.interfaces import IAcquirer
from plone.app.multilingual.interfaces import IPloneAppMultilingualInstalled
from plone.app.multilingual.interfaces import ITranslationManager
from plone.restapi import HAS_MULTILINGUAL
from plone.restapi.bbb import safe_hasattr
from plone.restapi.deserializer import json_body
from plone.restapi.exceptions import DeserializationError
Expand All @@ -23,6 +22,10 @@

import plone.protect.interfaces

if HAS_MULTILINGUAL:
from plone.app.multilingual.interfaces import IPloneAppMultilingualInstalled
from plone.app.multilingual.interfaces import ITranslationManager


class FolderPost(Service):
"""Creates a new content object."""
Expand Down Expand Up @@ -94,7 +97,8 @@ def reply(self):

# Link translation given the translation_of property
if (
IPloneAppMultilingualInstalled.providedBy(self.request)
HAS_MULTILINGUAL
and IPloneAppMultilingualInstalled.providedBy(self.request)
and translation_of
and language
):
Expand Down
6 changes: 0 additions & 6 deletions src/plone/restapi/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def tearDown(self):


class PloneRestApiDXLayer(PloneSandboxLayer):

defaultBases = (DATE_TIME_FIXTURE, PLONE_APP_CONTENTTYPES_FIXTURE)

def setUpZope(self, app, configurationContext):
Expand Down Expand Up @@ -159,7 +158,6 @@ def setUpPloneSite(self, portal):


class PloneRestApiTestWorkflowsLayer(PloneSandboxLayer):

defaultBases = (PLONE_RESTAPI_DX_FIXTURE,)

def setUpPloneSite(self, portal):
Expand All @@ -174,7 +172,6 @@ def setUpPloneSite(self, portal):


class PloneRestApiDXPAMLayer(PloneSandboxLayer):

defaultBases = (DATE_TIME_FIXTURE, PLONE_APP_CONTENTTYPES_FIXTURE)

def setUpZope(self, app, configurationContext):
Expand Down Expand Up @@ -217,7 +214,6 @@ def setUpPloneSite(self, portal):
if PloneAppCachingBase is not None:
# condition and fallback can be removed in a Plone 6.0 only scenario
class PloneRestApiCachingLayer(PloneAppCachingBase):

defaultBases = [
PLONE_RESTAPI_DX_PAM_FIXTURE,
]
Expand All @@ -240,7 +236,6 @@ class PloneRestApiCachingLayer(PloneAppCachingBase):


class PloneRestApiDXIterateLayer(PloneSandboxLayer):

defaultBases = (PLONEAPPITERATEDEX_FIXTURE,)

def setUpZope(self, app, configurationContext):
Expand All @@ -264,7 +259,6 @@ def setUpZope(self, app, configurationContext):


class PloneRestApIBlocksLayer(PloneSandboxLayer):

defaultBases = (PLONE_RESTAPI_DX_FIXTURE,)

def setUpPloneSite(self, portal):
Expand Down
19 changes: 11 additions & 8 deletions src/plone/restapi/tests/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from plone.app.discussion.interfaces import IDiscussionSettings
from plone.app.discussion.interfaces import IReplies
from plone.app.multilingual.interfaces import ITranslationManager
from plone.app.testing import applyProfile
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
Expand Down Expand Up @@ -35,6 +36,7 @@
from plone.app.testing import pushGlobalRegistry
from plone.restapi.testing import register_static_uuid_utility
from zope.component.hooks import getSite

import collections
import json
import os
Expand Down Expand Up @@ -213,7 +215,6 @@ def tearDown(self):


class TestDocumentation(TestDocumentationBase):

layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

def setUp(self):
Expand Down Expand Up @@ -1746,7 +1747,6 @@ def test_site_get(self):


class TestDocumentationMessageTranslations(TestDocumentationBase):

layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

def setUp(self):
Expand Down Expand Up @@ -1803,7 +1803,6 @@ def test_translate_messages_addons(self):


class TestCommenting(TestDocumentationBase):

layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

def setUp(self):
Expand Down Expand Up @@ -2072,7 +2071,6 @@ def test_aliases_root_filter(self):


class TestControlPanelDocumentation(TestDocumentationBase):

layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

def test_controlpanels_get_listing(self):
Expand Down Expand Up @@ -2130,13 +2128,20 @@ def test_controlpanels_crud_dexterity(self):


class TestPAMDocumentation(TestDocumentationBase):

layer = PLONE_RESTAPI_DX_PAM_FUNCTIONAL_TESTING

def setUp(self):
super().setUp()

#
language_tool = api.portal.get_tool("portal_languages")
language_tool.addSupportedLanguage("en")
language_tool.addSupportedLanguage("es")
language_tool.addSupportedLanguage("de")
if api.portal.get().portal_setup.profileExists(
"plone.app.multilingual:default"
):
applyProfile(self.portal, "plone.app.multilingual:default")

# We manually set the UIDs for LRFs here because the static uuid
# generator is not applied for LRFs.
# When we have tried to apply it for LRFs we have had several
Expand Down Expand Up @@ -2248,7 +2253,6 @@ def test_site_expansion_navroot_language_folder_content(self):


class TestIterateDocumentation(TestDocumentationBase):

layer = PLONE_RESTAPI_ITERATE_FUNCTIONAL_TESTING

def setUp(self):
Expand Down Expand Up @@ -2352,7 +2356,6 @@ def test_documentation_schema_user(self):


class TestRules(TestDocumentationBase):

layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

def setUp(self):
Expand Down
33 changes: 27 additions & 6 deletions src/plone/restapi/tests/test_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.dexterity.utils import createContentInContainer
from plone.restapi import HAS_MULTILINGUAL
from plone.restapi.bbb import ILanguage
from plone.restapi.testing import PLONE_RESTAPI_DX_PAM_FUNCTIONAL_TESTING
from plone.restapi.testing import PLONE_RESTAPI_DX_PAM_INTEGRATION_TESTING
Expand All @@ -17,10 +18,12 @@


class TestTranslationInfo(unittest.TestCase):

layer = PLONE_RESTAPI_DX_PAM_INTEGRATION_TESTING

def setUp(self):
if not HAS_MULTILINGUAL:
return self.skipTest("The plone.app.multilingual is not installed")

self.portal = self.layer["portal"]
self.request = self.layer["request"]
alsoProvides(self.layer["request"], IPloneAppMultilingualInstalled)
Expand All @@ -35,7 +38,8 @@ def setUp(self):

def test_translation_info_includes_translations(self):
tinfo = getMultiAdapter(
(self.en_content, self.request), name="GET_application_json_@translations"
(self.en_content, self.request),
name="GET_application_json_@translations",
)

info = tinfo.reply()
Expand All @@ -44,7 +48,8 @@ def test_translation_info_includes_translations(self):

def test_correct_translation_information(self):
tinfo = getMultiAdapter(
(self.en_content, self.request), name="GET_application_json_@translations"
(self.en_content, self.request),
name="GET_application_json_@translations",
)

info = tinfo.reply()
Expand All @@ -56,7 +61,8 @@ def test_correct_translation_information(self):

def test_translation_info_includes_root_translations(self):
tinfo = getMultiAdapter(
(self.en_content, self.request), name="GET_application_json_@translations"
(self.en_content, self.request),
name="GET_application_json_@translations",
)

info = tinfo.reply()
Expand All @@ -68,6 +74,9 @@ class TestLinkContentsAsTranslations(unittest.TestCase):
layer = PLONE_RESTAPI_DX_PAM_FUNCTIONAL_TESTING

def setUp(self):
if not HAS_MULTILINGUAL:
return self.skipTest("The plone.app.multilingual is not installed")

self.portal = self.layer["portal"]
self.request = self.layer["request"]
alsoProvides(self.layer["request"], IPloneAppMultilingualInstalled)
Expand Down Expand Up @@ -171,7 +180,9 @@ def test_get_translations_on_content_with_no_permissions(self):
response = response.json()
self.assertTrue(len(response["items"]) == 0)

def test_link_translation_with_an_already_translated_content_returns_400(self):
def test_link_translation_with_an_already_translated_content_returns_400(
self,
):
ITranslationManager(self.en_content).register_translation("es", self.es_content)
transaction.commit()
response = requests.post(
Expand Down Expand Up @@ -228,6 +239,9 @@ class TestUnLinkContentTranslations(unittest.TestCase):
layer = PLONE_RESTAPI_DX_PAM_FUNCTIONAL_TESTING

def setUp(self):
if not HAS_MULTILINGUAL:
return self.skipTest("The plone.app.multilingual is not installed")

self.portal = self.layer["portal"]
self.request = self.layer["request"]
alsoProvides(self.layer["request"], IPloneAppMultilingualInstalled)
Expand All @@ -252,7 +266,8 @@ def test_translation_unlinking_succeeds(self):
transaction.begin()
manager = ITranslationManager(self.en_content)
self.assertNotIn(
ILanguage(self.es_content).get_language(), list(manager.get_translations())
ILanguage(self.es_content).get_language(),
list(manager.get_translations()),
)

def test_calling_endpoint_without_language_gives_400(self):
Expand Down Expand Up @@ -293,6 +308,9 @@ class TestCreateContentsAsTranslations(unittest.TestCase):
layer = PLONE_RESTAPI_DX_PAM_FUNCTIONAL_TESTING

def setUp(self):
if not HAS_MULTILINGUAL:
return self.skipTest("The plone.app.multilingual is not installed")

self.portal = self.layer["portal"]
self.request = self.layer["request"]
alsoProvides(self.layer["request"], IPloneAppMultilingualInstalled)
Expand Down Expand Up @@ -332,6 +350,9 @@ class TestTranslationLocator(unittest.TestCase):
layer = PLONE_RESTAPI_DX_PAM_FUNCTIONAL_TESTING

def setUp(self):
if not HAS_MULTILINGUAL:
return self.skipTest("The plone.app.multilingual is not installed")

self.portal = self.layer["portal"]
self.portal_url = self.portal.absolute_url()
self.request = self.layer["request"]
Expand Down
15 changes: 6 additions & 9 deletions src/plone/restapi/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@

from collections import OrderedDict
from copy import copy
from plone.app.multilingual.dx.interfaces import MULTILINGUAL_KEY
from plone.autoform.form import AutoExtensibleForm
from plone.autoform.interfaces import IParameterizedWidget
from plone.autoform.interfaces import WIDGETS_KEY
from plone.behavior.interfaces import IBehavior
from plone.dexterity.interfaces import IDexterityContent
from plone.dexterity.interfaces import IDexterityFTI
from plone.dexterity.utils import getAdditionalSchemata
from plone.dexterity.schema import splitSchemaName
from plone.i18n.normalizer import idnormalizer
from plone.restapi.interfaces import IFieldDeserializer
from plone.restapi.serializer.converters import IJsonCompatible
from plone.restapi.types.interfaces import IJsonSchemaProvider
from plone.restapi import HAS_MULTILINGUAL
from plone.supermodel import serializeModel
from plone.supermodel.interfaces import FIELDSETS_KEY
from plone.supermodel.utils import mergedTaggedValueDict
Expand All @@ -43,14 +44,8 @@
from zope.interface import implementer
from zope.schema.interfaces import IVocabularyFactory


try:
# Plone 5.1+
from plone.dexterity.schema import splitSchemaName
except ImportError:
# Plone 4.3
from plone.dexterity.utils import splitSchemaName

if HAS_MULTILINGUAL:
from plone.app.multilingual.dx.interfaces import MULTILINGUAL_KEY

_marker = [] # Create a new marker object.

Expand Down Expand Up @@ -203,6 +198,8 @@ def get_widget_params(schemas):


def get_multilingual_directives(schemas):
if not HAS_MULTILINGUAL:
return {}
params = {}
for schema in schemas:
if not schema:
Expand Down

0 comments on commit 2100a45

Please sign in to comment.