Back to index

plone3  3.1.7
rule.py
Go to the documentation of this file.
00001 from zope.component import queryUtility, getUtility
00002 from zope.annotation.interfaces import IAnnotations
00003 
00004 from Acquisition import aq_base
00005 
00006 from plone.contentrules.engine.interfaces import IRuleStorage
00007 from plone.contentrules.engine.interfaces import IRuleAssignmentManager
00008 
00009 from plone.contentrules.rule.rule import Rule as BaseRule
00010 
00011 from OFS.SimpleItem import SimpleItem
00012 from BTrees.OOBTree import OOSet
00013 from Products.CMFCore.interfaces import ISiteRoot
00014 
00015 ANNOTATION_KEY = "plone.app.contentrules.ruleassignments"
00016 
00017 class Rule(SimpleItem, BaseRule):
00018     """A Zope 2 version of a rule, subject to acqusition, but otherwise
00019     identical.
00020     """
00021     
00022     __name__ = u""
00023     
00024     @property
00025     def id(self):
00026         return '++rule++%s' % self.__name__
00027 
00028 def get_assignments(rule):
00029     annotations = IAnnotations(rule)
00030     return annotations.setdefault(ANNOTATION_KEY, OOSet())
00031     
00032 # Events that keep track of rule-to-assignment mappings
00033     
00034 def rule_removed(rule, event):
00035     
00036     storage = queryUtility(IRuleStorage)
00037     rule_name = rule.__name__
00038     
00039     if storage is None:
00040         return
00041     
00042     portal = getUtility(ISiteRoot)
00043     for path in get_assignments(rule):
00044         container = portal.unrestrictedTraverse(path)
00045         if container is not None:
00046             assignable = IRuleAssignmentManager(container, None)
00047             if assignable is not None and rule_name in assignable:
00048                 del assignable[rule_name]
00049     
00050 def container_moved(container, event):
00051     
00052     if event.oldParent is None or event.newParent is None or event.oldName is None:
00053         return
00054     
00055     assignable = IRuleAssignmentManager(container, None)
00056     storage = queryUtility(IRuleStorage)
00057     
00058     if assignable is None or storage is None:
00059         return
00060     
00061     old_path = "%s/%s" % ('/'.join(event.oldParent.getPhysicalPath()), event.oldName,)
00062     new_path = '/'.join(container.getPhysicalPath())
00063     
00064     if aq_base(event.object) is not aq_base(container):
00065         new_path_of_moved = '/'.join(event.object.getPhysicalPath())
00066         old_path = old_path + new_path[len(new_path_of_moved):]
00067     
00068     for rule_name in assignable.keys():
00069         rule = storage.get(rule_name, None)
00070         if rule is not None:
00071             assignments = get_assignments(rule)
00072             if old_path in assignments:
00073                 assignments.remove(old_path)
00074                 assignments.insert(new_path)
00075     
00076 def container_removed(container, event):
00077     
00078     assignable = IRuleAssignmentManager(container, None)
00079     storage = queryUtility(IRuleStorage)
00080     
00081     if assignable is None or storage is None:
00082         return
00083     
00084     path = '/'.join(container.getPhysicalPath())
00085     for rule_name in assignable.keys():
00086         rule = storage.get(rule_name, None)
00087         if rule is not None:
00088             assignments = get_assignments(rule)
00089             if path in assignments:
00090                 assignments.remove(path)