Skip to content

Commit

Permalink
added function get_allow_discussion_value (#1674)
Browse files Browse the repository at this point in the history
* added allow_discussion funtion

* added log

* added else condition

* added back test

* Handle IContentish interface implementation in get_allow_discussion_value

* formatted using black

* imported IContentish Interface

* removed the else condition

* corrected else statement

* added discussion test to portal

* added testSerializer

* added function

* formatted

* test name changes to prevent flake8 fail

* made changes allow_discussion_portal_function

* removed flake 8 error

* update the requested version

* removed the portal error
  • Loading branch information
Akshat2Jain authored Feb 12, 2024
1 parent 56d00c0 commit 7833026
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
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
16 changes: 16 additions & 0 deletions src/plone/restapi/tests/test_dxcontent_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
from zope.component import queryUtility
from zope.interface import Interface
from zope.publisher.interfaces.browser import IBrowserRequest
from importlib import import_module

import json
import unittest

HAS_PLONE_61 = getattr(
import_module("Products.CMFPlone.factory"), "PLONE61MARKER", False
)


class AdapterCM:
"""Context manager that will temporarily register an adapter"""
Expand Down Expand Up @@ -498,6 +503,17 @@ def test_allow_discussion_obj_instance_allows_global_enabled(self):
self.assertIn("allow_discussion", obj)
self.assertEqual(True, obj["allow_discussion"])

@unittest.skipUnless(
HAS_PLONE_61, "Skipping test for Plone versions earlier than 6.1"
)
def test_allow_discussion_portal_default(self):
"""Not globally addable, not fti enabled, not obj instance enabled"""
serializer = getMultiAdapter((self.portal, self.request), ISerializeToJson)
obj = serializer()

self.assertIn("allow_discussion", obj)
self.assertEqual(False, obj["allow_discussion"])

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

0 comments on commit 7833026

Please sign in to comment.