Commit 99b7a652 authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Eteri

Transforms: add getAvailableTargetMimetypeList method

parent dcd188f7
......@@ -30,6 +30,7 @@ from Products.PortalTransforms.transforms import initialize
from Products.PortalTransforms.utils import log
from Products.PortalTransforms.utils import TransformException
from Products.PortalTransforms.utils import _www
from Products.PortalTransforms.utils import parseContentType
from ZODB.POSException import ConflictError
......@@ -651,5 +652,44 @@ class TransformTool(UniqueObject, ActionProviderBase, Folder):
available_types.append(input)
return available_types
security.declarePublic('getAvailableTargetMimetypeList')
def getAvailableTargetMimetypeList(self, source_mimetype):
"""
Returns a list of mimetypes that can be used as target for
`source_mimetype` conversion.
"""
# clean up mimetype from its useless characters
source_mimetype = parseContentType(source_mimetype)
source_mimetype = ";".join([source_mimetype.gettype()] + source_mimetype.getplist())
# fill dict that will contain all possible conversion for each mimetype
input_output_dict = {} # {"application/pdf": set(["text/html", "application/msword", ...])}
for obj in self.objectValues():
for input_ in obj.inputs:
if input_ in input_output_dict:
input_output_dict[input_].add(obj.output)
else:
input_output_dict[input_] = set([obj.output])
# browse mimetypes to fill result_set of available target mimetypes
result_set = set([source_mimetype])
browsing_list = [source_mimetype]
input_output_items = input_output_dict.items()
while len(browsing_list):
browsing_mimetype = browsing_list.pop()
for mimetype, output_mimetype_set in input_output_items:
if (browsing_mimetype == mimetype or
(mimetype.endswith("/*") and
browsing_mimetype[:len(mimetype[:-1])] == mimetype[:-1])
):
for output_mimetype in output_mimetype_set:
if output_mimetype not in result_set:
result_set.add(output_mimetype)
browsing_list.append(output_mimetype)
result_set.remove(source_mimetype)
return list(result_set)
InitializeClass(TransformTool)
registerToolInterface('portal_transforms', IPortalTransformsTool)
......@@ -2,6 +2,9 @@
"""some common utilities
"""
import mimetools
import cStringIO
class TransformException(Exception):
pass
......@@ -25,3 +28,18 @@ def safeToInt(value):
return int(value)
except (TypeError, ValueError):
return 0
def parseContentType(content_type):
"""Parses `text/plain;charset="utf-8"` to a mimetools.Message object.
Note: Content type or MIME type are built like `maintype/subtype[;params]`.
parsed_content_type = parseContentType('text/plain;charset="utf-8"')
parsed_content_type.gettype() -> 'text/plain'
parsed_content_type.getmaintype() -> 'text'
parsed_content_type.getsubtype() -> 'plain'
parsed_content_type.getplist() -> 'charset="utf-8"'
parsed_content_type.getparam('charset') -> 'utf-8'
parsed_content_type.typeheader -> 'text/plain;charset="utf-8"'
"""
return mimetools.Message(cStringIO.StringIO("Content-Type:" + content_type.replace("\r\n", "\r\n\t")))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment