from collective.ftw.upgrade import UpgradeStep
from logging import getLogger
from plone import api

logger = getLogger(__name__)


class ReindexTheNewsItems(UpgradeStep):
    """Reindex the News Items."""

    def __call__(self):
        brains = api.content.find(portal_type="News Item")
        # self.log(f"Reindexing {len(brains)} News Items")
        logger.info("Reindexing %r News Items", len(brains))

        for idx, brain in enumerate(brains):
            if idx and idx % 100 == 0:
                logger.info(f"Reindexed {idx} News Items so far...")
            obj = brain.getObject()
            obj.reindexObject()

        logger.info("Reindexing of News Items completed.")
