def test_newsitem_content_is_migrated(self): from Products.ATContentTypes.content.newsitem import ATNewsItem from plone.app.contenttypes.migration import NewsItemMigrator from plone.app.textfield.interfaces import IRichTextValue from plone.namedfile.interfaces import INamedImage # create an ATNewsItem at_newsitem = self.createATCTobject(ATNewsItem, 'newsitem') at_newsitem.setText('Tütensuppe') at_newsitem.setContentType('chemical/x-gaussian-checkpoint') at_newsitem.setImageCaption('Daniel Düsentrieb') test_image_data = self.get_test_image_data() image_field = at_newsitem.getField('image') image_field.set(at_newsitem, test_image_data) image_field.setFilename(at_newsitem, 'testimage.png') # migrate migrator = self.get_migrator(at_newsitem, NewsItemMigrator) migrator.migrate() # assertions new_newsitem = self.portal['newsitem'] self.assertTrue(INamedImage.providedBy(new_newsitem.image)) self.assertEqual(new_newsitem.image.filename, 'testimage.png') self.assertEqual(new_newsitem.image.contentType, 'image/png') self.assertEqual(new_newsitem.image.data, test_image_data) self.assertEqual(new_newsitem.image_caption, u'Daniel Düsentrieb') self.assertTrue(IRichTextValue(new_newsitem.text)) self.assertEqual(new_newsitem.text.raw, u'Tütensuppe') self.assertEqual(new_newsitem.text.mimeType, 'chemical/x-gaussian-checkpoint') self.assertEqual(new_newsitem.text.outputMimeType, 'text/x-html-safe')
def set(self, data): # when setting data, we need to purge scales/image data... # XXX hack? try: scale_key = self.key.replace('.data.', '.scale.') del self.annotations[scale_key] except KeyError: pass for k, v in data.items(): if INamedImage.providedBy(v): mtime_key = '{0}_mtime'.format(k) if (self.key not in self.annotations or k not in self.annotations[self.key] or (self.key in self.annotations and data[k] != self.annotations[self.key][k])): # set modification time of the image notify(Purge(self.tile)) data[mtime_key] = repr(time.time()) else: data[mtime_key] = self.annotations[self.key].get( mtime_key, '') self.annotations[self.key] = PersistentDict(data) notify(ObjectModifiedEvent(self.context))
def modified(self): """ provide a callable to return the modification time of content items, so stored image scales can be invalidated """ mtime = 0 for k, v in self.context.data.items(): if INamedImage.providedBy(v): mtime += v._p_mtime return mtime
def set(self, data): for k, v in data.items(): if INamedImage.providedBy(v): if (self.key not in self.annotations or k not in self.annotations[self.key] or (self.key not in self.annotations and data[k] != self.annotations[self.key][k])): # set modification time of the image data['%s_mtime' % k] = time.mktime(datetime.now().timetuple()) self.annotations[self.key] = PersistentDict(data)
def set(self, data): for k, v in data.items(): if INamedImage.providedBy(v): if (self.key not in self.annotations or k not in self.annotations[self.key] or (self.key in self.annotations and data[k] != self.annotations[self.key][k])): # set modification time of the image notify(Purge(self.tile)) data['%s_mtime' % k] = '%f' % time.time() self.annotations[self.key] = PersistentDict(data)
def set(self, data): for k, v in data.items(): if INamedImage.providedBy(v): if (self.key not in self.annotations or k not in self.annotations[self.key] or (self.key in self.annotations and data[k] != self.annotations[self.key][k])): # set modification time of the image notify(Purge(self.tile)) data['{0}_mtime'.format(k)] = '%f' % time.time() self.annotations[self.key] = PersistentDict(data)
def set(self, data): for k, v in data.items(): if INamedImage.providedBy(v): if (self.key not in self.annotations or k not in self.annotations[self.key] or (self.key not in self.annotations and data[k] != self.annotations[self.key][k])): # set modification time of the image data['%s_mtime' % k] = time.mktime( datetime.now().timetuple()) self.annotations[self.key] = PersistentDict(data)
def set(self, data): for k, v in data.items(): if INamedImage.providedBy(v): if not self.annotations.has_key(self.key) or \ not self.annotations[self.key].has_key(k) or \ (self.annotations.has_key(self.key) and \ data[k] != self.annotations[self.key][k]): # set modification time of the image data['%s_mtime' % k] = time.mktime( datetime.now().timetuple()) self.annotations[self.key] = PersistentDict(data)
def set(self, data): for k, v in data.items(): if INamedImage.providedBy(v): if (self.key not in self.annotations or k not in self.annotations[self.key] or (self.key in self.annotations and data[k] != self.annotations[self.key][k])): # set modification time of the image notify(Purge(self.tile)) data['{0}_mtime'.format(k)] = '%f' % time.time() self.annotations[self.key] = PersistentDict(data) notify(ObjectModifiedEvent(self.context))
def test_namedimage_setting(self): data = 'datattatatata' filename = FILENAME_TESTSTRING + u".jpeg" content_type = 'image/jpeg' fileSetter = self.getFileSetter(ISchemaWithNamedImage) fileSetter.set(data, filename.encode('utf-8'), content_type) field = fileSetter.context.field self.assertTrue(INamedImage.providedBy(field)) self.assertEqual(field.filename, filename) self.assertEqual(field.contentType, content_type) self.assertEqual(field.data, data)
def getRelativePaths(self): context = aq_inner(self.context) parent = aq_parent(context) portal_url = api.portal.get().portal_url() prefix = context.url.replace(portal_url, '', 1) yield prefix for k, v in context.data.items(): if INamedImage.providedBy(v): yield '{0}/@@images/image'.format(prefix) scales = parent.unrestrictedTraverse('{0}/{1}'.format( prefix.strip('/'), '@@images')) for size in scales.getAvailableSizes().keys(): yield '{0}/@@images/{1}'.format(prefix, size)
def modified(self): """ provide a callable to return the modification time of content items, so stored image scales can be invalidated """ if not IPersistentCoverTile.providedBy(self.context): base_scales = getMultiAdapter( (self.context, self.request), name='images') return base_scales.modified() mtime = '' for k, v in self.context.data.items(): if INamedImage.providedBy(v): mtime += self.context.data.get('%s_mtime' % k, 0) return mtime
def getRelativePaths(self): context = self.context.aq_inner portal_state = getMultiAdapter( (context, context.request), name=u'plone_portal_state') prefix = context.url.replace(portal_state.portal_url(), '', 1) yield prefix for _, v in context.data.items(): if INamedImage.providedBy(v): yield "%s/@@images/image" % prefix scales = aq_parent(context).unrestrictedTraverse(prefix.strip('/') + '/@@images') for size in scales.getAvailableSizes().keys(): yield "%s/@@images/%s" % (prefix, size,)
def getRelativePaths(self): context = aq_inner(self.context) parent = aq_parent(context) portal_url = api.portal.get().portal_url() prefix = context.url.replace(portal_url, '', 1) yield prefix for k, v in context.data.items(): if INamedImage.providedBy(v): yield '{0}/@@images/image'.format(prefix) scales = parent.unrestrictedTraverse( '{0}/{1}'.format(prefix.strip('/'), '@@images')) for size in scales.getAvailableSizes().keys(): yield '{0}/@@images/{1}'.format(prefix, size)
def modified(self): """ provide a callable to return the modification time of content items, so stored image scales can be invalidated """ if not IPersistentCoverTile.providedBy(self.context): base_scales = queryMultiAdapter((self.context, self.request), name='images', default=None) return base_scales and base_scales.modified() mtime = None for k, v in self.context.data.items(): if INamedImage.providedBy(v): mtime = self.context.data.get('{0}_mtime'.format(k), None) return mtime
def modified(self): """ provide a callable to return the modification time of content items, so stored image scales can be invalidated """ if not IPersistentCoverTile.providedBy(self.context): base_scales = queryMultiAdapter((self.context, self.request), name='images', default=None) return base_scales and base_scales.modified() mtime = '' for k, v in self.context.data.items(): if INamedImage.providedBy(v): mtime += self.context.data.get('{0}_mtime'.format(k), 0) return mtime
def getRelativePaths(self): context = aq_inner(self.context) parent = aq_parent(context) portal_state = getMultiAdapter((context, context.request), name=u'plone_portal_state') prefix = context.url.replace(portal_state.portal_url(), '', 1) yield prefix for _, v in context.data.items(): if INamedImage.providedBy(v): yield '{0}/@@images/image'.format(prefix) scales = parent.unrestrictedTraverse('{0}/{1}'.format( prefix.strip('/'), '@@images')) for size in scales.getAvailableSizes().keys(): yield '{0}/@@images/{1}'.format(prefix, size)
def getRelativePaths(self): context = aq_inner(self.context) parent = aq_parent(context) portal_state = getMultiAdapter( (context, context.request), name=u'plone_portal_state') prefix = context.url.replace(portal_state.portal_url(), '', 1) yield prefix for _, v in context.data.items(): if INamedImage.providedBy(v): yield '{0}/@@images/image'.format(prefix) scales = parent.unrestrictedTraverse( '{0}/{1}'.format(prefix.strip('/'), '@@images')) for size in scales.getAvailableSizes().keys(): yield '{0}/@@images/{1}'.format(prefix, size)
def test_image_content_is_migrated(self): from plone.app.contenttypes.migration import ImageMigrator from plone.namedfile.interfaces import INamedImage from Products.ATContentTypes.content.image import ATImage # create the ATImage at_image = self.createATCTobject(ATImage, 'image') test_image_data = self.get_test_image_data() field = at_image.getField('image') field.set(at_image, test_image_data) field.setFilename(at_image, 'testimage.png') migrator = self.get_migrator(at_image, ImageMigrator) migrator.migrate() new_image = self.portal['image'] self.assertTrue(INamedImage.providedBy(new_image.image)) self.assertEqual(new_image.image.filename, 'testimage.png') self.assertEqual(new_image.image.contentType, 'image/png') self.assertEqual(new_image.image.data, test_image_data)
def set(self, data): # when setting data, we need to purge scales/image data... # XXX hack? try: scale_key = self.key.replace('.data.', '.scale.') del self.annotations[scale_key] except KeyError: pass for k, v in data.items(): if INamedImage.providedBy(v): if (self.key not in self.annotations or k not in self.annotations[self.key] or (self.key in self.annotations and data[k] != self.annotations[self.key][k])): # set modification time of the image notify(Purge(self.tile)) data['{0}_mtime'.format(k)] = '%f' % time.time() self.annotations[self.key] = PersistentDict(data) notify(ObjectModifiedEvent(self.context))
def fix_image_field_modification_time(context): """Fix image modification time to be float timestamp instead of string.""" covers = context.portal_catalog(object_provides=ICover.__identifier__) 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 not INamedImage.providedBy(v): continue 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')
def width(self): if INamedImage.providedBy(self.value): return self.value._width else: return None
def height(self): if INamedImage.providedBy(self.value): return self.value._height else: return None