From 8ae21ca66e072fe4e6776de31b15591a48b1c5be Mon Sep 17 00:00:00 2001 From: Rodrigo Ferreira de Souza Date: Fri, 2 Dec 2016 08:05:53 -0200 Subject: [PATCH] Use timestamp instead of string of timestamp to save image modified attribute --- CHANGES.rst | 4 +++ src/collective/cover/browser/scaling.py | 6 ++-- src/collective/cover/tests/test_scaling.py | 32 +++++++++++++++++ src/collective/cover/tests/test_upgrades.py | 35 ++++++++++++++++++- src/collective/cover/tiles/data.py | 5 ++- src/collective/cover/upgrades/v15/__init__.py | 27 ++++++++++++++ .../cover/upgrades/v15/configure.zcml | 6 ++++ 7 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 src/collective/cover/tests/test_scaling.py diff --git a/CHANGES.rst b/CHANGES.rst index 8802dc677..e602b1308 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,9 @@ There's a frood who really knows where his towel is. 1.3b2 (unreleased) ^^^^^^^^^^^^^^^^^^ +- Use timestamp object instead of string of timestamp to represent image modified date (fixes `#686`_). + [rodfersou] + - Fixed adding a 'more' link in list tiles. Previously you could select an item to use as 'more' link, but it did not stick. [maurits] @@ -157,3 +160,4 @@ Previous entries can be found in the HISTORY.rst file. .. _`#608`: https://github.com/collective/collective.cover/issues/608 .. _`#641`: https://github.com/collective/collective.cover/issues/641 .. _`#651`: https://github.com/collective/collective.cover/issues/651 +.. _`#686`: https://github.com/collective/collective.cover/issues/686 diff --git a/src/collective/cover/browser/scaling.py b/src/collective/cover/browser/scaling.py index df9af4aa7..e8a8244bd 100644 --- a/src/collective/cover/browser/scaling.py +++ b/src/collective/cover/browser/scaling.py @@ -158,10 +158,12 @@ def modified(self): name='images', default=None) return base_scales and base_scales.modified() - mtime = '' + mtime = 0 for k, v in self.context.data.items(): if INamedImage.providedBy(v): - mtime += self.context.data.get('{0}_mtime'.format(k), '') + image_time = self.context.data.get('{0}_mtime'.format(k), 0) + if image_time > mtime: + mtime = image_time return mtime diff --git a/src/collective/cover/tests/test_scaling.py b/src/collective/cover/tests/test_scaling.py new file mode 100644 index 000000000..7e3117b91 --- /dev/null +++ b/src/collective/cover/tests/test_scaling.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +from collective.cover.browser.scaling import ImageScaling +from collective.cover.testing import INTEGRATION_TESTING +from collective.cover.tiles.basic import BasicTile +from plone import api + +import unittest + + +class ImageScalingTestCase(unittest.TestCase): + + layer = INTEGRATION_TESTING + + def setUp(self): + self.portal = self.layer['portal'] + self.request = self.layer['request'] + + with api.env.adopt_roles(['Manager']): + self.cover = api.content.create( + self.portal, 'collective.cover.content', 'c1') + + self.tile = BasicTile(self.cover, self.request) + self.tile.__name__ = u'collective.cover.basic' + self.tile.id = u'test' + + obj = self.portal['my-image'] + self.tile.populate_with_object(obj) + + self.scaling = ImageScaling(self.tile, self.request) + + def test_modified(self): + self.assertIsInstance(self.scaling.modified(), float) diff --git a/src/collective/cover/tests/test_upgrades.py b/src/collective/cover/tests/test_upgrades.py index e7e6a357c..4c9a9de52 100644 --- a/src/collective/cover/tests/test_upgrades.py +++ b/src/collective/cover/tests/test_upgrades.py @@ -3,6 +3,7 @@ from collective.cover.config import IS_PLONE_5 from collective.cover.controlpanel import ICoverSettings from collective.cover.testing import INTEGRATION_TESTING +from persistent.dict import PersistentDict from persistent.mapping import PersistentMapping from plone import api from plone.registry.interfaces import IRegistry @@ -587,4 +588,36 @@ def setUp(self): def test_registrations(self): version = self.setup.getLastVersionForProfile(self.profile_id)[0] self.assertGreaterEqual(int(version), int(self.to_version)) - self.assertEqual(self._how_many_upgrades_to_do(), 1) + self.assertEqual(self._how_many_upgrades_to_do(), 2) + + def test_fix_image_modified_date(self): + title = u'Update all tiles to fix modified date' + step = self._get_upgrade_step(title) + assert step is not None + + # simulate state on previous version + cover = self._create_cover('test-cover', 'Empty layout') + cover.cover_layout = ( + '[{"type": "row", "children": [{"column-size": 16, "type": ' + '"group", "children": [{"tile-type": ' + '"collective.cover.basic", "type": "tile", "id": ' + '"ca6ba6675ef145e4a569c5e410af7511"}], "roles": ["Manager"]}]}]' + ) + + tile = cover.get_tile('ca6ba6675ef145e4a569c5e410af7511') + obj = self.portal['my-image'] + tile.populate_with_object(obj) + + dmgr = ITileDataManager(tile) + old_data = dmgr.get() + old_data['image_mtime'] = repr(old_data['image_mtime']) + dmgr.annotations[dmgr.key] = PersistentDict(old_data) + + data = dmgr.get() + self.assertIsInstance(data['image_mtime'], str) + + # run the upgrade step to validate the update + self._do_upgrade_step(step) + + data = dmgr.get() + self.assertIsInstance(data['image_mtime'], float) diff --git a/src/collective/cover/tiles/data.py b/src/collective/cover/tiles/data.py index ce62a69fd..78a4aa915 100644 --- a/src/collective/cover/tiles/data.py +++ b/src/collective/cover/tiles/data.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- - from collective.cover.tiles.base import IPersistentCoverTile from persistent.dict import PersistentDict from plone.namedfile.interfaces import INamedImage @@ -52,9 +51,9 @@ def set(self, data): data[k] != self.annotations[self.key][k])): # set modification time of the image notify(Purge(self.tile)) - data[mtime_key] = repr(time.time()) + data[mtime_key] = time.time() else: - data[mtime_key] = self.annotations[self.key].get(mtime_key, '') + data[mtime_key] = self.annotations[self.key].get(mtime_key, 0) self.annotations[self.key] = PersistentDict(data) notify(ObjectModifiedEvent(self.context)) diff --git a/src/collective/cover/upgrades/v15/__init__.py b/src/collective/cover/upgrades/v15/__init__.py index 40a96afc6..ade549f55 100644 --- a/src/collective/cover/upgrades/v15/__init__.py +++ b/src/collective/cover/upgrades/v15/__init__.py @@ -1 +1,28 @@ # -*- coding: utf-8 -*- +from collective.cover.logger import logger +from persistent.dict import PersistentDict +from plone.namedfile.interfaces import INamedImage +from plone.tiles.interfaces import ITileDataManager + + +def fix_image_modified_date(context): + """Update all tiles to fix modified date""" + + covers = context.portal_catalog(portal_type='collective.cover.content') + logger.info('About to update {0} objects'.format(len(covers))) + for cover in covers: + obj = cover.getObject() + for tile_id in obj.list_tiles(): + tile = obj.get_tile(tile_id) + dmgr = ITileDataManager(tile) + data = dmgr.get() + for k, v in data.items(): + if INamedImage.providedBy(v): + mtime_key = '{0}_mtime'.format(k) + data[mtime_key] = float(data[mtime_key]) + # need to set changes directly into annotation + dmgr.annotations[dmgr.key] = PersistentDict(data) + msg = 'Tile {0} at {1} updated' + logger.info(msg.format(tile_id, cover.getPath())) + + logger.info('Done') diff --git a/src/collective/cover/upgrades/v15/configure.zcml b/src/collective/cover/upgrades/v15/configure.zcml index 04a52854c..5c8d2bd01 100644 --- a/src/collective/cover/upgrades/v15/configure.zcml +++ b/src/collective/cover/upgrades/v15/configure.zcml @@ -9,6 +9,12 @@ destination="15" profile="collective.cover:default"> + +