Skip to content

Instantly share code, notes, and snippets.

@rafaelbco
Last active April 6, 2016 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelbco/fed0616744d01ceb40e18e1ba4339d73 to your computer and use it in GitHub Desktop.
Save rafaelbco/fed0616744d01ceb40e18e1ba4339d73 to your computer and use it in GitHub Desktop.
Script to populate uid_catalog and reference_catalog in order to make link integrity work
# coding=utf8
u"""Script to populate uid_catalog and reference_catalog in order to make link integrity work.
Only content items that implements the IReferenceable behavior (plone.app.referenceablebehavior) are
added to the catalogs.
ATTENTION: Be sure to adjust the global variables after the imports according to your environment.
"""
from Acquisition import aq_base
from Products.Archetypes import config
from plone import api
from plone.app.referenceablebehavior.referenceable import IReferenceable
from zope.component.hooks import setSite
DRY_RUN = True
PLONE_SITE_ID = 'Plone'
ADMIN_USER_ID = 'admin'
def main(app):
portal = app[PLONE_SITE_ID]
setSite(portal)
with api.env.adopt_user(username=ADMIN_USER_ID):
portal_catalog = api.portal.get_tool('portal_catalog')
brains = portal_catalog(object_provides=IReferenceable.__identifier__)
brains = sorted(brains, key=lambda b: b.getPath())
for b in brains:
obj = b.getObject()
add_to_uid_catalog(obj)
for child_obj in get_references(obj):
add_to_uid_catalog(child_obj)
add_to_reference_catalog(child_obj)
if not DRY_RUN:
# Commit transaction
import transaction; transaction.commit()
# Perform ZEO client synchronization (if running in clustered mode)
app._p_jar.sync()
def add_to_catalog(obj, catalog):
path = get_path(obj)
print 'Adding to {}: {} ...'.format(catalog.getId(), path)
catalog.catalog_object(obj, path)
def add_to_uid_catalog(obj):
add_to_catalog(obj=obj, catalog=api.portal.get_tool(config.UID_CATALOG))
def add_to_reference_catalog(obj):
add_to_catalog(obj=obj, catalog=api.portal.get_tool(config.REFERENCE_CATALOG))
def get_references(obj):
if not hasattr(aq_base(obj), config.REFERENCE_ANNOTATION):
return ()
at_references = getattr(obj, config.REFERENCE_ANNOTATION)
return () if (not at_references) else at_references.values()
def get_path(obj):
physical_path = obj.getPhysicalPath()
if config.REFERENCE_ANNOTATION in physical_path:
physical_path = physical_path[len(api.portal.get().getPhysicalPath()):]
path = '/'.join(physical_path)
assert ('/' in path), 'Invalid path.' # To avoid regressions.
return path
main(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment