def acquired_rules(self): # Short circuit if this is the root of the portal if ISiteRoot.providedBy(self.context): return [] in_use = set([r['id'] for r in self.assigned_rules()]) storage = getUtility(IRuleStorage) events = self._events() assignments = [] context = aq_parent(aq_inner(self.context)) while context is not None: assignable = IRuleAssignmentManager(context, None) if assignable is not None: for key, assignment in assignable.items(): if key not in in_use and assignment.bubbles: rule = storage.get(key, None) if rule is not None: assignments.append(dict(id=key, title=rule.title, description=rule.description, trigger=events.get(rule.event, "Unknown"), url=context.absolute_url() + '/@@manage-content-rules', enabled=(assignment.enabled and rule.enabled), )) if ISiteRoot.providedBy(context): context = None else: context = aq_parent(context) return assignments
def assigned_rules(self): assignable = IRuleAssignmentManager(self.context) storage = getUtility(IRuleStorage) events = self._events() assignments = [] for key, assignment in assignable.items(): rule = storage.get(key, None) if rule is not None: assignments.append(dict(id=key, title=rule.title, description=rule.description, trigger=events.get(rule.event, "Unknown"), url=self._rule_url(key), bubbles=assignment.bubbles, enabled=assignment.enabled, global_enabled=rule.enabled, )) return assignments
def assigned_rules(self): assignable = IRuleAssignmentManager(self.context) storage = getUtility(IRuleStorage) events = self._events() assignments = [] for key, assignment in assignable.items(): rule = storage.get(key, None) if rule is not None: assignments.append( dict( id=key, title=rule.title, description=rule.description, trigger=events.get(rule.event, "Unknown"), url=self._rule_url(key), bubbles=assignment.bubbles, enabled=assignment.enabled, global_enabled=rule.enabled, )) return assignments
def availableTriggers(context): # get rules assigned to context assignable = IRuleAssignmentManager(context) # get storage storage = getUtility(IRuleStorage) terms = [] if storage and assignable is not None: # iterate over context rules for key, assignment in assignable.items(): rule = storage.get(key, None) # check if given rule is an IFTTT rule if IFTTTMarker.providedBy(rule): terms.append( SimpleVocabulary.createTerm( rule, key.encode('utf-8'), _(u'${title}', mapping=dict(title=rule.title)))) return SimpleVocabulary(terms)
def acquired_rules(self): # Short circuit if this is the root of the portal if ISiteRoot.providedBy(self.context): return [] in_use = set([r['id'] for r in self.assigned_rules()]) storage = getUtility(IRuleStorage) events = self._events() assignments = [] context = aq_parent(aq_inner(self.context)) while context is not None: assignable = IRuleAssignmentManager(context, None) if assignable is not None: for key, assignment in assignable.items(): if key not in in_use and assignment.bubbles: rule = storage.get(key, None) if rule is not None: assignments.append( dict( id=key, title=rule.title, description=rule.description, trigger=events.get(rule.event, "Unknown"), url=context.absolute_url() + '/@@manage-content-rules', enabled=(assignment.enabled and rule.enabled), )) if ISiteRoot.providedBy(context): context = None else: context = aq_parent(context) return assignments
def _extractRules(self): """Extract rules to a document fragment """ site = self.environ.getSite() storage = queryUtility(IRuleStorage) if storage is None: return fragment = self._doc.createDocumentFragment() assignment_paths = set() for name, rule in storage.items(): rule_node = self._doc.createElement('rule') rule_node.setAttribute('name', name) rule_node.setAttribute('title', rule.title) rule_node.setAttribute('description', rule.description) rule_node.setAttribute('event', _getDottedName(rule.event)) rule_node.setAttribute('enabled', str(rule.enabled)) rule_node.setAttribute('stop-after', str(rule.stop)) rule_node.setAttribute('cascading', str(rule.cascading)) # Aq-wrap so that exporting fields with clever getters or # vocabularies will work. We also aq-wrap conditions and # actions below. rule = rule.__of__(site) # Add conditions conditions_node = self._doc.createElement('conditions') for condition in rule.conditions: condition_data = IRuleElementData(condition) condition = condition.__of__(rule) condition_node = self._doc.createElement('condition') condition_node.setAttribute('type', condition_data.element) handler = IRuleElementExportImportHandler(condition) handler.export_element(self._doc, condition_node) conditions_node.appendChild(condition_node) rule_node.appendChild(conditions_node) # Add actions actions_node = self._doc.createElement('actions') for action in rule.actions: action_data = IRuleElementData(action) action = action.__of__(rule) action_node = self._doc.createElement('action') action_node.setAttribute('type', action_data.element) handler = IRuleElementExportImportHandler(action) handler.export_element(self._doc, action_node) actions_node.appendChild(action_node) rule_node.appendChild(actions_node) fragment.appendChild(rule_node) assignment_paths.update(get_assignments(rule)) # Export assignments last - this is necessary to ensure they # are orderd properly site_path_length = len('/'.join(site.getPhysicalPath())) for path in assignment_paths: try: container = site.unrestrictedTraverse(path) except KeyError: continue assignable = IRuleAssignmentManager(container, None) if assignable is None: continue location = path[site_path_length:] for name, assignment in assignable.items(): assignment_node = self._doc.createElement('assignment') assignment_node.setAttribute('location', location) assignment_node.setAttribute('name', name) assignment_node.setAttribute('enabled', str(assignment.enabled)) assignment_node.setAttribute('bubbles', str(assignment.bubbles)) fragment.appendChild(assignment_node) return fragment
def _extractRules(self): """Extract rules to a document fragment """ site = self.environ.getSite() storage = queryUtility(IRuleStorage) if storage is None: return fragment = self._doc.createDocumentFragment() assignment_paths = set() for name, rule in storage.items(): rule_node = self._doc.createElement('rule') rule_node.setAttribute('name', name) rule_node.setAttribute('title', rule.title) rule_node.setAttribute('description', rule.description) rule_node.setAttribute('event', _getDottedName(rule.event)) rule_node.setAttribute('enabled', str(rule.enabled)) rule_node.setAttribute('stop-after', str(rule.stop)) rule_node.setAttribute('cascading', str(rule.cascading)) # Aq-wrap so that exporting fields with clever getters or # vocabularies will work. We also aq-wrap conditions and # actions below. rule = rule.__of__(site) # Add conditions conditions_node = self._doc.createElement('conditions') for condition in rule.conditions: condition_data = IRuleElementData(condition) condition = condition.__of__(rule) condition_node = self._doc.createElement('condition') condition_node.setAttribute('type', condition_data.element) handler = IRuleElementExportImportHandler(condition) handler.export_element(self._doc, condition_node) conditions_node.appendChild(condition_node) rule_node.appendChild(conditions_node) # Add actions actions_node = self._doc.createElement('actions') for action in rule.actions: action_data = IRuleElementData(action) action = action.__of__(rule) action_node = self._doc.createElement('action') action_node.setAttribute('type', action_data.element) handler = IRuleElementExportImportHandler(action) handler.export_element(self._doc, action_node) actions_node.appendChild(action_node) rule_node.appendChild(actions_node) fragment.appendChild(rule_node) assignment_paths.update(get_assignments(rule)) # Export assignments last - this is necessary to ensure they # are orderd properly site_path_length = len('/'.join(site.getPhysicalPath())) for path in assignment_paths: try: container = site.unrestrictedTraverse(path) except KeyError: continue assignable = IRuleAssignmentManager(container, None) if assignable is None: continue location = path[site_path_length:] for name, assignment in assignable.items(): assignment_node = self._doc.createElement('assignment') assignment_node.setAttribute('location', location) assignment_node.setAttribute('name', name) assignment_node.setAttribute('enabled', str(assignment.enabled)) assignment_node.setAttribute('bubbles', str(assignment.bubbles)) fragment.appendChild(assignment_node) return fragment