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

fix: contextnavigation when a file has an unknown type #1864

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/1864.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In the `@contextnavigation` endpoint, return `"icon": null` for Files with a mimetype not found in the `content_type_registry`, instead of raising `TypeError`. @mamico
3 changes: 2 additions & 1 deletion src/plone/restapi/services/contextnavigation/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ def getMimeTypeIcon(self, node):
mtt = getToolByName(self.context, "mimetypes_registry")
if fileo.contentType:
ctype = mtt.lookup(fileo.contentType)
return os.path.join(portal_url, guess_icon_path(ctype[0]))
if ctype:
return os.path.join(portal_url, guess_icon_path(ctype[0]))
except AttributeError:
pass

Expand Down
29 changes: 29 additions & 0 deletions src/plone/restapi/tests/test_services_contextnavigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.app.testing import TEST_USER_ID
from plone.namedfile.file import NamedBlobFile
from plone.registry.interfaces import IRegistry
from plone.restapi.services.contextnavigation.get import ContextNavigation
from plone.restapi.testing import PLONE_RESTAPI_DX_FUNCTIONAL_TESTING
Expand Down Expand Up @@ -99,6 +100,9 @@ def populateSite(self):
folder2.invokeFactory("Document", "doc22")
folder2.invokeFactory("Document", "doc23")
folder2.invokeFactory("File", "file21")
folder2.file21.file = NamedBlobFile(
data="Hello World", contentType="text/plain", filename="file.txt"
)
folder2.invokeFactory("Folder", "folder21")
folder21 = getattr(folder2, "folder21")
folder21.invokeFactory("Document", "doc211")
Expand Down Expand Up @@ -996,3 +1000,28 @@ def testContextNavigation(self):
"/plone/folder1/doc11",
)
)

def testIcon(self):
view = self.renderer(
self.portal.folder2.file21,
opts(root_path="/folder2", topLevel=0),
)
tree = view.getNavTree()
self.assertTrue(tree)
self.assertEqual(
tree["items"][0]["icon"],
"/plone/++resource++mimetype.icons/txt.png",
)

def testIconNotRegisteredMimetype(self):
self.portal.folder2.file21.file.contentType = "plain/x-text"
view = self.renderer(
self.portal.folder2.file21,
opts(root_path="/folder2", topLevel=0),
)
tree = view.getNavTree()
self.assertTrue(tree)
self.assertEqual(
tree["items"][0]["icon"],
None,
)
Loading