Back to index

plone3  3.1.7
Facade.py
Go to the documentation of this file.
00001 from Products.CMFCore.utils import getToolByName
00002 from Products.Archetypes.Storage import StorageLayer
00003 from Products.Archetypes.interfaces.storage import IStorage
00004 from Products.Archetypes.interfaces.layer import ILayer
00005 from Products.Archetypes.Field import encode
00006 
00007 from AccessControl import ClassSecurityInfo
00008 from Products.Archetypes.Registry import registerStorage
00009 
00010 class FacadeMetadataStorage(StorageLayer):
00011     """A Facade Storage which delegates to
00012     CMFMetadata's Metadata Tool for actually
00013     storing the metadata values
00014     """
00015 
00016     security = ClassSecurityInfo()
00017 
00018     __implements__ = (IStorage, ILayer)
00019     
00020     def __init__(self, metadata_set):
00021         self.metadata_set = metadata_set
00022 
00023     security.declarePrivate('getTool')
00024     def getTool(self, instance):
00025         return getToolByName(instance, 'portal_metadata')
00026 
00027     security.declarePrivate('initializeInstance')
00028     def initializeInstance(self, instance, item=None, container=None):
00029         pass
00030 
00031     security.declarePrivate('initializeField')
00032     def initializeField(self, instance, field):
00033         pass
00034 
00035     security.declarePrivate('get')
00036     def get(self, name, instance, **kwargs):
00037         field = kwargs['field']
00038         tool = self.getTool(instance)
00039         mdata = tool.getMetadata(instance)
00040         value = mdata[self.metadata_set][field.metadata_name]
00041         return value
00042 
00043     security.declarePrivate('set')
00044     def set(self, name, instance, value, **kwargs):
00045         field = kwargs['field']
00046         tool = self.getTool(instance)
00047         mdata = tool.getMetadata(instance)
00048         if type(value) == type(u''):
00049             value = encode(value, instance)
00050         data = {field.metadata_name:value}
00051         # Calling _setData directly, because there's
00052         # *no* method for setting one field at a time,
00053         # and setValues takes a dict and does
00054         # validation, which prevents us from setting
00055         # values.
00056         mdata._setData(data, set_id=self.metadata_set)
00057 
00058     security.declarePrivate('unset')
00059     def unset(self, name, instance, **kwargs):
00060         pass
00061 
00062     security.declarePrivate('cleanupField')
00063     def cleanupField(self, instance, field, **kwargs):
00064         pass
00065 
00066     security.declarePrivate('cleanupInstance')
00067     def cleanupInstance(self, instance, item=None, container=None):
00068         pass
00069 
00070 registerStorage(FacadeMetadataStorage)