-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from plone/preview-image-link
Added preview image link.
- Loading branch information
Showing
10 changed files
with
155 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- Added preview image link behavior (Plone 6+ only) | ||
[robgietema] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# -*- coding: utf-8 -*- | ||
from plone import api | ||
from plone.app.z3cform.widget import RelatedItemsFieldWidget | ||
from plone.autoform import directives | ||
from plone.autoform.interfaces import IFormFieldProvider | ||
from plone.base.interfaces import IImageScalesFieldAdapter | ||
from plone.rfc822.interfaces import IPrimaryFieldInfo | ||
from plone.supermodel import model | ||
from plone.volto import _ | ||
from z3c.form.util import getSpecification | ||
from z3c.relationfield.schema import RelationChoice | ||
from zope.component import adapter | ||
from zope.component import queryMultiAdapter | ||
from zope.interface import implementer | ||
from zope.interface import Interface | ||
from zope.interface import provider | ||
from zope.schema import TextLine | ||
|
||
|
||
@provider(IFormFieldProvider) | ||
class IPreviewLink(model.Schema): | ||
|
||
preview_image_link = RelationChoice( | ||
title=_("label_previewimage", default="Preview image"), | ||
description=_( | ||
"help_previewimage", | ||
default="Select an image that will be used in listing and teaser blocks.", | ||
), | ||
vocabulary="plone.app.vocabularies.Catalog", | ||
required=False, | ||
) | ||
|
||
directives.widget( | ||
"preview_image_link", | ||
RelatedItemsFieldWidget, | ||
frontendOptions={ | ||
"widget": "object_browser", | ||
"widgetProps": {"mode": "image", "return": "single"}, | ||
}, | ||
) | ||
|
||
preview_caption_link = TextLine( | ||
title=_("Preview image caption"), description="", required=False | ||
) | ||
|
||
|
||
@adapter(getSpecification(IPreviewLink["preview_image_link"]), Interface, Interface) | ||
@implementer(IImageScalesFieldAdapter) | ||
class PreviewImageScalesFieldAdapter: | ||
"""Get the image_scales for the preview_image_link field""" | ||
|
||
def __init__(self, field, context, request): | ||
self.field = field | ||
self.context = context | ||
self.request = request | ||
|
||
def __call__(self): | ||
value = self.field.get(self.context) | ||
linked_image = value.to_object | ||
primary_field = IPrimaryFieldInfo(linked_image).field | ||
serializer = queryMultiAdapter( | ||
(primary_field, linked_image, self.request), IImageScalesFieldAdapter | ||
) | ||
if serializer is not None: | ||
values = serializer() | ||
if values: | ||
portal_url = api.portal.get().absolute_url() | ||
base_path = linked_image.absolute_url().replace(portal_url, "") | ||
for value in values: | ||
value["base_path"] = base_path | ||
return values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from plone.namedfile.file import NamedBlobImage | ||
from plone.restapi.interfaces import ISerializeToJsonSummary | ||
from plone.volto.testing import PLONE_6 | ||
from plone.volto.testing import PLONE_VOLTO_CORE_INTEGRATION_TESTING | ||
from z3c.form.interfaces import IDataManager | ||
from zope.component import getMultiAdapter | ||
|
||
import unittest | ||
|
||
|
||
TEST_GIF = ( | ||
b"GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;" | ||
) | ||
|
||
|
||
@unittest.skipIf( | ||
not PLONE_6, | ||
"This test is only intended to run for Plone 6", | ||
) | ||
class TestPreviewLinkBehavior(unittest.TestCase): | ||
layer = PLONE_VOLTO_CORE_INTEGRATION_TESTING | ||
|
||
def setUp(self): | ||
from plone.volto.behaviors.preview_link import IPreviewLink | ||
|
||
self.app = self.layer["app"] | ||
self.portal = self.layer["portal"] | ||
self.request = self.layer["request"] | ||
self.catalog = self.portal.portal_catalog | ||
|
||
fti = self.portal.portal_types.Document | ||
fti.behaviors += ("volto.preview_image_link",) | ||
|
||
self.doc = self.portal[self.portal.invokeFactory("Document", id="doc1")] | ||
self.image = self.portal[ | ||
self.portal.invokeFactory("Image", id="image-1", title="Target image") | ||
] | ||
self.image.image = NamedBlobImage(data=TEST_GIF, filename="test.gif") | ||
dm = getMultiAdapter( | ||
(self.doc, IPreviewLink["preview_image_link"]), IDataManager | ||
) | ||
dm.set(self.image) | ||
self.doc.reindexObject() | ||
|
||
def test_image_scales_includes_preview_image_link(self): | ||
brain = self.catalog(UID=self.doc.UID())[0] | ||
summary = getMultiAdapter((brain, self.request), ISerializeToJsonSummary)() | ||
self.assertIn("preview_image_link", summary["image_scales"]) |