security.declarePrivate('unset')

    def unset(self, name, instance, **kwargs):
        if not shasattr(instance, "_md"):
            log("Broken instance %s, no _md" % instance)
        else:
            del instance._md[name]
            instance._p_changed = 1

    security.declarePrivate('cleanupField')

    def cleanupField(self, instance, field, **kwargs):
        # Don't clean up the field self to avoid problems with copy/rename. The
        # python garbarage system will clean up if needed.
        pass

    security.declarePrivate('cleanupInstance')

    def cleanupInstance(self, instance, item=None, container=None):
        # Don't clean up the instance self to avoid problems with copy/rename. The
        # python garbarage system will clean up if needed.
        pass

__all__ = ('ReadOnlyStorage', 'ObjectManagedStorage',
           'MetadataStorage', 'AttributeStorage',)

for name in __all__:
    storage = locals()[name]
    registerStorage(storage)
Example #2
0
        return value

    security.declarePrivate('set')
    def set(self, name, instance, value, **kwargs):
        field = kwargs['field']
        tool = self.getTool(instance)
        mdata = tool.getMetadata(instance)
        if isinstance(value, unicode):
            value = encode(value, instance)
        data = {field.metadata_name:value}
        # Calling _setData directly, because there's
        # *no* method for setting one field at a time,
        # and setValues takes a dict and does
        # validation, which prevents us from setting
        # values.
        mdata._setData(data, set_id=self.metadata_set)

    security.declarePrivate('unset')
    def unset(self, name, instance, **kwargs):
        pass

    security.declarePrivate('cleanupField')
    def cleanupField(self, instance, field, **kwargs):
        pass

    security.declarePrivate('cleanupInstance')
    def cleanupInstance(self, instance, item=None, container=None):
        pass

registerStorage(FacadeMetadataStorage)
    def _migration(self, name, instance, **kwargs):
        """Migrates data from the original storage
        """
        value = getattr(aq_base(instance), name, _marker)
        if value is _marker:
            raise AttributeError(name)
        # explicit del althought set would do the job, too
        delattr(instance, name)
        self.set(name, instance, value, **kwargs)
        return value

    def _cleanup(self, name, instance, value, **kwargs):
        if shasattr(instance, name):
            delattr(instance, name)

registerStorage(AnnotationStorage)


class MetadataAnnotationStorage(BaseAnnotationStorage, StorageLayer):
    """Stores metadata as ATAnnotations on the object
    """

    _key = AT_MD_STORAGE

    security = ClassSecurityInfo()

    def _migration(self, name, instance, **kwargs):
        """Migrates data from the original storage
        """
        try:
            md = aq_base(instance)._md
        logger.info("CassandraFieldStorage.set: name=%s, instance=%s, value=%s ..., kw=%s" % (name, repr(instance), str(value)[:20], kwargs))
        key = self.key_for_instance(instance)

        field = kwargs.get("field")

        if key is None:
            logger.warning("CassandraFieldStorage.set: Field %s, instance %s: key is None. Cannot save data." %(field, repr(instance)))
            return

        if not isinstance(value, types.StringTypes):
            # XXX: fix Image and File Fields!
            value = str(value) # BaseUnit

        self.data.insert(key, {name: value.encode(CassandraFieldStorage.encoding)})

    security.declarePrivate('unset')
    def unset(self, name, instance, **kwargs):
        logger.info("CassandraFieldStorage.unset: name=%s, instance=%s, kw=%s" % (name, repr(instance), kw))

        key = self.key_for_instance(instance)
        try:
            self.data.remove(key, columns=[name])
        except (pycassa.NotFoundException, pycassa.NoServerAvailable), e:
            pass


setSecurity(CassandraFieldStorage)
registerStorage(CassandraFieldStorage)

# vim: set ft=python ts=4 sw=4 expandtab :