Skip to content

Commit

Permalink
Merge pull request #4024 from plone/fix-get-resource
Browse files Browse the repository at this point in the history
[6.1] Restore the capability of the resource registry to serve OFS files as resources
  • Loading branch information
thet authored Oct 9, 2024
2 parents 16a6741 + 6700390 commit ad248d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Products/CMFPlone/resources/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from Acquisition import aq_base
from Acquisition import aq_inner
from Acquisition import aq_parent
from OFS.Image import File
from plone.base.interfaces.resources import OVERRIDE_RESOURCE_DIRECTORY_NAME
from plone.resource.file import FilesystemFile
from plone.resource.interfaces import IResourceDirectory
Expand Down Expand Up @@ -75,9 +76,15 @@ def get_resource(context, path):
if hasattr(aq_base(resource), "GET"):
# for FileResource
result = resource.GET()
else:
elif isinstance(resource, File):
# An OFS.Image.File object
result = resource.data
elif callable(resource):
# any BrowserView
result = resource()
else:
logger.info("Cannot get data from resource %r", resource)
result = b""
context.REQUEST.response = response_before
return result

Expand Down
26 changes: 26 additions & 0 deletions Products/CMFPlone/tests/testResourceRegistries.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from OFS.Image import File
from plone.app.testing import logout
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
Expand All @@ -13,6 +14,7 @@
from Products.CMFPlone.resources.browser.resource import REQUEST_CACHE_KEY
from Products.CMFPlone.resources.browser.resource import ScriptsView
from Products.CMFPlone.resources.browser.resource import StylesView
from Products.CMFPlone.resources.webresource import PloneScriptResource
from Products.CMFPlone.tests import PloneTestCase
from zope.component import getUtility

Expand Down Expand Up @@ -180,6 +182,30 @@ def test_bundle_depends_on_missing(self):
# bundle should be skipped when rendering
self.assertNotIn("http://foo.bar/foobar.js", results)

def test_resource_browser_static_resource(self):
resource = PloneScriptResource(self.portal, resource="++resource++plone-admin-ui.js")
self.assertIn(
b"window.onload", resource.file_data,
)

def test_resource_ofs_file(self):
self.portal["foo.js"] = File("foo.js", "Title", b'console.log()')
resource = PloneScriptResource(self.portal, resource="foo.js")
self.assertEqual(
resource.file_data, b'console.log()',
)

def test_resource_view(self):
resource = PloneScriptResource(self.portal, resource="@@ok")
self.assertEqual(
resource.file_data, b'OK',
)

def test_resource_bogus(self):
resource = PloneScriptResource(self.portal, resource="I_do_not_exist")
self.assertEqual(
resource.file_data, b'I_do_not_exist',
)

class TestStylesViewlet(PloneTestCase.PloneTestCase):
def test_styles_viewlet(self):
Expand Down
2 changes: 2 additions & 0 deletions news/4022.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Resource registry: Support OFS.Image.File objects.
[ale-rt, thet]

0 comments on commit ad248d7

Please sign in to comment.