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