Back to index

plone3  3.1.7
assignments.py
Go to the documentation of this file.
00001 from zope.component import getUtility, getMultiAdapter
00002 from zope.schema.interfaces import IVocabularyFactory
00003 
00004 from Acquisition import aq_inner, aq_parent
00005 
00006 from Products.Five.browser import BrowserView
00007 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00008 from Products.CMFCore.interfaces import ISiteRoot
00009 from Products.CMFPlone import PloneMessageFactory as _
00010 from Products.statusmessages.interfaces import IStatusMessage
00011 
00012 from plone.contentrules.engine.interfaces import IRuleStorage
00013 from plone.contentrules.engine.interfaces import IRuleAssignmentManager
00014 
00015 from plone.contentrules.engine.assignments import RuleAssignment
00016 from plone.app.contentrules.rule import get_assignments
00017 
00018 from plone.memoize.instance import memoize
00019 
00020 class ManageAssignments(BrowserView):
00021     """Manage contextual rule assignments
00022     """
00023     
00024     template = ViewPageTemplateFile('templates/manage-assignments.pt')
00025 
00026     def __call__(self):
00027         context = aq_inner(self.context)
00028         request = aq_inner(self.request)
00029         form = request.form
00030         path = '/'.join(context.getPhysicalPath())
00031         status = IStatusMessage(self.request)
00032         assignable = IRuleAssignmentManager(context)
00033         storage = getUtility(IRuleStorage)
00034         
00035         operation = request.get('operation', None)
00036         
00037         if operation == 'move_up':
00038             rule_id = request.get('rule_id')            
00039             keys = list(assignable.keys())
00040             idx = keys.index(rule_id)
00041             del keys[idx]
00042             keys.insert(idx-1, rule_id)
00043             assignable.updateOrder(keys)
00044         elif operation == 'move_down':
00045             rule_id = request.get('rule_id')            
00046             keys = list(assignable.keys())
00047             idx = keys.index(rule_id)
00048             del keys[idx]
00049             keys.insert(idx+1, rule_id)
00050             assignable.updateOrder(keys)            
00051         elif 'form.button.AddAssignment' in form:
00052             rule_id = form.get('rule_id')
00053             assignable[rule_id] = RuleAssignment(rule_id)
00054             get_assignments(storage[rule_id]).insert(path)
00055         elif 'form.button.Delete' in form:
00056             rule_ids = form.get('rule_ids', ())
00057             for r in rule_ids:
00058                 del assignable[r]
00059                 get_assignments(storage[r]).remove(path)
00060             status.addStatusMessage(_(u"Assignments deleted."), type='info')
00061         elif 'form.button.Enable' in form:
00062             rule_ids = form.get('rule_ids', ())
00063             for r in rule_ids:
00064                 assignment = assignable.get(r, None)
00065                 if assignment is not None:
00066                     assignment.enabled = True
00067             status.addStatusMessage(_(u"Assignments enabled."), type='info')
00068         elif 'form.button.Disable' in form:
00069             rule_ids = form.get('rule_ids', ())
00070             for r in rule_ids:
00071                 assignment = assignable.get(r, None)
00072                 if assignment is not None:
00073                     assignment.enabled = False
00074             status.addStatusMessage(_(u"Assignments disabled."), type='info')
00075         elif 'form.button.Bubble' in form:
00076             rule_ids = form.get('rule_ids', ())
00077             for r in rule_ids:
00078                 assignment = assignable.get(r, None)
00079                 if assignment is not None:
00080                     assignment.bubbles = True
00081             status.addStatusMessage(_(u"Changes saved."), type='info')
00082         elif 'form.button.NoBubble' in form:
00083             rule_ids = form.get('rule_ids', ())
00084             for r in rule_ids:
00085                 assignment = assignable.get(r, None)
00086                 if assignment is not None:
00087                     assignment.bubbles = False
00088             status.addStatusMessage(_(u"Changes saved."), type='info')
00089                                 
00090         return self.template()
00091         
00092     def globally_enabled(self):
00093         storage = getUtility(IRuleStorage)
00094         return storage.active
00095         
00096     @memoize
00097     def view_url(self):
00098         return self.context.absolute_url() + '/@@manage-content-rules'
00099         
00100     @memoize
00101     def type_name(self):
00102         context = aq_inner(self.context)
00103         fti = context.getTypeInfo()
00104         return fti.getProperty('title', None) or fti.getId()
00105 
00106     @memoize
00107     def acquired_rules(self):
00108         
00109         # Short circuit if this is the root of the portal
00110         if ISiteRoot.providedBy(self.context):
00111             return []
00112         
00113         in_use = set([r['id'] for r in self.assigned_rules()])
00114         
00115         storage = getUtility(IRuleStorage)
00116         events = self._events()
00117         
00118         assignments = []
00119         context = aq_parent(aq_inner(self.context))
00120         
00121         while context is not None:
00122             assignable = IRuleAssignmentManager(context, None)
00123             if assignable is not None:
00124                 for key, assignment in assignable.items():
00125                     if key not in in_use and assignment.bubbles:
00126                         rule = storage.get(key, None)
00127                         if rule is not None:
00128                             assignments.append(dict(id = key,
00129                                                     title = rule.title,
00130                                                     description = rule.description,
00131                                                     trigger = events.get(rule.event, "Unknown"),
00132                                                     url = context.absolute_url() + '/@@manage-content-rules',
00133                                                     enabled = (assignment.enabled and rule.enabled),))
00134             if ISiteRoot.providedBy(context):
00135                 context = None
00136             else:
00137                 context = aq_parent(context)
00138         
00139         return assignments
00140     
00141     @memoize
00142     def assigned_rules(self):
00143         assignable = IRuleAssignmentManager(self.context)
00144         storage = getUtility(IRuleStorage)
00145         events = self._events()
00146         
00147         assignments = []
00148         for key, assignment in assignable.items():
00149             rule = storage.get(key, None)
00150             if rule is not None:
00151                 assignments.append(dict(id = key,
00152                                         title = rule.title,
00153                                         description = rule.description,
00154                                         trigger = events.get(rule.event, "Unknown"),
00155                                         url = self._rule_url(key),
00156                                         bubbles = assignment.bubbles,
00157                                         enabled = assignment.enabled,
00158                                         global_enabled = rule.enabled,))
00159         return assignments
00160         
00161     def has_rules(self):
00162         return len(self.assigned_rules()) > 0 or len(self.acquired_rules()) > 0
00163         
00164     def assignable_rules(self):
00165         in_use = set([r['id'] for r in self.assigned_rules()])
00166         assignable = []
00167         for key, rule in getUtility(IRuleStorage).items():
00168             if key not in in_use:
00169                 assignable.append(dict( id = key,
00170                                         title = rule.title,
00171                                         description = rule.description,))
00172         return assignable
00173         
00174     @memoize
00175     def _events(self):
00176         eventsFactory = getUtility(IVocabularyFactory, name="plone.contentrules.events")
00177         return dict([(e.value, e.token) for e in eventsFactory(self.context)])
00178         
00179     def _rule_url(self, key):
00180         return "%s/++rule++%s/@@manage-elements" % (self._portal_url(), key,)
00181         
00182     @memoize
00183     def _portal_url(self):
00184         portal_state = getMultiAdapter((self.context, self.request), name="plone_portal_state")
00185         return portal_state.portal_url()