Back to index

plone3  3.1.7
assignable.py
Go to the documentation of this file.
00001 from persistent.dict import PersistentDict
00002 
00003 from zope.interface import implements, implementer
00004 from zope.component import adapts, adapter
00005 from zope.annotation.interfaces import IAnnotations
00006 
00007 from plone.portlets.interfaces import ILocalPortletAssignmentManager
00008 from plone.portlets.interfaces import IPortletAssignmentMapping
00009 from plone.portlets.interfaces import ILocalPortletAssignable
00010 from plone.portlets.interfaces import IPortletManager
00011 
00012 from plone.portlets.storage import PortletAssignmentMapping
00013 from plone.portlets.constants import CONTEXT_ASSIGNMENT_KEY
00014 from plone.portlets.constants import CONTEXT_BLACKLIST_STATUS_KEY
00015 from plone.portlets.constants import CONTEXT_CATEGORY
00016 
00017 from BTrees.OOBTree import OOBTree
00018 
00019 @adapter(ILocalPortletAssignable, IPortletManager)
00020 @implementer(IPortletAssignmentMapping)
00021 def localPortletAssignmentMappingAdapter(context, manager):
00022     """When adapting (context, manager), get an IPortletAssignmentMapping
00023     by finding one in the object's annotations. The container will be created
00024     if necessary.
00025     """
00026     annotations = IAnnotations(context)
00027     local = annotations.get(CONTEXT_ASSIGNMENT_KEY, None)
00028     if local is None:
00029         local = annotations[CONTEXT_ASSIGNMENT_KEY] = OOBTree()
00030     portlets = local.get(manager.__name__, None)
00031     if portlets is None:
00032         portlets = local[manager.__name__] = PortletAssignmentMapping(manager=manager.__name__,
00033                                                                       category=CONTEXT_CATEGORY)
00034     return portlets
00035     
00036 
00037 class LocalPortletAssignmentManager(object):
00038     """Default implementation of ILocalPortletAssignmentManager which stores
00039     information in an annotation.
00040     """
00041     implements(ILocalPortletAssignmentManager)
00042     adapts(ILocalPortletAssignable, IPortletManager)
00043 
00044     def __init__(self, context, manager):
00045         self.context = context
00046         self.manager = manager
00047 
00048     def setBlacklistStatus(self, category, status):
00049         blacklist = self._getBlacklist(True)
00050         blacklist[category] = status
00051     
00052     def getBlacklistStatus(self, category):
00053         blacklist = self._getBlacklist(False)
00054         if blacklist is None:
00055             return None
00056         return blacklist.get(category, None)
00057         
00058     def _getBlacklist(self, create=False):
00059         annotations = IAnnotations(self.context)
00060         local = annotations.get(CONTEXT_BLACKLIST_STATUS_KEY, None)
00061         if local is None:
00062             if create:
00063                 local = annotations[CONTEXT_BLACKLIST_STATUS_KEY] = PersistentDict()
00064             else:
00065                 return None
00066         blacklist = local.get(self.manager.__name__, None)
00067         if blacklist is None:
00068             if create:
00069                 blacklist = local[self.manager.__name__] = PersistentDict()
00070             else:
00071                 return None
00072         return blacklist