Skip to content

Instantly share code, notes, and snippets.

@david-batranu
Created September 23, 2018 14:02
Show Gist options
  • Save david-batranu/b3bac9e9a267f23435172e6aed32ca4f to your computer and use it in GitHub Desktop.
Save david-batranu/b3bac9e9a267f23435172e6aed32ca4f to your computer and use it in GitHub Desktop.
import gc
import transaction
from zope.globalrequest import getRequest
from Products.CMFCore.utils import getToolByName
import plone.api as api
PROFILE_ID = 'profile-esdrt.content:default'
WORKFLOWS = (
'esd-answer-workflow',
'esd-comment-workflow',
'esd-conclusion-phase2-workflow',
'esd-conclusion-workflow',
'esd-file-workflow',
'esd-question-review-workflow',
'esd-review-workflow',
'esd-reviewtool-folder-workflow',
)
PERMISSIONS = (
'Access contents information',
'View',
)
TYPES = (
'ConclusionsPhase2',
'Conclusion',
'ESDRTFile',
'CommentAnswer',
'Comment',
'Question',
'Observation',
'ReviewFolder',
)
def upgrade(context, logger=None):
if logger is None:
from logging import getLogger
logger = getLogger('esdrt.content.upgrades.55_56')
install_workflow(context, logger)
logger.info('Upgrade steps executed')
def install_workflow(context, logger):
setup = getToolByName(context, 'portal_setup')
wtool = getToolByName(context, 'portal_workflow')
catalog = getToolByName(context, 'portal_catalog')
wtool.manage_delObjects(list(WORKFLOWS))
setup.runImportStepFromProfile(PROFILE_ID, 'rolemap')
setup.runImportStepFromProfile(PROFILE_ID, 'workflow')
logger.info('Reinstalled Workflows.')
for ptype in TYPES:
logger.info('Updating %s!', ptype)
for idx, brain in enumerate(catalog(portal_type=ptype), start=1):
url = brain.getURL()
try:
content = brain.getObject()
except KeyError:
logger.warn('Removing stale brain: %s', url)
catalog.uncatalog_object(brain.getPath())
continue
for permission in PERMISSIONS:
current_roles = [
r['name'] for r in
content.rolesOfPermission(permission)
if r['selected']
]
new_roles = list(set(current_roles + ['Auditor']))
content.manage_permission(permission, roles=new_roles, acquire=0)
try:
content.reindexObjectSecurity()
except KeyError:
logger.warn('Cannot reindex. Calling catalog_object for %s!', url)
catalog.catalog_object(content)
logger.info('Updated %s %s/%s.', url, idx, brains_len)
if idx % 100 == 0:
logger.info('transaction.commit after %s!', idx)
transaction.commit()
gc.collect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment