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

added function get_allow_discussion_value #1674

Merged
merged 23 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions news/1674.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `allow_discussion` serialization for the Plone Site, to return a boolean like other content types. @Akshat2Jain
17 changes: 14 additions & 3 deletions src/plone/restapi/serializer/dxcontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from plone.supermodel.utils import mergedTaggedValueDict
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import base_hasattr
from Products.CMFCore.interfaces import IContentish
from zope.component import adapter
from zope.component import getMultiAdapter
from zope.component import queryMultiAdapter
Expand All @@ -39,6 +40,18 @@
WorkingCopyInfo = None


def get_allow_discussion_value(context, request, result):
# This test is to handle the plone.app.discussion not being installed situation
if "allow_discussion" in result:
# Check if the content item implements the IContentish interface
if IContentish.providedBy(context):
result["allow_discussion"] = getMultiAdapter(
(context, request), name="conversation_view"
).enabled()
else:
result["allow_discussion"] = False


@implementer(ISerializeToJson)
@adapter(IDexterityContent, Interface)
class SerializeToJson:
Expand Down Expand Up @@ -125,9 +138,7 @@ def __call__(self, version=None, include_items=True):
if target_url:
result["targetUrl"] = target_url

result["allow_discussion"] = getMultiAdapter(
(self.context, self.request), name="conversation_view"
).enabled()
get_allow_discussion_value(self.context, self.request, result)

return result

Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/serializer/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from zope.interface import Interface
from zope.schema import getFields
from zope.security.interfaces import IPermission
from plone.restapi.serializer.dxcontent import get_allow_discussion_value

import json

Expand Down Expand Up @@ -121,6 +122,8 @@ def __call__(self, version=None):
for brain in batch
]

get_allow_discussion_value(self.context, self.request, result)

return result

def check_permission(self, permission_name, obj):
Expand Down
21 changes: 21 additions & 0 deletions src/plone/restapi/tests/test_dxcontent_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from plone.restapi.serializer.utils import get_portal_type_title
from plone.uuid.interfaces import IMutableUUID
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import get_installer
from zope.component import getGlobalSiteManager
from zope.component import getMultiAdapter
from zope.component import provideAdapter
Expand Down Expand Up @@ -498,6 +499,26 @@ def test_allow_discussion_obj_instance_allows_global_enabled(self):
self.assertIn("allow_discussion", obj)
self.assertEqual(True, obj["allow_discussion"])

def isPlone61OrAbove(self):
"""Check if the Plone version is 6.1 or above."""
installer = get_installer(self.portal)
return installer.getVersion() >= "6.1"

def test_allow_discussion_portal_default(self):
"""Not globally addable, not fti enabled, not obj instance enabled"""
if self.isPlone61OrAbove():
# Test for Plone 6.1 and above
self.portal.invokeFactory("Document", id="doc2")
serializer = getMultiAdapter(
(self.portal.doc2, self.request), ISerializeToJson
)
obj = serializer()

self.assertIn("allow_discussion", obj)
self.assertEqual(False, obj["allow_discussion"])
else:
self.skipTest("Test skipped for Plone versions earlier than 6.1")

def test_allow_discussion_obj_instance_not_set_global_enabled(self):
self.portal.invokeFactory("Document", id="doc2")
registry = queryUtility(IRegistry)
Expand Down