def setUp(self):
        self.portal = self.layer['portal']
        self.request = self.layer['request']
        login(self.portal, TEST_USER_NAME)
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Folder', 'f1')
        self.folder = self.portal['f1']
        self.folder.invokeFactory('Document', 'd1')
        self.portal.invokeFactory('Folder', 'target')
        self.folder.invokeFactory('Folder', 'f1')
        self.folder.f1.invokeFactory('Folder', 'f11')
        self.folder.f1.invokeFactory('Folder', 'f12')

        self.storage = getUtility(IRuleStorage)
        self.storage['r1'] = Rule()
        self.storage['r2'] = Rule()
        self.storage['r3'] = Rule()

        self.f11a = IRuleAssignmentManager(self.folder.f1.f11)
        self.f11a['r1'] = RuleAssignment('r1', bubbles=True)
        insert_assignment(self.storage['r1'],
                          '/'.join(self.folder.f1.f11.getPhysicalPath()))

        self.f12a = IRuleAssignmentManager(self.folder.f1.f12)
        self.f12a['r1'] = RuleAssignment('r1', bubbles=True)
        insert_assignment(self.storage['r1'],
                          '/'.join(self.folder.f1.f12.getPhysicalPath()))

        self.f12a['r2'] = RuleAssignment('r2', bubbles=True)
        insert_assignment(self.storage['r2'],
                          '/'.join(self.folder.f1.f12.getPhysicalPath()))
Ejemplo n.º 2
0
    def createRule(self, context, rule_context, rule_id, rule_title, rule_description,
                   rule_event, message, subject, for_types=None, area_id=None):
        """
        Enabled types are taken from the Plone registry, or you can manually set them using for_types
        Id of the area (used for getting the notification group) is taken from current area, or by area_id param
        """
        registry = queryUtility(IRegistry)
        settings = registry.forInterface(IGroupwareNotifySettings, check=False)
        #create the rule
        rule = Rule()
        rule.__name__ = rule_id
        rule.event = rule_event
        rule.title = rule_title
        rule.description = rule_description
        #add the rule to rule's storage
        storage = getUtility(IRuleStorage)
        # chooser = INameChooser(storage)
        if rule_id not in storage.keys():
            storage[rule_id] = rule
            #set the action and add it to the rule
            action = MailForGroupwareNotificationAction(area_id)
            if settings and settings.default_email_sender:
                action.source = settings.default_email_sender
            action.sender = None
            action.subject = subject
            action.message = message
            rule.actions.append(action)
            if not for_types:
                #set the condition and add it to the rule
                if settings:

                    behavior = ISelectableConstrainTypes(rule_context)
                    allowed_types = behavior.getLocallyAllowedTypes()
                    types_list =  set(allowed_types).difference(settings.black_list)
                    condition = PortalTypeCondition()
                    condition.check_types=tuple(types_list)
                    rule.conditions.append(condition)
            else:
                # explicit types
                condition=PortalTypeCondition()
                condition.check_types=tuple(for_types)
                rule.conditions.append(condition)

            logger.info('Created rule %s' % rule_id)

        #assignment
        rule_id=rule.id.replace('++rule++','')
        assignable = IRuleAssignmentManager(rule_context)
        assignable[rule_id] = RuleAssignment(rule_id)
        assignable[rule_id].bubbles=True
        get_assignments(storage[rule_id]).insert('/'.join(rule_context.getPhysicalPath()))
        logger.info('Enabled rule %s on %s' % (rule_id, rule_context.Title().decode('utf-8')))
def on_create(obj, event):
    """
    temporary disabled
    """
    storage = getUtility(IRuleStorage)
    assignable = IRuleAssignmentManager(obj)
    for rule_id in [
            "booking-accepted",
            "booking-moved",
            "booking-created-user",
            "booking-refuse",
    ]:

        assignable[rule_id] = RuleAssignment(rule_id)
        assignable[rule_id].bubbles = True
        get_assignments(storage[rule_id]).insert("/".join(
            obj.getPhysicalPath()))
Ejemplo n.º 4
0
def _activate_rule(rule_id, context=None):
    storage = component.getUtility(IRuleStorage)
    rule = storage.get(rule_id)
    assignable = IRuleAssignmentManager(context)
    assignment = assignable.get(rule_id, None)
    if not assignment:
        assignment = assignable[rule_id] = RuleAssignment(rule_id)
    assignment.enabled = True
    assignment.bubbles = True
    get_assignments(rule).insert('/'.join(context.getPhysicalPath()))
Ejemplo n.º 5
0
def assign_rule(container,
                rule_id,
                enabled=True,
                bubbles=True,
                insert_before=None):
    """Assign
       @param string rule_id
       rule to
       @param object container
    with options
       @param bool enabled
       @param bool bubbles (apply in subfolders)
       @param string insert-before
    """
    storage = queryUtility(IRuleStorage)
    if storage is None:
        return

    assignable = IRuleAssignmentManager(container, None)
    if assignable is None:
        return

    assignment = assignable.get(rule_id, None)
    if assignment is None:
        assignable[rule_id] = RuleAssignment(rule_id)

    assignable[rule_id].enabled = bool(enabled)
    assignable[rule_id].bubbles = bool(bubbles)
    path = '/'.join(container.getPhysicalPath())
    insert_assignment(storage[rule_id], path)

    if insert_before:
        position = None
        keys = list(assignable.keys())
        if insert_before == '*':
            position = 0
        elif insert_before in keys:
            position = keys.index(insert_before)

        if position is not None:
            keys.remove(rule_id)
            keys.insert(position, rule_id)
            assignable.updateOrder(keys)
Ejemplo n.º 6
0
    def afterSetUp(self):
        self.folder.invokeFactory('Folder', 'f1')
        self.folder.f1.invokeFactory('Folder', 'f11')
        self.folder.f1.invokeFactory('Folder', 'f12')

        self.storage = getUtility(IRuleStorage)
        self.storage['r1'] = Rule()
        self.storage['r2'] = Rule()

        self.f11a = IRuleAssignmentManager(self.folder.f1.f11)
        self.f11a['r1'] = RuleAssignment('r1')
        get_assignments(self.storage['r1']).insert('/'.join(self.folder.f1.f11.getPhysicalPath()))

        self.f12a = IRuleAssignmentManager(self.folder.f1.f12)
        self.f12a['r1'] = RuleAssignment('r1')
        get_assignments(self.storage['r1']).insert('/'.join(self.folder.f1.f12.getPhysicalPath()))

        self.f12a['r2'] = RuleAssignment('r2')
        get_assignments(self.storage['r2']).insert('/'.join(self.folder.f1.f12.getPhysicalPath()))
Ejemplo n.º 7
0
def assign_rule(container, rule_id, enabled=True, bubbles=True,
                insert_before=None):
    """Assign
       @param string rule_id
       rule to
       @param object container
    with options
       @param bool enabled
       @param bool bubbles (apply in subfolders)
       @param string insert-before
    """
    storage = queryUtility(IRuleStorage)
    if storage is None:
        return

    assignable = IRuleAssignmentManager(container, None)
    if assignable is None:
        return

    assignment = assignable.get(rule_id, None)
    if assignment is None:
        assignable[rule_id] = RuleAssignment(rule_id)

    assignable[rule_id].enabled = bool(enabled)
    assignable[rule_id].bubbles = bool(bubbles)
    path = '/'.join(container.getPhysicalPath())
    insert_assignment(storage[rule_id], path)

    if insert_before:
        position = None
        keys = list(assignable.keys())
        if insert_before == "*":
            position = 0
        elif insert_before in keys:
            position = keys.index(insert_before)

        if position is not None:
            keys.remove(rule_id)
            keys.insert(position, rule_id)
            assignable.updateOrder(keys)
Ejemplo n.º 8
0
    def activate(self):
        """
        1) make sure condition is enabled for transition
        2) enable at root and bubble to item below
        """
        c = WorkflowTransitionCondition()
        c.wf_transitions = [self.transition.id]
        self.rule.conditions = [c]
        self.rule.event = IActionSucceededEvent

        assignable = IRuleAssignmentManager(self.portal)
        path = '/'.join(self.portal.getPhysicalPath())
        assignable[self.rule.__name__] = RuleAssignment(self.rule.id,
                                                        enabled=True,
                                                        bubbles=True)
        assignments = get_assignments(self.rule)
        if not path in assignments:
            assignments.insert(path)
Ejemplo n.º 9
0
def assignContentRulesToContainer(container, storage, bubbles):
    '''Assign the standard EDRN content events to the given container.'''
    assignmentManager = IRuleAssignmentManager(container, None)
    if assignmentManager is None:
        # This container doesn't support content rules, so skip it.
        return
    for eventName in _edrnContentEvents:
        assignment = assignmentManager.get(eventName, None)
        if assignment is None:
            # Brand new assignment
            assignment = assignmentManager[eventName] = RuleAssignment(
                eventName)
        if not assignment.enabled:
            assignment.enabled = True
        if assignment.bubbles != bubbles:
            assignment.bubbles = bubbles
        path = '/'.join(container.getPhysicalPath())
        get_assignments(storage[eventName]).insert(path)
def _assignContentRules(self, rule_id, f, langs):
    log.info("_assignContentRules")
    for lang, dummy in langs:
        parents = [
            '%s/campaigns/ew2006/' % lang,
            '%s/campaigns/ew2007/' % lang,
            '%s/campaigns/hw2008/' % lang,
            '%s/campaigns/hwi/' % lang,
            '%s/good_practice/' % lang,
            '%s/oshnetwork/member-states/' % lang,
            '%s/topics/business/' % lang,
            '%s/riskobservatory/' % lang,
        ]

        storage = component.queryUtility(IRuleStorage)
        rule = storage.get(rule_id)
        portal_obj = self.portal_url.getPortalObject()
        for p in parents:
            try:
                parent = portal_obj.unrestrictedTraverse(p)
            except:
                log.info(
                    "Couldn't find folder %s for adding content rule %s \n" %
                    (parent.absolute_url(), rule_id))
                f.write(
                    "Couldn't find folder %s for adding content rule %s \n" %
                    (parent.absolute_url(), rule_id))
                continue

            # XXX DOUBLE CHECK!!!!
            assignments = IRuleAssignmentManager(parent, None)
            get_assignments(storage[rule_id]).insert('/'.join(
                parent.getPhysicalPath()))
            rule_ass = RuleAssignment(ruleid=rule_id,
                                      enabled=True,
                                      bubbles=True)
            assignments[rule_id] = rule_ass
            f.write("Content Rule '%s' assigned to %s \n" %
                    (rule_id, parent.absolute_url()))
            log.info("Content Rule '%s' assigned to %s \n" %
                     (rule_id, parent.absolute_url()))
Ejemplo n.º 11
0
    def _initRules(self, node):
        """Import rules from the given node
        """

        site = self.environ.getSite()
        storage = queryUtility(IRuleStorage)
        if storage is None:
            return

        for child in node.childNodes:
            if child.nodeName == 'rule':

                rule = None
                name = child.getAttribute('name')
                if name:
                    rule = storage.get(name, None)

                if rule is None:
                    rule = Rule()

                    if not name:
                        chooser = INameChooser(storage)
                        name = chooser.chooseName(None, rule)

                    storage[name] = rule
                else:
                    # Clear out conditions and actions since we're expecting new ones
                    del rule.conditions[:]
                    del rule.actions[:]

                rule.title = child.getAttribute('title')
                rule.description = child.getAttribute('description')
                event_name = child.getAttribute('event')
                rule.event = _resolveDottedName(event_name)
                if not rule.event:
                    raise ImportError("Can not import %s" % event_name)

                rule.enabled = as_bool(child.getAttribute('enabled'), True)
                rule.stop = as_bool(child.getAttribute('stop-after'))

                # Aq-wrap to enable complex setters for elements below
                # to work

                rule = rule.__of__(site)

                for rule_config_node in child.childNodes:
                    if rule_config_node.nodeName == 'conditions':
                        for condition_node in rule_config_node.childNodes:
                            if not condition_node.nodeName == 'condition':
                                continue

                            type_ = condition_node.getAttribute('type')
                            element_type = getUtility(IRuleCondition,
                                                      name=type_)
                            if element_type.factory is None:
                                continue

                            condition = element_type.factory()

                            # Aq-wrap in case of complex setters
                            condition = condition.__of__(rule)

                            handler = IRuleElementExportImportHandler(
                                condition)
                            handler.import_element(condition_node)

                            rule.conditions.append(aq_base(condition))

                    elif rule_config_node.nodeName == 'actions':
                        for action_node in rule_config_node.childNodes:
                            if not action_node.nodeName == 'action':
                                continue

                            type_ = action_node.getAttribute('type')
                            element_type = getUtility(IRuleAction, name=type_)
                            if element_type.factory is None:
                                continue

                            action = element_type.factory()

                            # Aq-wrap in case of complex setters
                            action = action.__of__(rule)

                            handler = IRuleElementExportImportHandler(action)
                            handler.import_element(action_node)

                            rule.actions.append(aq_base(action))

            elif child.nodeName == 'assignment':
                location = child.getAttribute('location')
                if location.startswith("/"):
                    location = location[1:]

                try:
                    container = site.unrestrictedTraverse(str(location))
                except KeyError:
                    continue

                assignable = IRuleAssignmentManager(container, None)
                if assignable is None:
                    continue

                name = child.getAttribute('name')
                assignment = assignable.get(name, None)
                if assignment is None:
                    assignment = assignable[name] = RuleAssignment(name)

                assignment.enabled = as_bool(child.getAttribute('enabled'))
                assignment.bubbles = as_bool(child.getAttribute('bubbles'))

                insert_before = child.getAttribute('insert-before')
                if insert_before:
                    position = None
                    keys = list(assignable.keys())

                    if insert_before == "*":
                        position = 0
                    elif insert_before in keys:
                        position = keys.index(insert_before)

                    if position is not None:
                        keys.remove(name)
                        keys.insert(position, name)
                        assignable.updateOrder(keys)

                path = '/'.join(container.getPhysicalPath())
                get_assignments(storage[name]).insert(path)
Ejemplo n.º 12
0
    def __call__(self):
        context = aq_inner(self.context)
        request = aq_inner(self.request)
        form = request.form
        path = '/'.join(context.getPhysicalPath())
        status = IStatusMessage(self.request)
        assignable = IRuleAssignmentManager(context)
        storage = getUtility(IRuleStorage)

        operation = request.get('operation', None)

        if operation == 'move_up':
            rule_id = request.get('rule_id')
            keys = list(assignable.keys())
            idx = keys.index(rule_id)
            del keys[idx]
            keys.insert(idx - 1, rule_id)
            assignable.updateOrder(keys)
        elif operation == 'move_down':
            rule_id = request.get('rule_id')
            keys = list(assignable.keys())
            idx = keys.index(rule_id)
            del keys[idx]
            keys.insert(idx + 1, rule_id)
            assignable.updateOrder(keys)
        elif 'form.button.AddAssignment' in form:
            rule_id = form.get('rule_id')
            assignable[rule_id] = RuleAssignment(rule_id)
            get_assignments(storage[rule_id]).insert(path)
        elif 'form.button.Delete' in form:
            rule_ids = form.get('rule_ids', ())
            for r in rule_ids:
                del assignable[r]
                get_assignments(storage[r]).remove(path)
            status.addStatusMessage(_(u"Assignments deleted."), type='info')
        elif 'form.button.Enable' in form:
            rule_ids = form.get('rule_ids', ())
            for r in rule_ids:
                assignment = assignable.get(r, None)
                if assignment is not None:
                    assignment.enabled = True
            status.addStatusMessage(_(u"Assignments enabled."), type='info')
        elif 'form.button.Disable' in form:
            rule_ids = form.get('rule_ids', ())
            for r in rule_ids:
                assignment = assignable.get(r, None)
                if assignment is not None:
                    assignment.enabled = False
            status.addStatusMessage(_(u"Assignments disabled."), type='info')
        elif 'form.button.Bubble' in form:
            rule_ids = form.get('rule_ids', ())
            for r in rule_ids:
                assignment = assignable.get(r, None)
                if assignment is not None:
                    assignment.bubbles = True
            status.addStatusMessage(_(u"Changes saved."), type='info')
        elif 'form.button.NoBubble' in form:
            rule_ids = form.get('rule_ids', ())
            for r in rule_ids:
                assignment = assignable.get(r, None)
                if assignment is not None:
                    assignment.bubbles = False
            status.addStatusMessage(_(u"Changes saved."), type='info')

        return self.template()