Skip to content

Commit

Permalink
Use timestamp instead of string of timestamp to save image modified a…
Browse files Browse the repository at this point in the history
…ttribute
  • Loading branch information
rodfersou committed Dec 2, 2016
1 parent 16804ec commit 8ae21ca
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -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]
Expand Down Expand Up @@ -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
6 changes: 4 additions & 2 deletions src/collective/cover/browser/scaling.py
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions 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)
35 changes: 34 additions & 1 deletion src/collective/cover/tests/test_upgrades.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions 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
Expand Down Expand Up @@ -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))
27 changes: 27 additions & 0 deletions 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')
6 changes: 6 additions & 0 deletions src/collective/cover/upgrades/v15/configure.zcml
Expand Up @@ -9,6 +9,12 @@
destination="15"
profile="collective.cover:default">

<genericsetup:upgradeStep
title="Update all tiles to fix modified date"
description="Modified date should be DateTime object instead of string of timestamp"
handler=".fix_image_modified_date"
/>

<genericsetup:upgradeStep
title="Cook JS resources"
description="There were changes in the JS files, so we need to cook the resources."
Expand Down

0 comments on commit 8ae21ca

Please sign in to comment.