def handleRepair(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return

        query = Indexed('chimpfeeds')
        brains = self.context.portal_catalog.evalAdvancedQuery(query)

        context = self.getContent()
        vocabulary = feeds_factory(context)
        all_feeds = set(term.value for term in vocabulary)

        count = 0
        bad = set()

        changed = []
        for i, brain in enumerate(brains):
            try:
                feeds = set(brain.chimpfeeds)
            except TypeError:
                continue

            missing = feeds - all_feeds
            bad |= missing

            if missing:
                count += 1
                obj = brain.getObject()
                try:
                    field = obj.getField('feeds')
                except AttributeError:
                    feeds = obj.feeds
                    field = None
                else:
                    feeds = set(field.get(obj))

                fixed = feeds - missing

                if field is None:
                    obj.feeds = fixed
                else:
                    field.set(obj, fixed)

                changed.append(obj)

        for obj in changed:
            modified(obj)
            obj.reindexObject()

        logger.info("Repair complete; %d items updated." % count)
        if bad:
            logger.info("Feeds removed: %s." % (", ".join(bad).encode('utf-8')))

        IStatusMessage(self.request).addStatusMessage(
            _(u"Repaired ${count} items.", mapping={'count': count}),
            "info"
        )
    def _getFile(self):
        context = getattr(self.context, 'aq_explicit', self.context)
        field = context.getField(self.fieldname)

        if field is None:
            raise pNotFound(self, self.fieldname, self.request)
        if not field.checkPermission('r', context):
            raise Unauthorized()
        return field.get(context)
Beispiel #3
0
    def handleRepair(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return

        query = Indexed('chimpfeeds')
        brains = self.context.portal_catalog.evalAdvancedQuery(query)

        context = self.getContent()
        vocabulary = feeds_factory(context)
        all_feeds = set(term.value for term in vocabulary)

        count = 0
        bad = set()

        changed = []
        for i, brain in enumerate(brains):
            try:
                feeds = set(brain.chimpfeeds)
            except TypeError:
                continue

            missing = feeds - all_feeds
            bad |= missing

            if missing:
                count += 1
                obj = brain.getObject()
                try:
                    field = obj.getField('feeds')
                except AttributeError:
                    feeds = obj.feeds
                    field = None
                else:
                    feeds = set(field.get(obj))

                fixed = feeds - missing

                if field is None:
                    obj.feeds = fixed
                else:
                    field.set(obj, fixed)

                changed.append(obj)

        for obj in changed:
            modified(obj)
            obj.reindexObject()

        logger.info("Repair complete; %d items updated." % count)
        if bad:
            logger.info("Feeds removed: %s." %
                        (", ".join(bad).encode('utf-8')))

        IStatusMessage(self.request).addStatusMessage(
            _(u"Repaired ${count} items.", mapping={'count': count}), "info")