Skip to content

Commit

Permalink
add test and fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-bellenghi committed Jul 10, 2024
1 parent 71ad458 commit 48a0428
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/redturtle/volto/adapters/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@
/>

<adapter
factory=".rss.DesignPloneContentTypeItem"
factory=".rss.CustomFeedItem"
provides="plone.base.interfaces.syndication.IFeedItem"
for="design.plone.contenttypes.interfaces.IDesignPloneContentType
for="redturtle.volto.interfaces.ICustomFeedItem
plone.base.interfaces.syndication.IFeed"
/>
</configure>
13 changes: 7 additions & 6 deletions src/redturtle/volto/adapters/rss.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from DateTime import DateTime
from design.plone.contenttypes.interfaces import IDesignPloneContentType
from plone import api
from plone.api.exc import InvalidParameterError
from plone.app.contenttypes.behaviors.leadimage import ILeadImageBehavior
Expand All @@ -8,6 +7,7 @@
from plone.namedfile.interfaces import INamedField
from plone.rfc822.interfaces import IPrimaryFieldInfo
from plone.volto.behaviors.preview import IPreview
from redturtle.volto.interfaces import ICustomFeedItem


try:
Expand All @@ -19,8 +19,8 @@
from zope.component import adapter


@adapter(IDesignPloneContentType, IFeed)
class DesignPloneContentTypeItem(DexterityItem):
@adapter(ICustomFeedItem, IFeed)
class CustomFeedItem(DexterityItem):

def _has_valid_image(self, behavior, field_name):
if not behavior:
Expand All @@ -38,6 +38,7 @@ def _has_valid_image(self, behavior, field_name):
def __init__(self, context, feed):
super().__init__(context, feed)
self.dexterity = IDexterityContent.providedBy(context)
self.img_choice = None
self.file = None
self.field_name = None
self._set_image_field()
Expand All @@ -50,7 +51,7 @@ def _set_image_field(self):
# if we don't have the record gently fallback on the old behavior
try:
img_choice = api.portal.get_registry_record(
"redturtle.volto.rss_image_choice", "image"
"redturtle.volto.rss_image_choice"
)
except InvalidParameterError:
# seems the default of get_registry_record is not working
Expand All @@ -64,7 +65,7 @@ def _set_image_field(self):
elif img_choice == "image" and self._has_valid_image(lead, "image"):
self.file = lead.image
self.field_name = "image"
elif img_choice == "like_listing":
elif img_choice == "listing_like":
if self._has_valid_image(preview, "preview_image"):
self.file = preview.image
self.field_name = "preview_image"
Expand All @@ -87,7 +88,7 @@ def _set_primary_field(self):


@adapter(IEvent, IFeed)
class EventItem(DesignPloneContentTypeItem):
class EventItem(CustomFeedItem):
@property
def startdate(self):
"""
Expand Down
14 changes: 14 additions & 0 deletions src/redturtle/volto/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@
handler=".events.manage_auth_token"
/>

<!-- provide interface to News-->
<class class="plone.volto.content.FolderishNewsItem">
<implements interface="redturtle.volto.interfaces.ICustomFeedItem" />
</class>
<!-- <class class="plone.volto.content.FolderishEvent">
<implements interface="redturtle.volto.interfaces.ICustomFeedItem" />
</class> -->
<class class="plone.volto.content.FolderishDocument">
<implements interface="redturtle.volto.interfaces.ICustomFeedItem" />
</class>
<class class="plone.app.contenttypes.content.Link">
<implements interface="redturtle.volto.interfaces.ICustomFeedItem" />
</class>

</configure>
7 changes: 7 additions & 0 deletions src/redturtle/volto/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from plone.dexterity.interfaces import IDexterityContent
from plone.restapi.controlpanels.interfaces import IControlpanel
from redturtle.volto import _
from zope.interface import Interface
Expand Down Expand Up @@ -40,3 +41,9 @@ class IRedTurtleVoltoSettings(Interface):
default=False,
required=False,
)


class ICustomFeedItem(IDexterityContent):
"""
Marker interface for custom feed items.
"""
1 change: 1 addition & 0 deletions src/redturtle/volto/profiles/default/registry/rss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<value>thumb</value>
</record>
<record name="redturtle.volto.rss_image_choice">
<value>image</value>
<field type="plone.registry.field.Choice">
<title>RSS Image Choice</title>
<values>
Expand Down
2 changes: 1 addition & 1 deletion src/redturtle/volto/profiles/uninstall/controlpanel.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<object name="portal_controlpanel"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="design.plone.contenttypes">
i18n:domain="redturtle.volto">

<configlet
title="RedTurtle Volto Settings"
Expand Down
94 changes: 90 additions & 4 deletions src/redturtle/volto/tests/test_rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,119 @@
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from redturtle.volto.adapters.rss import CustomFeedItem
from redturtle.volto.interfaces import ICustomFeedItem


try:
from plone.base.interfaces.syndication import IFeed
except ModuleNotFoundError:
from Products.CMFPlone.interfaces.syndication import IFeed

from plone.namedfile.file import NamedBlobImage
from redturtle.volto.testing import REDTURTLE_VOLTO_API_FUNCTIONAL_TESTING

import os
import unittest


class TestContentTypeLink(unittest.TestCase):
class TestCustomRSSFeed(unittest.TestCase):
layer = REDTURTLE_VOLTO_API_FUNCTIONAL_TESTING

def setUp(self):
self.app = self.layer["app"]
self.portal = self.layer["portal"]
old_behavior = self.portal["portal_types"]["News Item"].behaviors
self.portal["portal_types"]["News Item"].behaviors = old_behavior + (
"volto.preview_image",
)
self.portal_url = self.portal.absolute_url()
self.feed = IFeed(self.portal)
setRoles(self.portal, TEST_USER_ID, ["Manager"])

filename = os.path.join(os.path.dirname(__file__), "logo.png")
self.news1 = api.content.create(
container=self.portal,
type="News Item",
title="News 1",
description="",
)
self.news1.image = NamedBlobImage(
data=open(filename, "rb").read(),
filename="logo.png",
contentType="image/png",
)

self.news2 = api.content.create(
container=self.portal,
type="News Item",
title="News 2",
description="",
)
self.news2.preview_image = NamedBlobImage(
data=open(filename, "rb").read(),
filename="logo.png",
contentType="image/png",
)

def test_rss_item(self):
import pdb
self.news3 = api.content.create(
container=self.portal,
type="News Item",
title="News 3",
description="",
)
self.news3.image = NamedBlobImage(
data=open(filename, "rb").read(),
filename="image.png",
contentType="image/png",
)
self.news3.preview_image = NamedBlobImage(
data=open(filename, "rb").read(),
filename="preview.png",
contentType="image/png",
)

def test_assert_news_has_volto_preview_image(self):
self.assertTrue(
"volto.preview_image" in self.portal["portal_types"]["News Item"].behaviors
)

def test_rss_item_iface_provided(self):
self.assertTrue(ICustomFeedItem.providedBy(self.news1))
self.assertTrue(ICustomFeedItem.providedBy(self.news2))

def test_rss_item_field_name_image(self):

adapter_news_1 = CustomFeedItem(self.news1, self.feed)
adapter_news_2 = CustomFeedItem(self.news2, self.feed)

# news 1 has image feld
self.assertEqual(adapter_news_1.field_name, "image")

# news 2 has the preview image field compiled, so has the default options
# is to use image field field_name will be None
self.assertEqual(adapter_news_2.field_name, None)

def test_rss_item_field_name_preview_image(self):

api.portal.set_registry_record(
"redturtle.volto.rss_image_choice", "preview_image"
)
adapter_news_1 = CustomFeedItem(self.news1, self.feed)
adapter_news_2 = CustomFeedItem(self.news2, self.feed)

self.assertEqual(adapter_news_1.field_name, None)
self.assertEqual(adapter_news_2.field_name, "preview_image")

def test_rss_item_field_name_like_listing(self):
api.portal.set_registry_record(
"redturtle.volto.rss_image_choice", "listing_like"
)
adapter_news_1 = CustomFeedItem(self.news1, self.feed)
adapter_news_2 = CustomFeedItem(self.news2, self.feed)
adapter_news_3 = CustomFeedItem(self.news3, self.feed)

pdb.set_trace()
self.assertEqual(adapter_news_1.field_name, "image")
self.assertEqual(adapter_news_2.field_name, "preview_image")
# image 3 has both image and preview_image, so the preview one is used
self.assertEqual(adapter_news_3.field_name, "preview_image")

0 comments on commit 48a0428

Please sign in to comment.