Commit ceca5d61 authored by Mame Coumba Sall's avatar Mame Coumba Sall

-Remove Conversion API in Document.py as it is now in mixin/convertable

-Modified to include methods that returns allowed target item list for
conversion


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@34092 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 6993cc96
This diff is collapsed.
...@@ -53,6 +53,8 @@ except ImportError: ...@@ -53,6 +53,8 @@ except ImportError:
from zLOG import LOG, WARNING from zLOG import LOG, WARNING
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
#Mixin import
from Products.ERP5.mixin.convertable import ConvertableMixin
default_displays_id_list = ('nano', 'micro', 'thumbnail', default_displays_id_list = ('nano', 'micro', 'thumbnail',
'xsmall', 'small', 'medium', 'xsmall', 'small', 'medium',
...@@ -60,7 +62,7 @@ default_displays_id_list = ('nano', 'micro', 'thumbnail', ...@@ -60,7 +62,7 @@ default_displays_id_list = ('nano', 'micro', 'thumbnail',
default_formats = ['jpg', 'jpeg', 'png', 'gif', 'pnm', 'ppm'] default_formats = ['jpg', 'jpeg', 'png', 'gif', 'pnm', 'ppm']
class Image(File, OFSImage): class Image(File, OFSImage, ConvertableMixin):
""" """
An Image is a File which contains image data. It supports An Image is a File which contains image data. It supports
various conversions of format, size, resolution through various conversions of format, size, resolution through
...@@ -322,11 +324,36 @@ class Image(File, OFSImage): ...@@ -322,11 +324,36 @@ class Image(File, OFSImage):
return mime_type, result return mime_type, result
# Conversion API # Conversion API
security.declareProtected(Permissions.View, 'getAllowedTargetItemList')
def getAllowedTargetItemList(self):
import commands
import re
import os
new_result = []
filename = os.path.abspath(self.getSourceReference())
result = commands.getstatusoutput('convert -list format %s ' % self.getSourceReference())
new_list = re.split('\n',result[1])
allowed = []
for new_str in new_list:
test_str = new_str.lstrip()
pattern = re.compile(r'''([A-z]+[*]?\s+[A-z]+\s+[rw+-]+\s+[A-z]+\s+[A-z]+\D+[A-z]+)''',re.VERBOSE)
if re.match(pattern,test_str):
new_result.append(test_str)
len_new_result = len(new_result)
for i in range(0,len_new_result):
allowed.append(list((new_result[i].split()[1].lower(),' '.join(new_result[i].split()[3:]))))
return [(y, x) for x, y in allowed]
security.declareProtected(Permissions.AccessContentsInformation, 'convert') security.declareProtected(Permissions.AccessContentsInformation, 'convert')
def convert(self, format, display=None, quality=75, resolution=None, frame=None, **kw): def convert(self, format, display=None, quality=75, resolution=None, frame=None, **kw):
""" """
Implementation of conversion for Image files Implementation of conversion for Image files
""" """
# Raise an error if the format is not permitted
if not self.isTargetFormatPermitted(format):
raise Unauthorized("User does not have enough permission to access document"
" in %s format" % (format or 'original'))
if format in ('text', 'txt', 'html', 'base_html', 'stripped-html'): if format in ('text', 'txt', 'html', 'base_html', 'stripped-html'):
try: try:
return self.getConversion(format=format) return self.getConversion(format=format)
...@@ -339,7 +366,7 @@ class Image(File, OFSImage): ...@@ -339,7 +366,7 @@ class Image(File, OFSImage):
if (display is not None or resolution is not None or quality != 75 or format != ''\ if (display is not None or resolution is not None or quality != 75 or format != ''\
or frame is not None) and image_size: or frame is not None) and image_size:
kw = dict(display=display, format=format, quality=quality, kw = dict(display=display, format=format, quality=quality,
resolution=resolution, frame=frame, image_size=image_size) resolution=resolution, frame=frame, image_size=image_size)
try: try:
mime, image = self.getConversion(**kw) mime, image = self.getConversion(**kw)
except KeyError: except KeyError:
...@@ -369,7 +396,7 @@ class Image(File, OFSImage): ...@@ -369,7 +396,7 @@ class Image(File, OFSImage):
# display may be set from a cookie (?) # display may be set from a cookie (?)
image_size = self.getSizeFromImageDisplay(display) image_size = self.getSizeFromImageDisplay(display)
kw = dict(display=display, format=format, quality=quality, kw = dict(display=display, format=format, quality=quality,
resolution=resolution, frame=frame, image_size=image_size) resolution=resolution, frame=frame, image_size=image_size)
_setCacheHeaders(_ViewEmulator().__of__(self), kw) _setCacheHeaders(_ViewEmulator().__of__(self), kw)
if (display is not None or resolution is not None or quality != 75 or format != ''\ if (display is not None or resolution is not None or quality != 75 or format != ''\
......
...@@ -37,7 +37,13 @@ from Products.ERP5.Document.Image import Image ...@@ -37,7 +37,13 @@ from Products.ERP5.Document.Image import Image
from Products.ERP5.Document.Document import ConversionError from Products.ERP5.Document.Document import ConversionError
from Products.ERP5.mixin.cached_convertable import CachedConvertableMixin from Products.ERP5.mixin.cached_convertable import CachedConvertableMixin
class PDFDocument(Image, CachedConvertableMixin):
from zLOG import LOG, WARNING
# Mixin import
from Products.ERP5.mixin.convertable import ConvertableMixin
class PDFDocument(Image, ConvertableMixin, CachedConvertableMixin):
""" """
PDFDocument is a subclass of Image which is able to PDFDocument is a subclass of Image which is able to
extract text content from a PDF file either as text extract text content from a PDF file either as text
...@@ -98,6 +104,11 @@ class PDFDocument(Image, CachedConvertableMixin): ...@@ -98,6 +104,11 @@ class PDFDocument(Image, CachedConvertableMixin):
resolution=resolution, frame=frame) resolution=resolution, frame=frame)
# Conversion API # Conversion API
security.declareProtected(Permissions.View, 'getAllowedTargetItemList')
def getAllowedTargetItemList(self):
return Image.getAllowedTargetItemList(self) + \
[('Text', 'txt'),('Plain Text','text'), ('HTML Document', 'html')]
security.declareProtected(Permissions.AccessContentsInformation, 'convert') security.declareProtected(Permissions.AccessContentsInformation, 'convert')
def convert(self, format, **kw): def convert(self, format, **kw):
""" """
......
...@@ -43,9 +43,12 @@ try: ...@@ -43,9 +43,12 @@ try:
except ImportError: except ImportError:
from Products.ERP5Type.patches.string import Template from Products.ERP5Type.patches.string import Template
# Mixin import
from Products.ERP5.mixin.convertable import ConvertableMixin
DEFAULT_TEXT_FORMAT = 'text/html' DEFAULT_TEXT_FORMAT = 'text/html'
class TextDocument(Document, TextContent): class TextDocument(Document, TextContent, ConvertableMixin):
""" """
A Document contains text which can be formatted using A Document contains text which can be formatted using
*Structured Text* or *HTML*. Text can be automatically translated *Structured Text* or *HTML*. Text can be automatically translated
...@@ -146,6 +149,10 @@ class TextDocument(Document, TextContent): ...@@ -146,6 +149,10 @@ class TextDocument(Document, TextContent):
if format is None: if format is None:
# The default is to use ERP5 Forms to render the page # The default is to use ERP5 Forms to render the page
return self.view() return self.view()
# Raise an error if the format is not permitted
if not self.isTargetFormatPermitted(format):
raise Unauthorized("User does not have enough permission to access document"
" in %s format" % (format or 'original'))
mime, data = self.convert(format=format) mime, data = self.convert(format=format)
RESPONSE.setHeader('Content-Length', len(str(data))) # XXX - Not efficient RESPONSE.setHeader('Content-Length', len(str(data))) # XXX - Not efficient
# if datastream instance # if datastream instance
...@@ -200,12 +207,25 @@ class TextDocument(Document, TextContent): ...@@ -200,12 +207,25 @@ class TextDocument(Document, TextContent):
substitution_method_parameter_dict = {} substitution_method_parameter_dict = {}
return self._substituteTextContent(subject, safe_substitute=safe_substitute, return self._substituteTextContent(subject, safe_substitute=safe_substitute,
**substitution_method_parameter_dict) **substitution_method_parameter_dict)
security.declareProtected(Permissions.View, 'getAllowedTargetItemList')
def getAllowedTargetItemList(self):
mime_type = getToolByName(self, 'mimetypes_registry')
allowed=[]
for extension in mime_type.extensions:
allowed.append((mime_type.extensions[extension].name(),extension))
return [(y, x) for x, y in allowed]
security.declareProtected(Permissions.AccessContentsInformation, 'convert') security.declareProtected(Permissions.AccessContentsInformation, 'convert')
def convert(self, format, substitution_method_parameter_dict=None, safe_substitute=True, **kw): def convert(self, format, substitution_method_parameter_dict=None, safe_substitute=True, **kw):
""" """
Convert text using portal_transforms or oood Convert text using portal_transforms or oood
""" """
# Raise an error if the format is not permitted
if not self.isTargetFormatPermitted(format):
raise Unauthorized("User does not have enough permission to access document"
" in %s format" % (format or 'original'))
# Accelerate rendering in Web mode # Accelerate rendering in Web mode
_setCacheHeaders(_ViewEmulator().__of__(self), {'format' : format}) _setCacheHeaders(_ViewEmulator().__of__(self), {'format' : format})
# Return the raw content # Return the raw content
......
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