def test_stop_word_in_object(self):
        """Check that the verified marker interface is removed

        It should be removed if the stop words are found on the object's text.
        """
        alsoProvides(self.doc, IStopWordsVerified)
        self.doc.reindexObject()

        catalog = api.portal.get_tool('portal_catalog')
        brain = catalog(id=self.doc.id)[0]

        verify_brain(brain, new_entries='random')

        self.assertFalse(IStopWordsVerified.providedBy(self.doc))
        self.assertTrue(IHasStopWords.providedBy(self.doc))
    def test_object_without_verified_marker_interface(self):
        """Corner case: the object does not have the verified marker interface

        It should add the stop words marker interface without raising any
        exception.
        """
        self.assertFalse(IStopWordsVerified.providedBy(self.doc))
        self.assertFalse(IHasStopWords.providedBy(self.doc))

        catalog = api.portal.get_tool('portal_catalog')
        brain = catalog(id=self.doc.id)[0]

        verify_brain(brain, new_entries='random')

        self.assertFalse(IStopWordsVerified.providedBy(self.doc))
        self.assertTrue(IHasStopWords.providedBy(self.doc))
    def test_no_brain(self):
        """Corner case: verify_brain does not get a brain but something else

        It should return silently.
        """
        self.assertIsNone(
            verify_brain(self.doc, new_entries='random')
        )
    def test_none_entries(self):
        """Corner case: verify_brain gets None as new entries

        It should return silently.
        """
        catalog = api.portal.get_tool('portal_catalog')
        brain = catalog(id=self.doc.id)[0]
        self.assertIsNone(
            verify_brain(brain, new_entries=None)
        )
def review_verified_objects(settings, event):
    """Get the new entries on settings and apply them on verified objects"""
    new_entries = get_new_entries(event.oldValue, event.newValue)

    # if there's nothing new, we are done
    if not new_entries:
        return

    new_entries = '\n'.join(new_entries)

    catalog = api.portal.get_tool('portal_catalog')
    brains = catalog(object_provides=IStopWordsVerified.__identifier__)

    if not ASYNC:
        for brain in brains:
            verify_brain(brain, new_entries)
    else:
        # split the work to review the verified objects for new stop words in
        # batches
        batch = 1
        amount = 300
        count = len(brains)
        while count > 0:
            view_path = '/{0}/@@review-objects'.format(
                api.portal.get().id
            )
            params = {
                'start': amount * batch - amount,
                'size': amount,
                'entries': new_entries,
            }
            logger.warn('Queued request {0} {1}'.format(view_path, params))
            taskqueue.add(
                view_path,
                params=params
            )

            batch += 1
            count -= amount
    def __call__(self):
        start, size, entries = self._check_parameters()

        catalog = api.portal.get_tool("portal_catalog")

        query = {"sort_on": "effective"}

        # get the marker interface, if any, to filter objects
        provides = self.request.get("type", None)
        if provides:
            query["object_provides"] = provides
        else:
            query["object_provides"] = IStopWordsVerified.__identifier__

        # get the workflow states, if any, to filter objects
        states = self.request.get("states", None)
        if states:
            query["review_state"] = states.split(",")

        brains = catalog(query)
        for brain in brains[start : start + size]:
            verify_brain(brain, entries)