Commit 9ffbfd64 authored by Jérome Perrin's avatar Jérome Perrin

Update Cached Convertable interface

- The interface says that getConversionSize raises a KeyError when this cache
  does not exist. Update implementation accordingly.
- update getConversion to always return a string for the data. The caller
  should not have to worry about the data format.
- warn when an OFS.Image is passed to setConversion
parent d351a1c3
......@@ -54,7 +54,7 @@ class ICachedConvertable(Interface):
Saves in the cache the converted data and mime type of a document,
and records the date of conversion.
data -- the converted data (string or PData)
data -- the converted data (string or Pdata)
mime -- the mime type of the converted data (string)
......@@ -65,9 +65,10 @@ class ICachedConvertable(Interface):
def getConversion(**kw):
"""
Returns the converted mime type and data of a document if any,
otherwise, raises an KeyError exception. mime type is a string.
data is either or PData instance (preferred) or a string.
Returns a tuple containing converted mime type and data of a document if
any, otherwise, raises an KeyError exception.
mime type is a string.
data is a string.
**kw -- conversion parameters
"""
......
......@@ -28,6 +28,7 @@
##############################################################################
from hashlib import md5
from warnings import warn
import string
from Acquisition import aq_base
......@@ -130,6 +131,7 @@ class CachedConvertableMixin:
conversion_md5 = md5(size).hexdigest()
size = len(size)
elif isinstance(data, OFSImage):
warn('Passing an OFS.Image to setConversion is deprecated', stacklevel=1)
cached_value = data
conversion_md5 = md5(str(data.data)).hexdigest()
size = len(data.data)
......@@ -215,17 +217,19 @@ class CachedConvertableMixin:
"""
"""
cached_dict = self._getConversionDataDict(**kw)
return cached_dict['mime'], cached_dict['data']
mime = cached_dict['mime']
data = cached_dict['data']
if isinstance(data, OFSImage):
data = data.data
if isinstance(data, Pdata):
data = str(data)
return mime, data
security.declareProtected(Permissions.View, 'getConversionSize')
def getConversionSize(self, **kw):
"""
"""
try:
return self._getConversionDataDict(**kw)['size']
except KeyError:
# If conversion doesn't exists return 0
return 0
return self._getConversionDataDict(**kw)['size']
security.declareProtected(Permissions.View, 'getConversionDate')
def getConversionDate(self, **kw):
......
......@@ -31,7 +31,6 @@ from Products.Formulator import Widget, Validator
from Products.Formulator.Field import ZMIField
from Products.Formulator.DummyField import fields
from OFS.Image import Image as OFSImage
from OFS.Image import Pdata
from lxml.etree import Element
from lxml import etree
import re
......@@ -157,10 +156,7 @@ class ImageFieldWidget(Widget.TextWidget):
# is displayed in the form as a thumbnail, it will be added in the odg
# document as thumbnail size/quality
content_type, image_data = image_object.convert(**image_parameter_dict)
if isinstance(image_data, (basestring, Pdata)):
image = OFSImage('', '', image_data)
else:
image = image_data
image = OFSImage('', '', image_data)
width = image.width
height = image.height
if image_data is None:
......
......@@ -30,6 +30,7 @@
import unittest
import time
from DateTime import DateTime
from Testing import ZopeTestCase
from testDms import TestDocumentMixin
......@@ -74,8 +75,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
def getTitle(self):
return "OOo Conversion Cache"
## tests
def test_image_conversion(self):
filename = 'TEST-en-002.doc'
file = makeFileUpload(filename)
......@@ -114,8 +113,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
"""
Test Conversion Cache mechanism
"""
print '\nPersistent Cache Conversion'
filename = 'TEST-en-002.doc'
file = makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file)
......@@ -130,13 +127,15 @@ class TestDocumentConversionCache(TestDocumentMixin):
document.convert(format=format)
self.commit()
self.assertTrue(document.hasConversion(format=format), 'Cache Storage failed for %s' % (format))
self.assertEquals(DateTime().Date(), document.getConversionDate(format=format).Date())
self.assertTrue(document.getConversionMd5(format=format))
self.assertTrue(document.getConversionSize(format=format))
document.edit(title='Foo')
self.commit()
#Test Cache is cleared
for format in format_list:
self.assertFalse(document.hasConversion(format=format), 'Cache Storage failed for %s' % (format))
self.assertEquals(document.getConversionSize(format=format), 0)
self.assertRaises(KeyError, document.getConversionSize, format=format)
document.edit(title='Bar')
self.tic()
#Test Conversion Cache after editing
......@@ -147,11 +146,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.assertTrue(document.getConversionSize(format=format))
def test_02_VolatileCacheConversionOfTempObject(self):
"""
Test Conversion Cache mechanism
"""
print '\nVolatile Cache Conversion of temp objects'
filename = 'TEST-en-002.doc'
file = makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file, temp_object=1)
......@@ -166,13 +160,15 @@ class TestDocumentConversionCache(TestDocumentMixin):
document.convert(format=format)
self.commit()
self.assertTrue(document.hasConversion(format=format), 'Cache Storage failed for %s' % (format))
self.assertEquals(DateTime().Date(), document.getConversionDate(format=format).Date())
self.assertTrue(document.getConversionMd5(format=format))
self.assertTrue(document.getConversionSize(format=format))
document.edit(title='Foo')
self.commit()
#Test Cache is cleared
for format in format_list:
self.assertFalse(document.hasConversion(format=format), 'Cache Storage failed for %s' % (format))
self.assertEqual(document.getConversionSize(format=format), 0)
self.assertRaises(KeyError, document.getConversionSize, format=format)
document.edit(title='Bar')
self.tic()
#Test Conversion Cache after editing
......@@ -183,11 +179,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.assertTrue(document.getConversionSize(format=format))
def test_03_CacheConversionOfTempObjectIsNotMixed(self):
"""
Test Conversion Cache mechanism
"""
print '\nCache Conversion of temp objects is not mixed'
filename1 = 'TEST-en-002.doc'
filename2 = 'TEST-en-002.odt'
file1 = makeFileUpload(filename1)
......@@ -208,10 +199,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.tic()
def test_04_PersistentCacheConversionWithFlare(self):
"""
Test Conversion Cache mechanism
"""
print '\nPersistent Cache Conversion with Flare'
default_pref = self.portal.portal_preferences.default_site_preference
default_pref.setPreferredConversionCacheFactory('dms_cache_factory')
#old preferred value is still cached
......@@ -239,7 +226,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
for format in format_list:
self.assertFalse(document.hasConversion(format=format),
'Cache Storage failed for %s' % (format))
self.assertEqual(document.getConversionSize(format=format), 0)
self.assertRaises(KeyError, document.getConversionSize, format=format)
document.edit(title='Bar')
self.tic()
#Test Conversion Cache after editing
......@@ -253,7 +240,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
"""
Test Conversion Cache return expected value with checksum
"""
print '\nCheck checksum in Conversion'
filename = 'TEST-en-002.doc'
file = makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file)
......@@ -283,7 +269,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
"""
Check that md5 checksum is well updated when upload a file
"""
print '\nCheck checksum is updated'
filename = 'TEST-en-002.doc'
file = makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file)
......@@ -302,7 +287,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
"""
Check that key (based on path of document) support unauthorised chars
"""
print '\nCheck key (based on path) support unauthorised chars'
default_pref = self.portal.portal_preferences.default_site_preference
default_pref.setPreferredConversionCacheFactory('dms_cache_factory')
#old preferred value is still cached
......@@ -325,7 +309,6 @@ class TestDocumentConversionCache(TestDocumentMixin):
def test_08_check_conversion_cache_with_portal_document_type_list(self):
"""Check cache conversion for all Portal Document Types
"""
print '\nCheck cache conversion for all Portal Document Types'
portal_type_list = list(self.portal.getPortalDocumentTypeList())
if 'File' in portal_type_list:
......
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