Back to index

plone3  3.1.7
elements.py
Go to the documentation of this file.
00001 from zope.component import getMultiAdapter, getUtilitiesFor, getUtility
00002 from zope.schema.interfaces import IVocabularyFactory
00003 
00004 from plone.contentrules.rule.interfaces import IRuleAction, IRuleCondition, IRuleElementData
00005 from plone.contentrules.engine import utils
00006 
00007 from plone.app.contentrules.rule import get_assignments
00008 
00009 from plone.memoize.instance import memoize 
00010 
00011 from Acquisition import aq_inner, aq_parent
00012 from Products.Five.browser import BrowserView
00013 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile 
00014 from Products.statusmessages.interfaces import IStatusMessage
00015 from Products.CMFCore.utils import getToolByName
00016 from Products.CMFCore.interfaces import ICatalogTool
00017 from Products.CMFCore.interfaces import ISiteRoot
00018 from Products.CMFPlone import PloneMessageFactory as _
00019 
00020 class ManageElements(BrowserView):
00021     """Manage elements in a rule
00022     """
00023     
00024     template = ViewPageTemplateFile('templates/manage-elements.pt')
00025     
00026     def __call__(self):
00027         redirect = False
00028         form = self.request.form
00029         idx = form.get('element_id', 0)
00030         
00031         rule = aq_inner(self.context)
00032         status = IStatusMessage(self.request)
00033         
00034         if 'form.button.Save' in form:
00035             rule.title = form.get('title', rule.title)
00036             rule.description = form.get('description', rule.description)
00037             status.addStatusMessage(_(u"Changes saved."), type='info')
00038         elif 'form.button.EditCondition' in form:
00039             editview = self.conditions()[idx]['editview']
00040             self.request.response.redirect(editview)
00041             redirect = True
00042         elif 'form.button.DeleteCondition' in form:
00043             del rule.conditions[idx]
00044             status.addStatusMessage(_(u"Condition deleted."), type='info')
00045         elif 'form.button.MoveConditionUp' in form:
00046             self._move_up(rule.conditions, idx)
00047             status.addStatusMessage(_(u"Condition moved up."), type='info')
00048         elif 'form.button.MoveConditionDown' in form:
00049             self._move_down(rule.conditions, idx)
00050             status.addStatusMessage(_(u"Condition moved down."), type='info')
00051             
00052         elif 'form.button.EditAction' in form:
00053             editview = self.actions()[idx]['editview']
00054             self.request.response.redirect(editview)
00055             redirect = True
00056         elif 'form.button.DeleteAction' in form:
00057             del rule.actions[idx]
00058             status.addStatusMessage(_(u"Action deleted."), type='info')
00059         elif 'form.button.MoveActionUp' in form:
00060             self._move_up(rule.actions, idx)
00061             status.addStatusMessage(_(u"Action moved up."), type='info')
00062         elif 'form.button.MoveActionDown' in form:
00063             self._move_down(rule.actions, idx)
00064             status.addStatusMessage(_(u"Action moved down."), type='info')
00065     
00066         if not redirect:
00067             return self.template()
00068     
00069     @memoize
00070     def base_url(self):
00071         rule = aq_inner(self.context)
00072         return rule.absolute_url()
00073     
00074     @memoize
00075     def view_url(self):
00076         return self.base_url() + '/@@manage-elements'
00077     
00078     def rule_title(self):
00079         return self.context.title
00080         
00081     def rule_description(self):
00082         return self.context.description
00083         
00084     def rule_event(self):
00085         eventsFactory = getUtility(IVocabularyFactory, name="plone.contentrules.events")
00086         for e in eventsFactory(self.context):
00087             if e.value == self.context.event:
00088                 return e.token
00089         return "Unknown event" # should not happen
00090     
00091     @memoize
00092     def actions(self):
00093         actions = {}
00094         for name, utility in getUtilitiesFor(IRuleAction):
00095             actions[name] = utility    
00096         return self._populate_info(self.context.actions, actions, 'action')
00097     
00098     @memoize    
00099     def conditions(self):
00100         conditions = {}
00101         for name, utility in getUtilitiesFor(IRuleCondition):
00102             conditions[name] = utility
00103         return self._populate_info(self.context.conditions, conditions, 'condition')
00104     
00105     def addable_conditions(self):
00106         rule = aq_inner(self.context)
00107         context = aq_parent(rule)
00108         baseUrl = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00109         
00110         info = []
00111         for element in utils.allAvailableConditions(rule.event):
00112             info.append({'title'       : element.title,
00113                          'description' : element.description,
00114                          'addview'    :  element.addview,
00115                         })
00116         return info
00117     
00118     def addable_actions(self):
00119         rule = aq_inner(self.context)
00120         context = aq_parent(rule)
00121         baseUrl = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00122         
00123         info = []
00124         for element in utils.allAvailableActions(rule.event):
00125             info.append({'title'       : element.title,
00126                          'description' : element.description,
00127                          'addview'     :  element.addview,
00128                         })
00129         return info
00130                 
00131     def assignments(self):
00132         rule = aq_inner(self.context)
00133         paths = set(get_assignments(rule))
00134         
00135         site = getToolByName(rule, "portal_url").getPortalObject()
00136         site_path = '/'.join(site.getPhysicalPath())
00137         
00138         plone_view = getMultiAdapter((rule, self.request), name="plone")
00139         
00140         info = []
00141         if site_path in paths:
00142             paths.remove(site_path)
00143             info.append({'url'         : site.absolute_url(),
00144                          'title'       : site.Title(),
00145                          'description' : site.Description(),
00146                          'icon'        : plone_view.getIcon(site),
00147                         })
00148             
00149         catalog = getToolByName(rule, "portal_catalog")
00150         for a in catalog(path=dict(query=list(paths), depth=0), sort_on='sortable_title'):
00151             info.append({'url'         : a.getURL(),
00152                          'title'       : a.Title,
00153                          'description' : a.Description,
00154                          'icon'        : plone_view.getIcon(a),
00155                         })
00156         return info
00157         
00158         
00159     def _populate_info(self, elements, meta, namespace):
00160         """Given an actual list of actions/conditions (elements) and a dict
00161         mapping element type names to instances (meta), and a namespace
00162         ('action' or 'condition'), return a list of dicts usable by the view
00163         template.
00164         """
00165         rule = aq_inner(self.context)
00166         context = aq_parent(rule)
00167         
00168         url = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00169         base_url = self.base_url()
00170         
00171         info = []
00172         
00173         last = len(elements) - 1
00174         for idx in range(len(elements)):
00175             e = elements[idx]
00176             
00177             data = IRuleElementData(e)
00178             descriptor = meta[data.element]
00179             
00180             editview = None
00181             if descriptor.editview:
00182                 editview = '%s/++%s++%d/%s' % (base_url, namespace, idx, descriptor.editview,)
00183             
00184             info.append({'title'       : descriptor.title,
00185                          'description' : descriptor.description,
00186                          'summary'     : data.summary,
00187                          'editview'    : editview,
00188                          'first'       : (idx == 0),
00189                          'last'        : (idx == last),
00190                          'idx'         : idx,
00191                         })
00192         return info
00193         
00194     def _move_up(self, elements, idx):
00195         element = elements[idx]
00196         del elements[idx]
00197         elements.insert(idx - 1, element)
00198         
00199     def _move_down(self, elements, idx):
00200         element = elements[idx]
00201         del elements[idx]
00202         elements.insert(idx + 1, element)