def set(self, name, instance, value, **kwargs): # Remove acquisition wrappers value = aq_base(value) ann = getAnnotation(instance) ann.setSubkey(self._key, value, subkey=name) if self._migrate: self._cleanup(name, instance, value, **kwargs)
def initializeField(self, instance, field): # Check for already existing field to avoid the reinitialization # (which means overwriting) of an already existing field after a # copy or rename operation ann = getAnnotation(instance) if not ann.hasSubkey(self._key, subkey=field.getName()): self.set(field.getName(), instance, field.getDefault(instance))
def get(self, name, instance, **kwargs): ann = getAnnotation(instance) value = ann.getSubkey(self._key, subkey=name, default=_marker) if value is _marker: if self._migrate: value = self._migration(name, instance, **kwargs) else: raise AttributeError(name) return value
def _migrate_book(obj): fields = [ 'title', 'description', 'use_titlepage', 'use_toc', 'use_lot', 'use_loi' ] ann = getAnnotation(obj) for name in fields: value = ann.getSubkey(AT_ANN_STORAGE, subkey=name, default=_marker) if value is not _marker: obj.getField(name).set(obj, value)
def upgrade_step_2(context): """Upgrade timezone and recurrence from AnnotationStorage to new storage (AttributeStorage). Using Products.contentmigration doesn't work here, since on migration time, the fields' storage is already an AttributeStorage. !!! ATTENTION !!! This upgrade steps migrates fields if there is a AnnotationStorage entry for it, even if there is already a value in AttributeStorage. We have to do so, because there is a default value for the timezone, which might not be the value previously set with AnnotationStorage. """ migrate_fields = ['timezone', 'recurrence'] query = {} query['object_provides'] = IATEvent_PAE.__identifier__ cat = getToolByName(context, 'portal_catalog') result = cat(**query) for brain in result: obj = brain.getObject() ann = getAnnotation(obj) for field in migrate_fields: key = 'Archetypes.storage.AnnotationStorage-%s' % field if key in ann: val = key in ann and ann[key] or None del ann[key] # Delete the annotation entry getter = getattr(obj, 'get%s' % field.title()) # Get the getter prev_val = getter() setter = getattr(obj, 'set%s' % field.title()) # Get the setter setter(val) # Set the val on new storage logger.info("""'Field migration for obj %s, field %s from AnnotationStorage to AttributeStorage done. Previous value: %s, new value from AnnotationStorage: %s.""" % (obj, field, prev_val, val))
def migrateStorageOfType(portal, portal_type, schema): """Migrate storage from attribute to annotation storage portal - portal portal_type - portal type name to migrate schema - schema of the type The schema is used to detect annotation and metadata annotation stored field for migration. """ catalog = getToolByName(portal, 'portal_catalog') brains = catalog(dict(Type=portal_type)) fields = [field.getName() for field in schema.fields() if field.storage.__class__ == AnnotationStorage ] md_fields = [field.getName() for field in schema.fields() if field.storage.__class__ == MetadataAnnotationStorage ] for brain in brains: obj = brain.getObject() if obj is None: continue try: state = obj._p_changed except: state = 0 ann = getAnnotation(obj) clean_obj = aq_base(obj) _attr2ann(clean_obj, ann, fields) _meta2ann(clean_obj, ann, md_fields) if state is None: obj._p_deactivate()
def getSourceType(self, instance): """the source path from the instance annotations or empty string""" ann = getAnnotation(instance) subkey = "%s-source-type" % self.getName() return ann.getSubkey(KEY, subkey, default='')
def customize(self, instance): """Set the customize flag to True""" ann = getAnnotation(instance) subkey = "%s-customized" % self.getName() ann.setSubkey(KEY, True, subkey) return None
def is_customized(self, instance): """Flag whether this field has been customized""" ann = getAnnotation(instance) subkey = "%s-customized" % self.getName() return ann.getSubkey(KEY, subkey, default=self.customized)
def setLastUpdate(self, instance, value): """Stores the time of the last update in the instance annotations""" ann = getAnnotation(instance) subkey = "%s-last-update" % self.getName() return ann.setSubkey(KEY, value, subkey)
def getLastUpdate(self, instance): """time of the last update or None""" ann = getAnnotation(instance) subkey = "%s-last-update" % self.getName() return ann.getSubkey(KEY, subkey, default=None)
def setTimeout(self, instance, value): """Stores the timeout in the instance annotations""" ann = getAnnotation(instance) subkey = "%s-timeout" % self.getName() return ann.setSubkey(KEY, value, subkey)
def setSourceType(self, instance, value): """Stores source path in the instance annotations""" ann = getAnnotation(instance) subkey = "%s-source-type" % self.getName() return ann.setSubkey(KEY, value, subkey)
def getTimeout(self, instance): """the timeout from the instance annotations or empty string""" ann = getAnnotation(instance) subkey = "%s-timeout" % self.getName() return ann.getSubkey(KEY, subkey, default=self.timeout)
def unset(self, name, instance, **kwargs): ann = getAnnotation(instance) try: ann.delSubkey(self._key, subkey=name) except KeyError: pass