Skip to content

Commit

Permalink
Merge pull request #199 from plone/ale-improvements
Browse files Browse the repository at this point in the history
Do not break with unknown MIME types
  • Loading branch information
pysailor authored May 26, 2020
2 parents 5c5015c + 3f5b501 commit 3c2e37b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions news/197.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not break with unknown MIME types [ale-rt]
1 change: 1 addition & 0 deletions news/198.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Require mock only on Python 2.7 [ale-rt]
16 changes: 13 additions & 3 deletions plone/app/content/browser/vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from Products.CMFPlone.utils import safe_unicode
from Products.Five import BrowserView
from Products.MimetypesRegistry.MimeTypeItem import guess_icon_path
from Products.MimetypesRegistry.MimeTypeItem import PREFIX
from types import FunctionType
from z3c.form.interfaces import ISubForm
from zope.component import getUtility
Expand Down Expand Up @@ -250,9 +251,18 @@ def __call__(self):
getattr(vocab_item, 'mime_type', None))
if contenttype:
ctype = mtt.lookup(contenttype)
item[key] = '/'.join([
base_path,
guess_icon_path(ctype[0])])
if ctype:
item[key] = '/'.join([
base_path,
guess_icon_path(ctype[0])])
else:
item[key] = "/".join(
[
base_path,
PREFIX.rstrip("/"),
"unknown.png",
]
)
items.append(item)
else:
items = [{'id': item.value,
Expand Down
6 changes: 5 additions & 1 deletion plone/app/content/tests/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
from zope.component import getUtility

import json
import mock
import transaction
import unittest

try:
from unittest import mock
except ImportError:
import mock


class ContentsCopyTests(unittest.TestCase):
layer = PLONE_APP_CONTENT_DX_INTEGRATION_TESTING
Expand Down
48 changes: 43 additions & 5 deletions plone/app/content/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from mock import Mock
from plone.app.content.browser import vocabulary
from plone.app.content.browser.file import FileUploadView
from plone.app.content.browser.query import QueryStringIndexOptions
Expand All @@ -26,7 +25,6 @@
from zope.publisher.browser import TestRequest

import json
import mock
import os
import transaction

Expand All @@ -37,6 +35,11 @@
def processQueue():
pass

try:
from unittest import mock
except ImportError:
import mock


_dir = os.path.dirname(__file__)

Expand Down Expand Up @@ -388,7 +391,7 @@ def testSource(self):
class DummyCatalogSource(object):
def search_catalog(self, query):
querytext = query['SearchableText']['query']
return [Mock(id=querytext)]
return [mock.Mock(id=querytext)]

widget = TextWidget(self.request)
widget.context = self.portal
Expand Down Expand Up @@ -468,7 +471,7 @@ def testSourcePermissionDenied(self):
class DummyCatalogSource(object):
def search_catalog(self, query):
querytext = query['SearchableText']['query']
return [Mock(id=querytext)]
return [mock.Mock(id=querytext)]

widget = TextWidget(self.request)
widget.context = self.portal
Expand Down Expand Up @@ -503,7 +506,7 @@ def testSourceTextQuery(self):
@implementer(ISource)
class DummyCatalogSource(object):
def search(self, query):
return [Mock(value=Mock(id=query))]
return [mock.Mock(value=mock.Mock(id=query))]

widget = TextWidget(self.request)
widget.context = self.portal
Expand Down Expand Up @@ -565,6 +568,41 @@ def testUntranslatableMetadata(self):
# portal_type is never translated
self.assertEqual(data['results'][0]['portal_type'], u'Document')

def testGetMimeIcon(self):
""" Check if the returned icon is correct
"""
self.request.form.update(
{
"name": "plone.app.vocabularies.Catalog",
"attributes": ["getMimeIcon"],
}
)
view = VocabularyView(self.portal, self.request)

# Check an empty file
self.portal.invokeFactory("File", id="my-file", title="My file")
obj = self.portal["my-file"]
obj.reindexObject()

self.assertListEqual(
json.loads(view())["results"], [{"getMimeIcon": None}]
)

# mock a pdf
obj.file = mock.Mock(contentType="application/pdf")
obj.reindexObject()
self.assertListEqual(
json.loads(view())["results"],
[{"getMimeIcon": "/plone/++resource++mimetype.icons/pdf.png"}],
)

# mock something unknown
obj.file = mock.Mock(contentType="x-foo/x-bar")
obj.reindexObject()
self.assertListEqual(
json.loads(view())["results"],
[{"getMimeIcon": "/plone/++resource++mimetype.icons/unknown.png"}],
)

class FunctionalBrowserTest(unittest.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
test=[
'plone.app.contenttypes',
'plone.app.testing',
'mock'
'mock;python_version<"3.3"'
]
),
install_requires=[
Expand Down

0 comments on commit 3c2e37b

Please sign in to comment.