Back to index

plone3  3.1.7
storage.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 
00003 from zope.app.container.btree import BTreeContainer
00004 from zope.app.container.contained import Contained, NameChooser
00005 from zope.app.container.ordered import OrderedContainer
00006 
00007 from plone.portlets.interfaces import IPortletStorage
00008 from plone.portlets.interfaces import IPortletCategoryMapping
00009 from plone.portlets.interfaces import IPortletAssignmentMapping
00010 
00011 from BTrees.OOBTree import OOBTree
00012 
00013 # XXX: We coerce all mapping keys (things like user and group ids)
00014 # to unicode, because the OOBTree that we store them in will fall over with
00015 # mixed encoded-str and unicode keys. It may be better to store byte strings
00016 # (and thus coerce the other way), especially to support things like Active
00017 # Directory where user ids are binary GUIDs. However, that's a problem for
00018 # another day, since it'll require more complex migration.
00019 
00020 def _coerce(key):
00021     if isinstance(key, str):
00022         key = unicode(key, encoding='utf-8')
00023     return key
00024 
00025 class PortletStorage(BTreeContainer):
00026     """The default portlet storage.
00027     """
00028     implements(IPortletStorage)
00029         
00030 class PortletCategoryMapping(BTreeContainer, Contained):
00031     """The default category/key mapping storage.
00032     """
00033     implements(IPortletCategoryMapping)
00034     
00035     # We need to hack some stuff to make sure keys are unicode.
00036     # The shole BTreeContainer/SampleContainer mess is a pain in the backside
00037     
00038     def __getitem__(self, key):
00039         return super(PortletCategoryMapping, self).__getitem__(_coerce(key))
00040 
00041     def get(self, key, default=None):
00042         '''See interface `IReadContainer`'''
00043         return super(PortletCategoryMapping, self).get(_coerce(key), default)
00044 
00045     def __contains__(self, key):
00046         '''See interface `IReadContainer`'''
00047         return super(PortletCategoryMapping, self).__contains__(_coerce(key))
00048 
00049     has_key = __contains__
00050 
00051     def __setitem__(self, key, object):
00052         '''See interface `IWriteContainer`'''
00053         super(PortletCategoryMapping, self).__setitem__(_coerce(key), object)
00054 
00055     def __delitem__(self, key):
00056         '''See interface `IWriteContainer`'''
00057         super(PortletCategoryMapping, self).__delitem__(_coerce(key))
00058         
00059 
00060 class PortletAssignmentMapping(OrderedContainer):
00061     """The default assignment mapping storage.
00062     """
00063     implements(IPortletAssignmentMapping)
00064     
00065     __manager__ = u''
00066     __category__ = u''
00067     __name__ = u''
00068     
00069     def __init__(self, manager=u'', category=u'', name=u''):
00070         # XXX: This depends on implementation detail in OrderedContainer,
00071         # but it uses a PersistentDict, which sucks :-/
00072         OrderedContainer.__init__(self)
00073         self._data = OOBTree()
00074         
00075         self.__manager__ = manager
00076         self.__category__ = category
00077         self.__name__ = name