def fix_layout(context): """ Fix layout for old style IVisualizationEnabled objects """ ctool = getToolByName(context, 'portal_catalog') # Fix broken brains iface = interfaceToName(context, IPossibleVisualization) brains = ctool( portal_type=['File', 'DataFile', 'EEAFigureFile'], object_provides=iface, show_inactive=True, Language='all' ) logger.info('Fixing daviz broken brains: %s', len(brains)) for brain in brains: doc = brain.getObject() if not doc: continue support = queryMultiAdapter( (doc, context.REQUEST), name='daviz_support') if support.is_visualization: doc.reindexObject(['object_provides', ]) # Fix daviz layouts iface = interfaceToName(context, IVisualizationEnabled) brains = ctool( object_provides=iface, show_inactive=True, Language='all' ) logger.info('Fixing daviz layouts: %s', len(brains)) for brain in brains: doc = brain.getObject() layout = doc.getLayout() if not layout.startswith('daviz-view.html'): logger.info("Fixing layout for %s", doc.absolute_url()) doc.setLayout('daviz-view.html')
def cleanup_p4a(context): """ eea.facetednavigation > 4.0 doesn't depend on p4a.subtyper anymore, but your instance will crash if it's missing as there are persistent references to p4a.subtyper.interfaces.ISubtyped. After you run this script, you should be able to drop p4a.subtyper from your buildout. """ try: from p4a.subtyper.interfaces import ISubtyper, ISubtyped except ImportError: logger.info('p4a.subtyper not installed. Aborting...') return ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) for brain in brains: doc = brain.getObject() anno = queryAdapter(doc, IAnnotations) subtyper = getUtility(ISubtyper) name = getattr(subtyper.existing_type(doc), 'name', '') if 'faceted' not in name.lower(): continue logger.info( 'Cleanup p4a.subtyper interface and descriptor info for: %s', doc.absolute_url()) noLongerProvides(doc, ISubtyped) anno.pop('p4a.subtyper.DescriptorInfo', None)
def migrate_autocomplete_widget(context): """ As in version 8.4 we added selection of the autocomplete suggestion view. To maintain backward compatibility we will set the value of this 'autocomplete_view' field to solr suggestions view. """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) count = 0 for brain in brains: doc = brain.getObject() settings = ICriteria(doc) for criterion in settings.values(): if criterion.widget == 'autocomplete': criterion.autocomplete_view = u'solr-autocomplete-suggest' logger.info( 'Set defaut autocomplete view of widget: %s', criterion.title ) count += 1 logger.info('Migrated %s autocomplete widgets', count)
def cleanup_fallback_images(context): """ Migrate exhibit image charts""" ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IVisualizationEnabled) brains = ctool(object_provides=iface, show_inactive=True, Language='all') count = 0 nbrains = len(brains) logger.info("Start removing aprox. %s Google Charts images", nbrains * 8) for idx, brain in enumerate(brains): doc = brain.getObject() clean = [] for item in CLEANUP: if item in doc.objectIds(): clean.append(item) if not clean: continue length = len(clean) logger.info('Removing %s Google Charts fallback images from %s', length, doc.absolute_url()) doc.manage_delObjects(clean) count += length if idx % 100 == 0: logger.info("Cleanup: Transaction commit: %s/%s", idx, nbrains) transaction.commit() logger.info('Done removing %s Google Charts fallback images', count)
def objs_with_iface(context, iface): """Return all objects in the system as found by the nearest portal catalog that provides the given interface. The result will be a generator for scalability reasons. >>> from zope import interface >>> class ITest(interface.Interface): pass >>> class Mock(object): ... def __init__(self, id): self.id = id ... def getObject(self): return self ... def __repr__(self): return '<Mock id=%s>' % self.id >>> m = Mock('Portal Root') >>> objs = [Mock('m1'), Mock('m2'), Mock('m3')] >>> interface.directlyProvides(objs[1], ITest) >>> m.portal_catalog = lambda **kwargs: objs >>> [x for x in objs_with_iface(m, ITest)] [<Mock id=m2>] """ catalog = cmfutils.getToolByName(context, 'portal_catalog') for brain in catalog(object_provides=interfaceToName(context, iface)): obj = brain.getObject() if iface in interface.directlyProvidedBy(obj): yield brain.getObject()
def fix_column_labels(context): """ Move column label settings from facet annotations directly to daviz JSON """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IVisualizationEnabled) brains = ctool( object_provides=iface, show_inactive=True, Language='all' ) logger.info('Fixing daviz column labels: %s', len(brains)) for brain in brains: try: doc = brain.getObject() mutator = queryAdapter(doc, IVisualizationConfig) data = mutator.json properties = data.get('properties', {}) facets = mutator.facets logger.info("Fixing column labels for %s", doc.absolute_url()) for facet in facets: name = facet.get('name') label = facet.get('label', name) if isinstance(label, str): label = label.decode('utf-8') config = properties.get(name, {}) config.setdefault('label', label) if isinstance(config['label'], str): config['label'] = config['label'].decode('utf-8') mutator.json = data except Exception: logger.info("Failed to fix")
def fix_default_layout(context): """ In eea.facetednavigation < 4.0 the default layout was folder_summary_view. As in Plone 4 folder_summary_view doesn't wrap the listing in a macro, the default layout for eea.facetednavigation > 4.0 is folder_listing. Still, we need to keep backward compatibility, at least when using with EEA site. Therefore this upgrade step is available only in EEA context, as folder_summary_view was customized in eea.design in order to define the 'content-core' macro. """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) for brain in brains: doc = brain.getObject() anno = queryAdapter(doc, IAnnotations) if anno.get(ANNO_FACETED_LAYOUT, ''): # Not using the default one, skipping continue logger.info( 'Updating faceted layout to folder_summary_view for: %s', doc.absolute_url()) anno[ANNO_FACETED_LAYOUT] = 'folder_summary_view'
def fix_column_labels(context): """ Move column label settings from facet annotations directly to daviz JSON """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IVisualizationEnabled) brains = ctool(object_provides=iface, show_inactive=True, Language='all') logger.info('Fixing daviz column labels: %s', len(brains)) for brain in brains: try: doc = brain.getObject() mutator = queryAdapter(doc, IVisualizationConfig) data = mutator.json properties = data.get('properties', {}) facets = mutator.facets logger.info("Fixing column labels for %s", doc.absolute_url()) for facet in facets: name = facet.get('name') label = facet.get('label', name) if isinstance(label, str): label = label.decode('utf-8') config = properties.get(name, {}) config.setdefault('label', label) if isinstance(config['label'], str): config['label'] = config['label'].decode('utf-8') mutator.json = data except Exception: logger.info("Failed to fix")
def add_sorting_widget(context): """ As in version 7.1 we removed default sorting by effective date, in order to maintain backward compatibility we will add a sorting widget, hidden for all faceted navigable items where this widget is not present """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) count = 0 for brain in brains: try: doc = brain.getObject() settings = ICriteria(doc) sorting = [criterion for criterion in settings.values() if criterion.widget == 'sorting'] if sorting: continue settings.add( 'sorting', 'right', title='Sort on', default='effective(reverse)', hidden=True ) except Exception, err: logger.exception(err) else: logger.info('Added sorting widget for: %s', doc.absolute_url()) count += 1
def fix_column_labels(context): """ Move column label settings from facet annotations directly to daviz JSON """ ctool = getToolByName(context, "portal_catalog") iface = interfaceToName(context, IVisualizationEnabled) brains = ctool(object_provides=iface, show_inactive=True, Language="all") logger.info("Fixing daviz column labels: %s", len(brains)) for brain in brains: doc = brain.getObject() mutator = queryAdapter(doc, IVisualizationConfig) data = mutator.json properties = data.get("properties", {}) facets = mutator.facets logger.info("Fixing column labels for %s", doc.absolute_url()) for facet in facets: name = facet.get("name") label = facet.get("label", name) if isinstance(label, str): label = label.decode("utf-8") config = properties.get(name, {}) config.setdefault("label", label) if isinstance(config["label"], str): config["label"] = config["label"].decode("utf-8") mutator.json = data
def change_faceted_vocabularies(context): """ Use collective.taxonomy taxonomies instead of portal_vocabulary """ catalog = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = catalog.unrestrictedSearchResults(object_provides=iface) vocabularies = { 'themes': 'collective.taxonomy.themes', 'themesmerged': 'collective.taxonomy.themesmerged' } count = 0 for brain in brains: doc = brain.getObject() anno = queryAdapter(doc, IAnnotations) criterias = anno.get(ANNO_CRITERIA, '') for crit in criterias: if crit.vocabulary in vocabularies.keys(): logger.info("Modified %s vocabulary for %s", crit.vocabulary, doc.absolute_url()) crit.vocabulary = vocabularies[crit.vocabulary] count += 1 anno[ANNO_CRITERIA] = PersistentList(criterias) transaction.commit() logger.info("Modified %s vocabularies.", count)
def cleanup_fallback_images(context): """ Migrate exhibit image charts""" ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IVisualizationEnabled) brains = ctool( object_provides=iface, show_inactive=True, Language='all' ) count = 0 nbrains = len(brains) logger.info("Start removing aprox. %s Google Charts images", nbrains*8) for idx, brain in enumerate(brains): doc = brain.getObject() clean = [] for item in CLEANUP: if item in doc.objectIds(): clean.append(item) if not clean: continue length = len(clean) logger.info('Removing %s Google Charts fallback images from %s', length, doc.absolute_url()) doc.manage_delObjects(clean) count += length if idx % 100 == 0: logger.info("Cleanup: Transaction commit: %s/%s", idx, nbrains) transaction.commit() logger.info('Done removing %s Google Charts fallback images', count)
def __init__(self, context): # Remove the security proxy so the values from the vocabulary # are the actual interfaces and not proxies. component = removeSecurityProxy(context) interfaces = providedBy(component).flattened() terms = [SimpleTerm(interface, interfaceToName(context, interface)) for interface in interfaces] super(ObjectInterfacesVocabulary, self).__init__(terms)
def getEntries(self): """See ITopic. """ weblog = self.getWeblogContentObject() weblog_config = IWeblogEnhancedConfiguration(weblog) path = '/'.join(weblog.getPhysicalPath()) catalog = getToolByName(self, 'portal_catalog') ifaces = [interfaceToName(catalog.aq_parent, IWeblogEntry), interfaceToName(catalog.aq_parent, IPossibleWeblogEntry)] results = catalog( object_provides={'query' : ifaces, 'operator' : 'or'}, path={'query':path, 'level': 0}, Creator={'query' : self.keywords, 'operator' : 'or'}, sort_on='effective', sort_order='reverse', review_state=weblog_config.published_states) return BloggifiedCatalogResults(results)
def __init__(self, context): # Remove the security proxy so the values from the vocabulary # are the actual interfaces and not proxies. component = removeSecurityProxy(context) interfaces = providedBy(component).flattened() terms = [ SimpleTerm(interface, interfaceToName(context, interface)) for interface in interfaces ] super(ObjectInterfacesVocabulary, self).__init__(terms)
def getBloggerPosts(self, blogger): catalog = getMultiAdapter((self.context, self.request), name=u'plone_tools').catalog() pstate = getMultiAdapter((self.context, self.request), name=u'plone_portal_state') portal = pstate.portal() brains = catalog(path='/%s/%s' % (portal.getId(), self.data.blog_path), object_provides=interfaceToName(portal, IPossibleWeblogEntry), review_state='published', Creator=blogger, ) return brains
def getAllEntries(self, maximum=None, offset=0): """See IWeblog. """ catalog, portal = self._setCatalog() results = catalog( object_provides=interfaceToName(portal, IPossibleWeblogEntry), path={ 'query' : '/'.join(self.context.getPhysicalPath()), 'level' : 0, }, sort_on = 'effective', sort_order = 'reverse') return BloggifiedCatalogResults(self._filter(results, maximum, offset))
def fix_layout(context): """ Fix layout for old style IFacetedNavigable objects """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool(object_provides=iface, show_inactive=True, Language='all') for brain in brains: doc = brain.getObject() layout = doc.getLayout() if not layout.startswith('faceted'): logger.info("Fixing layout for %s", doc.absolute_url()) doc.setLayout('facetednavigation_view')
def getEntries(self, maximum=None, offset=0): """See IWeblog. """ catalog, portal = self._setCatalog() weblog_config = IWeblogEnhancedConfiguration(self.context) results = catalog( object_provides=interfaceToName(portal, IPossibleWeblogEntry), path={ 'query' : '/'.join(self.context.getPhysicalPath()), 'level' : 0, }, sort_on='effective', sort_order='reverse', review_state={ 'query' : weblog_config.published_states, 'operator' : 'or'}) return BloggifiedCatalogResults(self._filter(results, maximum, offset))
def getEntries(self, maximum=None, offset=0): """ """ if self._results is None: min_datetime, max_datetime = self._getDateRange() catalog = getToolByName(self, 'portal_catalog') weblog = self.getWeblogContentObject() weblog_config = IWeblogEnhancedConfiguration(weblog) path = '/'.join(weblog.getPhysicalPath()) ifaces = [interfaceToName(catalog.aq_parent, IWeblogEntry), interfaceToName(catalog.aq_parent, IPossibleWeblogEntry)] results = catalog( object_provides={'query' : ifaces, 'operator' : 'or'}, path={'query':path, 'level': 0}, review_state=weblog_config.published_states, effective={ 'query' : [min_datetime, max_datetime], 'range': 'minmax'} ) results = results[offset:] if maximum is not None: results = results[:maximum] self._results = BloggifiedCatalogResults(results) return self._results
def getAuthors(self): """See IWeblog. """ catalog, portal = self._setCatalog() weblog_config = IWeblogEnhancedConfiguration(self.context) results = catalog( object_provides=interfaceToName(portal, IPossibleWeblogEntry), path={ 'query' : '/'.join(self.context.getPhysicalPath()), 'level' : 0, }, review_state={ 'query' : weblog_config.published_states, 'operator' : 'or'}) authors = {} for brain in results: authors[brain.Creator] = None return [AuthorTopic(author).__of__(self.context) for author in authors.keys()]
def fix_criteria(context): """ In version 8.8, the autocomplete widget uses select2 instead of jquery.autocomplete. """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) logger.info('Fixing %s faceted navigable criteria', len(brains)) for brain in brains: doc = brain.getObject() criteria = queryAdapter(doc, ICriteria) for cid in criteria.keys(): logger.info('Fixing faceted criteria for %s', brain.getURL()) criteria.upgrade(cid) logger.info('Done fixing faceted navigable criteria')
def enable_davizSettings(context): """ Move column label settings from facet annotations directly to daviz JSON """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IVisualizationEnabled) brains = ctool(object_provides=iface, show_inactive=True, Language='all') logger.info('Enabling daviz.properties: %s', len(brains)) for brain in brains: try: doc = brain.getObject() logger.info("Enabling daviz.properties for %s", doc.absolute_url()) mutator = queryAdapter(doc, IVisualizationConfig) if not mutator.view('daviz.properties'): mutator.add_view('daviz.properties') except Exception, err: logger.warn("Failed to enable daviz.properties: %s", err)
def hide_portlets(context): """ As eea.design changed, we need to hide left and right plone portlets columns. This upgrade step is available only in EEA context """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) logger.info('Hiding plone portlets for %s faceted navigable objects', len(brains)) for brain in brains: doc = brain.getObject() if not IHidePloneLeftColumn.providedBy(doc): logger.info('Hidding left portlet for %s', doc.absolute_url()) alsoProvides(doc, IHidePloneLeftColumn) if not IHidePloneRightColumn.providedBy(doc): logger.info('Hidding right portlet for %s', doc.absolute_url()) alsoProvides(doc, IHidePloneRightColumn) logger.info('Hiding plone portlets ... DONE')
def hide_portlets(context): """ As eea.design changed, we need to hide left and right plone portlets columns. This upgrade step is available only in EEA context """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) logger.info( 'Hiding plone portlets for %s faceted navigable objects', len(brains)) for brain in brains: doc = brain.getObject() if not IHidePloneLeftColumn.providedBy(doc): logger.info('Hidding left portlet for %s', doc.absolute_url()) alsoProvides(doc, IHidePloneLeftColumn) if not IHidePloneRightColumn.providedBy(doc): logger.info('Hidding right portlet for %s', doc.absolute_url()) alsoProvides(doc, IHidePloneRightColumn) logger.info('Hiding plone portlets ... DONE')
def enable_davizSettings(context): """ Move column label settings from facet annotations directly to daviz JSON """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IVisualizationEnabled) brains = ctool( object_provides=iface, show_inactive=True, Language='all' ) logger.info('Enabling daviz.properties: %s', len(brains)) for brain in brains: try: doc = brain.getObject() logger.info("Enabling daviz.properties for %s", doc.absolute_url()) mutator = queryAdapter(doc, IVisualizationConfig) if not mutator.view('daviz.properties'): mutator.add_view('daviz.properties') except Exception, err: logger.warn("Failed to enable daviz.properties: %s", err)
def set_geotags_interface(context): """ Set IGeoTagged interface to objects that have geotags set """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IGeoTaggable) brains = ctool(object_provides=iface, show_inactive=True, Language='all') count = 0 for brain in brains: if not brain.location: continue doc = brain.getObject() tags = IGeoTags(doc).tags if tags: alsoProvides(doc, IGeoTagged) doc.reindexObject(idxs=['object_provides', 'geotags']) count += 1 if count % 10 == 0: logg.info("Committing geotags interface migration transaction") transaction.commit() logg.info("DONE adding IGeoTagged to objects with geotags")
def getEntries(self, maximum=None, offset=0, path=None): """See IWeblog. """ catalog, portal = self._setCatalog() catalog._catalog.useBrains(WeblogEntryCatalogBrain) weblog = IWeblogLocator(self.context).find() if getattr(weblog, 'context', None): # `weblog' is presumably an adapter around the real content object. weblog = weblog.context weblog_config = IWeblogEnhancedConfiguration(weblog) path = path or '/'.join(self.context.getPhysicalPath()) results = catalog( object_provides=interfaceToName(portal, IPossibleWeblogEntry), path={ 'query' : path, 'level' : 0, }, sort_on='effective', sort_order='reverse', review_state={ 'query' : weblog_config.published_states, 'operator' : 'or'}) return self._filter(results, maximum, offset)
def cleanup_fallback_images(context): """ Migrate exhibit image charts""" ctool = getToolByName(context, "portal_catalog") iface = interfaceToName(context, IVisualizationEnabled) brains = ctool(object_provides=iface, show_inactive=True, Language="all") count = 0 for brain in brains: doc = brain.getObject() clean = [] for item in CLEANUP: if item in doc.objectIds(): clean.append(item) if not clean: continue length = len(clean) logger.info("Removing %s exhibit fallback images from %s", length, doc.absolute_url()) doc.manage_delObjects(clean) count += length logger.info("Removed %s exhibit fallback images", count)
def migrate_autocomplete_widget(context): """ As in version 8.4 we added selection of the autocomplete suggestion view. To maintain backward compatibility we will set the value of this 'autocomplete_view' field to solr suggestions view. """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) count = 0 for brain in brains: doc = brain.getObject() settings = ICriteria(doc) for criterion in settings.values(): if criterion.widget == 'autocomplete': criterion.autocomplete_view = u'solr-autocomplete-suggest' logger.info('Set defaut autocomplete view of widget: %s', criterion.title) count += 1 logger.info('Migrated %s autocomplete widgets', count)
def cleanup_fallback_images(context): """ Migrate exhibit image charts""" ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IVisualizationEnabled) brains = ctool(object_provides=iface, show_inactive=True, Language='all') count = 0 for brain in brains: doc = brain.getObject() clean = [] for item in CLEANUP: if item in doc.objectIds(): clean.append(item) if not clean: continue length = len(clean) logger.info('Removing %s exhibit fallback images from %s', length, doc.absolute_url()) doc.manage_delObjects(clean) count += length logger.info('Removed %s exhibit fallback images', count)
def fix_default_layout(context): """ In eea.facetednavigation < 4.0 the default layout was folder_summary_view. As in Plone 4 folder_summary_view doesn't wrap the listing in a macro, the default layout for eea.facetednavigation > 4.0 is folder_listing. Still, we need to keep backward compatibility, at least when using with EEA site. Therefore this upgrade step is available only in EEA context, as folder_summary_view was customized in eea.design in order to define the 'content-core' macro. """ ctool = getToolByName(context, 'portal_catalog') iface = interfaceToName(context, IFacetedNavigable) brains = ctool.unrestrictedSearchResults(object_provides=iface) for brain in brains: doc = brain.getObject() anno = queryAdapter(doc, IAnnotations) if anno.get(ANNO_FACETED_LAYOUT, ''): # Not using the default one, skipping continue logger.info('Updating faceted layout to folder_summary_view for: %s', doc.absolute_url()) anno[ANNO_FACETED_LAYOUT] = 'folder_summary_view'
def _callFUT(self, *args, **kw): from zope.component.interface import interfaceToName return interfaceToName(*args, **kw)
def _getInterfaceNames(self, interfaces): return [interfaceToName(self, iface) for iface in interfaces]
def object_implements(object, portal, **kw): return [interfaceToName(portal, i) for i in providedBy(object).flattened()]