Back to index

plone3  3.1.7
controlpanel.py
Go to the documentation of this file.
00001 from zope.component import getUtility
00002 from zope.schema.interfaces import IVocabularyFactory
00003 
00004 from plone.memoize.instance import memoize
00005 
00006 from plone.contentrules.engine.interfaces import IRuleStorage
00007 
00008 from Products.Five.browser import BrowserView
00009 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00010 from Products.CMFPlone import PloneMessageFactory as _
00011 
00012 class ContentRulesControlPanel(BrowserView):
00013     """Manage rules in a the global rules container
00014     """
00015     
00016     template = ViewPageTemplateFile('templates/controlpanel.pt')
00017 
00018     def __call__(self):
00019         form = self.request.form
00020         ruleIds = form.get('ruleId', [])
00021         storage = getUtility(IRuleStorage)
00022         if form.get('form.button.SaveSettings', None) is not None:
00023             storage.active = form.get('global_enable', True)
00024         elif form.get('form.button.EnableRule', None) is not None:
00025             for r in ruleIds:
00026                 if r in storage:
00027                     storage[r].enabled = True
00028         elif form.get('form.button.DisableRule', None) is not None:
00029             for r in ruleIds:
00030                 if r in storage:
00031                     storage[r].enabled = False
00032         elif form.get('form.button.DeleteRule', None) is not None:
00033             for r in ruleIds:
00034                 if r in storage:
00035                     del storage[r]
00036         return self.template()
00037 
00038     def globally_enabled(self):
00039         storage = getUtility(IRuleStorage)
00040         return storage.active 
00041 
00042     def registeredRules(self):
00043         selector = self.request.get('ruleType', 'all')
00044         rules = []
00045         if selector.startswith('state-'):
00046             rules = self._rulesByState(selector[6:] == 'enabled')
00047         elif selector.startswith('trigger-'):
00048             rules = self._rulesByTrigger(selector[8:])
00049         else:
00050             rules = self._getRules()
00051         events = dict([(e.value, e.token) for e in self._events()])
00052         info = []
00053         for r in rules:
00054             info.append(dict(id = r.__name__,
00055                         title = r.title,
00056                         description = r.description,
00057                         enabled = r.enabled,
00058                         trigger = events[r.event]))
00059         return info
00060      
00061     def ruleTypesToShow(self):
00062         selector = []
00063         for event in self._events():
00064             selector.append(dict(id = "trigger-" + event.value.__identifier__,
00065                                  title = _(u"Trigger: ${name}", mapping = {'name' : event.token})),)
00066                                  
00067         selector += ({'id': 'state-enabled', 'title': _(u"label_rule_enabled", default=u"Enabled")},
00068                      {'id': 'state-disabled', 'title': _(u"label_rule_disabled", default=u"Disabled"),},
00069                      # {'id': 'state-rule-assigned', 'title': _(u"Rule is in use")},
00070                      # {'id': 'state-rule-not-assigned', 'title': _(u"Rule is not assigned anywhere"),},
00071                      )
00072         
00073         return selector
00074         
00075     def _rulesByState(self, state):
00076         return [r for r in self._getRules() if r.enabled == state]
00077         
00078     def _rulesByTrigger(self, trigger):
00079         return [r for r in self._getRules() if r.event.__identifier__ == trigger]
00080         
00081     def _getRules(self):
00082         storage = getUtility(IRuleStorage)
00083         return storage.values()
00084         
00085     @memoize
00086     def _events(self):
00087         eventsFactory = getUtility(IVocabularyFactory, name="plone.contentrules.events")
00088         return eventsFactory(self.context)