Skip to content

Commit

Permalink
Add helper function to get the human representation of a mime-type on…
Browse files Browse the repository at this point in the history
… Dexterity-based content types
  • Loading branch information
hvelarde committed Oct 22, 2016
1 parent e23fed6 commit 2b2b1e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -6,6 +6,10 @@ There's a frood who really knows where his towel is.
1.3b2 (unreleased)
^^^^^^^^^^^^^^^^^^

- Add helper function to get the human representation of a mime-type on Dexterity-based content types.
This fixed an ``AttributeError`` that was causing an exception on Plone 5.
[hvelarde]

- We now get the types that use the view action in listings in Plone 5 also.
[hvelarde]

Expand Down
19 changes: 18 additions & 1 deletion src/collective/cover/tiles/file.py
Expand Up @@ -37,6 +37,23 @@ def get_download_html(url, portal_url, icon, mime_type, size):
return HTML.format(url, portal_url, icon, mime_type, size_str)


def lookupMime(obj, name):
"""Given an id, return the human representation of mime-type.
This is a helper funtion to deal with API inconsistencies.
It's based on a simplified version of the `lookupMime` script
included in Products.Archetypes `archetypes` skin.
"""
mtr = api.portal.get_tool('mimetypes_registry')
try:
mimetypes = mtr.lookup(name)
except MimeTypeException:
return None

if len(mimetypes):
return mimetypes[0].name()
return name


class IFileTile(IPersistentCoverTile):

title = schema.TextLine(
Expand Down Expand Up @@ -117,7 +134,7 @@ def download_widget(self):
except AttributeError:
# Dexterity
icon = self.getBestIcon(obj)
mime = obj.lookupMime(content_type)
mime = lookupMime(obj, content_type)
size = obj.file.size

return get_download_html(url, portal_url, icon, mime, size)
Expand Down

0 comments on commit 2b2b1e0

Please sign in to comment.