Commit 42eb2b36 authored by Ivan Tyagov's avatar Ivan Tyagov

OOoDocument can use quality now. Add tests for using quality with various DMS types.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@37217 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 437481dc
...@@ -45,8 +45,9 @@ from Products.CMFCore.utils import getToolByName, _setCacheHeaders,\ ...@@ -45,8 +45,9 @@ from Products.CMFCore.utils import getToolByName, _setCacheHeaders,\
from Products.ERP5Type import Permissions, PropertySheet, Constraint from Products.ERP5Type import Permissions, PropertySheet, Constraint
from Products.ERP5Type.Cache import CachingMethod from Products.ERP5Type.Cache import CachingMethod
from Products.ERP5.Document.File import File from Products.ERP5.Document.File import File
from Products.ERP5.Document.Document import Document,\ from Products.ERP5.Document.Document import Document, \
VALID_IMAGE_FORMAT_LIST, ConversionError, NotConvertedError VALID_IMAGE_FORMAT_LIST, ConversionError, NotConvertedError
from Products.ERP5.Document.Image import getDefaultImageQuality
from AccessControl.SecurityManagement import setSecurityManager from AccessControl.SecurityManagement import setSecurityManager
from Products.ERP5Type.Utils import fill_args_from_request from Products.ERP5Type.Utils import fill_args_from_request
from zLOG import LOG, ERROR from zLOG import LOG, ERROR
...@@ -92,7 +93,7 @@ class TimeoutTransport(SafeTransport): ...@@ -92,7 +93,7 @@ class TimeoutTransport(SafeTransport):
class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixin, File, class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixin, File,
TextConvertableMixin, Document): TextConvertableMixin, Document):
""" """
A file document able to convert OOo compatible files to A file document able to convert OOo compatible files to
any OOo supported format, to capture metadata and to any OOo supported format, to capture metadata and to
...@@ -281,7 +282,7 @@ class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixi ...@@ -281,7 +282,7 @@ class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixi
return response_dict['mime'], Pdata(dec(response_dict['data'])) return response_dict['mime'], Pdata(dec(response_dict['data']))
# Conversion API # Conversion API
def _convert(self, format, display=None, **kw): def _convert(self, format, display=None, quality=_MARKER, **kw):
"""Convert the document to the given format. """Convert the document to the given format.
If a conversion is already stored for this format, it is returned If a conversion is already stored for this format, it is returned
...@@ -340,6 +341,9 @@ class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixi ...@@ -340,6 +341,9 @@ class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixi
# Raise an error if the format is not supported # Raise an error if the format is not supported
if not self.isTargetFormatAllowed(format): if not self.isTargetFormatAllowed(format):
raise ConversionError("OOoDocument: target format %s is not supported" % format) raise ConversionError("OOoDocument: target format %s is not supported" % format)
if quality is _MARKER:
# we need to determine quality before image conversion happens due to cache
quality = getDefaultImageQuality(self.getPortalObject(), format)
# Return converted file # Return converted file
if requires_pdf_first: if requires_pdf_first:
# We should use original_format whenever we wish to # We should use original_format whenever we wish to
...@@ -348,7 +352,7 @@ class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixi ...@@ -348,7 +352,7 @@ class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixi
if display is None: if display is None:
has_format = self.hasConversion(format=original_format) has_format = self.hasConversion(format=original_format)
else: else:
has_format = self.hasConversion(format=original_format, display=display) has_format = self.hasConversion(format=original_format, display=display, quality=quality)
elif display is None or original_format not in VALID_IMAGE_FORMAT_LIST: elif display is None or original_format not in VALID_IMAGE_FORMAT_LIST:
has_format = self.hasConversion(format=original_format) has_format = self.hasConversion(format=original_format)
else: else:
...@@ -386,20 +390,20 @@ class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixi ...@@ -386,20 +390,20 @@ class OOoDocument(OOoDocumentExtensibleTraversableMixin, BaseConvertableFileMixi
file_name=self.getId(), file_name=self.getId(),
temp_object=1) temp_object=1)
temp_image._setData(data) temp_image._setData(data)
# we care for first page only # we care for first page only but as well for image quality
mime, data = temp_image.convert(original_format, display=display, frame=0, **kw) mime, data = temp_image.convert(original_format, display=display, frame=0, quality=quality, **kw)
# store conversion # store conversion
if display is None: if display is None:
self.setConversion(data, mime, format=original_format) self.setConversion(data, mime, format=original_format)
else: else:
self.setConversion(data, mime, format=original_format, display=display) self.setConversion(data, mime, format=original_format, display=display, quality=quality)
if requires_pdf_first: if requires_pdf_first:
format = original_format format = original_format
if display is None or original_format not in VALID_IMAGE_FORMAT_LIST: if display is None or original_format not in VALID_IMAGE_FORMAT_LIST:
return self.getConversion(format=original_format) return self.getConversion(format=original_format)
else: else:
return self.getConversion(format=original_format, display=display) return self.getConversion(format=original_format, display=display, quality=quality)
security.declareProtected(Permissions.ModifyPortalContent, security.declareProtected(Permissions.ModifyPortalContent,
'_populateConversionCacheWithHTML') '_populateConversionCacheWithHTML')
......
...@@ -1900,6 +1900,8 @@ return 1 ...@@ -1900,6 +1900,8 @@ return 1
Test converting to image all Document portal types on traversal i.e.: Test converting to image all Document portal types on traversal i.e.:
- image_module/1?quality=100&display=xlarge&format=jpeg - image_module/1?quality=100&display=xlarge&format=jpeg
- document_module/1?quality=100&display=large&format=jpeg - document_module/1?quality=100&display=large&format=jpeg
- document_module/1?quality=10&display=large&format=jpeg
- document_module/1?display=large&format=jpeg
- etc ... - etc ...
""" """
# Create OOo document # Create OOo document
...@@ -1924,7 +1926,7 @@ return 1 ...@@ -1924,7 +1926,7 @@ return 1
width = int(preference_tool.getPreference(width_preference)) width = int(preference_tool.getPreference(width_preference))
return (width, height) return (width, height)
def getURLSize(uri, **kw): def getURLSizeList(uri, **kw):
# __ac=RVJQNVR5cGVUZXN0Q2FzZTo%3D is encoded ERP5TypeTestCase with empty password # __ac=RVJQNVR5cGVUZXN0Q2FzZTo%3D is encoded ERP5TypeTestCase with empty password
url = '%s?%s&__ac=%s' %(uri, urllib.urlencode(kw), 'RVJQNVR5cGVUZXN0Q2FzZTo%3D') url = '%s?%s&__ac=%s' %(uri, urllib.urlencode(kw), 'RVJQNVR5cGVUZXN0Q2FzZTo%3D')
format=kw.get('format', 'jpeg') format=kw.get('format', 'jpeg')
...@@ -1932,13 +1934,15 @@ return 1 ...@@ -1932,13 +1934,15 @@ return 1
# save as file with proper incl. format filename (for some reasons PIL uses this info) # save as file with proper incl. format filename (for some reasons PIL uses this info)
filename = "%s%stest-image-format-resize.%s" %(os.getcwd(), os.sep, format) filename = "%s%stest-image-format-resize.%s" %(os.getcwd(), os.sep, format)
f = open(filename, "w") f = open(filename, "w")
f.write(infile.read()) image_data = infile.read()
f.write(image_data)
f.close() f.close()
infile.close() infile.close()
file_size = len(image_data)
image = Image.open(filename) image = Image.open(filename)
image_size = image.size image_size = image.size
os.remove(filename) os.remove(filename)
return image_size return image_size, file_size
ooo_document_url = '%s/%s' %(self.portal.absolute_url(), ooo_document.getRelativeUrl()) ooo_document_url = '%s/%s' %(self.portal.absolute_url(), ooo_document.getRelativeUrl())
pdf_document_url = '%s/%s' %(self.portal.absolute_url(), pdf_document.getRelativeUrl()) pdf_document_url = '%s/%s' %(self.portal.absolute_url(), pdf_document.getRelativeUrl())
...@@ -1954,17 +1958,36 @@ return 1 ...@@ -1954,17 +1958,36 @@ return 1
# so allow some tollerance which is produced by respective portal_transform command # so allow some tollerance which is produced by respective portal_transform command
# any OOo based portal type # any OOo based portal type
ooo_document_image_size = getURLSize(ooo_document_url, **convert_kw) ooo_document_image_size, ooo_document_file_size = getURLSizeList(ooo_document_url, **convert_kw)
self.assertTrue(max(preffered_size_for_display) - max(ooo_document_image_size) <= max_tollerance_px) self.assertTrue(max(preffered_size_for_display) - max(ooo_document_image_size) <= max_tollerance_px)
# PDF # PDF
pdf_document_image_size = getURLSize(pdf_document_url, **convert_kw) pdf_document_image_size, pdf_document_file_size = getURLSizeList(pdf_document_url, **convert_kw)
self.assertTrue(max(preffered_size_for_display) - max(pdf_document_image_size) <= max_tollerance_px) self.assertTrue(max(preffered_size_for_display) - max(pdf_document_image_size) <= max_tollerance_px)
# Image # Image
image_document_image_size = getURLSize(image_document_url, **convert_kw) image_document_image_size, image_document_file_size = getURLSizeList(image_document_url, **convert_kw)
self.assertTrue(max(preffered_size_for_display) - max(image_document_image_size) <= max_tollerance_px) self.assertTrue(max(preffered_size_for_display) - max(image_document_image_size) <= max_tollerance_px)
# test changing image quality will decrease its file size
for url in (image_document_url, pdf_document_url, ooo_document_url):
convert_kw = {'display':'xlarge', \
'format':'jpeg', \
'quality':100}
image_document_image_size_100p,image_document_file_size_100p = getURLSizeList(url, **convert_kw)
# decrease in quality should decrease its file size
convert_kw['quality'] = 5.0
image_document_image_size_5p,image_document_file_size_5p = getURLSizeList(url, **convert_kw)
# removing quality should enable defaults settings which should be reasonable between 5% and 100%
del convert_kw['quality']
image_document_image_size_no_quality,image_document_file_size_no_quality = getURLSizeList(url, **convert_kw)
# check file sizes
self.assertTrue(image_document_file_size_100p > image_document_file_size_no_quality and \
image_document_file_size_no_quality > image_document_file_size_5p)
# no matter of quality image sizes whould be the same
self.assertTrue(image_document_image_size_100p==image_document_image_size_5p and \
image_document_image_size_5p==image_document_image_size_no_quality)
def test_checkConversionFormatPermission(self): def test_checkConversionFormatPermission(self):
""" """
Test various use cases when conversion can be not allowed Test various use cases when conversion can be not allowed
......
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