Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix icon catalog metadata #858

Open
rodfersou opened this issue Jul 9, 2019 · 4 comments
Open

Fix icon catalog metadata #858

rodfersou opened this issue Jul 9, 2019 · 4 comments

Comments

@rodfersou
Copy link
Member

After static resources reorganization, the getIcon catalog metadata was not updated.

We need to create a new upgrade step to fix this metadata:

def fix_cover_icons(context):
    """ Based on https://bit.ly/2FZcnYY
    """
    zcatalog = getToolByName(context, 'portal_catalog')

    # get the more hidden, inner (real) catalog implementation
    catalog = zcatalog._catalog
    try:
        # Get the getIcon index value and
        # check if there is a getIcon at all, this may not exist in some
        # customizations, who knows, but always exists in default Plone
        metadata_index = catalog.names.index('getIcon')
    except ValueError:
        logger.info('`getIcon` not in metadata, skip upgrade step')
        return

    cnt = 0
    # search whole catalog
    results = zcatalog.unrestrictedSearchResults(
        portal_type='collective.cover.content')
    num_objects = len(results)
    pghandler = ZLogHandler(1000)
    pghandler.init('Updating getIcon metadata', num_objects)
    for brain in results:
        pghandler.report(cnt)
        # First get the new value
        try:
            obj = brain._unrestrictedGetObject()
        except (AttributeError, KeyError, TypeError, NotFound):
            continue

        # We can now update the record with the new getIcon value
        record = list(catalog.data[brain.getRID()])
        new_value = record[metadata_index]
        if '/img/' not in new_value:
            new_value = new_value.split('/')
            new_value.insert(1, 'img')
            new_value = '/'.join(new_value)
        record[metadata_index] = new_value
        catalog.data[brain.getRID()] = tuple(record)

        cnt += 1  # we are curious
    # done
    pghandler.finish()
    logger.info('Reindexed `getIcon` for %s items', str(cnt))
@rodfersou
Copy link
Member Author

@hvelarde it should be a new upgrade step, or we should fix the old upgrade step for static resources reorganisation to also take care of this?

@hvelarde
Copy link
Member

I think a new one is better.

@rodfersou
Copy link
Member Author

okay, also this should work just for Plone < 5.0.1

@rodfersou
Copy link
Member Author

at Plone 5.0.1 this metadata becomes boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants