Back to index

plone3  3.1.7
assignments.py
Go to the documentation of this file.
00001 from persistent import Persistent
00002 
00003 from zope.interface import implements, implementer
00004 from zope.component import adapter, queryUtility
00005 
00006 from zope.annotation.interfaces import IAnnotations
00007 from zope.app.container.ordered import OrderedContainer
00008 from zope.app.container.contained import Contained
00009 
00010 from plone.contentrules.engine.interfaces import IRuleStorage
00011 from plone.contentrules.engine.interfaces import IRuleAssignable
00012 from plone.contentrules.engine.interfaces import IRuleAssignment
00013 from plone.contentrules.engine.interfaces import IRuleAssignmentManager
00014 
00015 from BTrees.OOBTree import OOBTree
00016 
00017 KEY = 'plone.contentrules.localassignments'
00018     
00019 class RuleAssignment(Contained, Persistent):
00020     """An assignment of a rule to a context
00021     """
00022     implements(IRuleAssignment)
00023     
00024     def __init__(self, ruleid, enabled=True, bubbles=False):
00025         super(RuleAssignment, self).__init__()
00026         self.__name__ = ruleid
00027         self.enabled = enabled
00028         self.bubbles = bubbles
00029     
00030 class RuleAssignmentManager(OrderedContainer):
00031     """A context-specific container for rule assignments
00032     """
00033     implements(IRuleAssignmentManager)
00034     
00035     def __init__(self):
00036         # XXX: This depends on implementation detail in OrderedContainer,
00037         # but it uses a PersistentDict, which sucks :-/
00038         OrderedContainer.__init__(self)
00039         self._data = OOBTree()
00040 
00041     def getRules(self, event, bubbled=False):
00042         rules = []
00043         storage = queryUtility(IRuleStorage)
00044         if storage is not None:
00045             for a in self.values():
00046                 if a.enabled and (bubbled == False or a.bubbles):
00047                     r = storage.get(a.__name__, None)
00048                     if r is not None and r.enabled and r.event.providedBy(event):
00049                         rules.append(r)
00050         return rules
00051 
00052 @adapter(IRuleAssignable)
00053 @implementer(IRuleAssignmentManager)
00054 def ruleAssignmentManagerAdapterFactory(context):
00055     """When adapting an IRuleAssignable, get an IRuleAssignmentManager by 
00056     finding one in the object's annotations. The container will be created 
00057     if necessary.
00058     """
00059     annotations = IAnnotations(context)
00060     manager = annotations.get(KEY, None)
00061     if manager is None:
00062         manager = annotations[KEY] = RuleAssignmentManager()
00063     return manager