def create_test_participant(self): return content.create( container=self.portal, type=self.participant_name, id='sample-participant', programName=content.get_uuid(obj=self.program), )
def content_added(obj, event): library = library_utils.get_library(obj) library_uid = None annotations = IAnnotations(obj) if library is not None: library_uid = content_api.get_uuid(library) annotations[constants.LIBRARY_ANNOTATION_KEY] = library_uid
def get_calendar_year_and_uid(self): calendar_year = content.create( container=self.portal, type='OIECalendarYear', id='2020', ) uid = content.get_uuid(obj=calendar_year) return (calendar_year, uid)
def library_modified(obj, event): if IContainerModifiedEvent.providedBy(event): return catalog = portal_api.get_tool('portal_catalog') try: for brain in catalog.unrestrictedSearchResults( parent_libraries=content_api.get_uuid(obj)): o = brain._unrestrictedGetObject() o.reindexObject(idxs=['parent_libraries']) except Exception: logger.exception('An error in library_modified event handler')
def get_permalink(self): """Create a permalink.""" registry = getUtility(IRegistry) types_use_view_action = frozenset( registry.get('plone.types_use_view_action_in_listings', []), ) if self.context.portal_type in types_use_view_action: suffix = '/view' else: suffix = '' return '{0}/resolveuid/{1}{2}'.format( portal.get().absolute_url(), content.get_uuid(self.context), suffix, )
def content_moved(obj, event): new_parent = getattr(event, 'newParent', None) old_parent = getattr(event, 'oldParent', None) if new_parent and old_parent and new_parent != old_parent: library = library_utils.get_library(obj) annotations = IAnnotations(obj) existing_lib_uid = annotations.get(constants.LIBRARY_ANNOTATION_KEY, None) modified = False if library is not None: library_uid = content_api.get_uuid(library) if library_uid != existing_lib_uid: modified = True annotations[constants.LIBRARY_ANNOTATION_KEY] = library_uid if existing_lib_uid and library is None: modified = True annotations[constants.LIBRARY_ANNOTATION_KEY] = None if modified: obj.reindexObject()
def get_content(self, name=None, objects=True, restricted=False, folders_only=False, **kw): library = library_utils.get_library(self) if library is None: return list() library_uid = content_api.get_uuid(library) path_in_library = library_utils.get_path_in_library(self) if self.portal_type == constants.LIBRARY_PORTAL_TYPE: path_in_library = [''] if path_in_library is None: return list() portal_types = portal_api.get_tool('portal_types') all_types = portal_types.listContentTypes() test_types = [ t for t in all_types if t not in (constants.LIBRARY_FOLDER_PORTAL_TYPE, constants.LIBRARY_PORTAL_TYPE) ] base_query = dict(path_in_library={ 'query': '/'.join(path_in_library), 'depth': 1 }) base_query.update(kw) if folders_only: base_query['portal_type'] = constants.LIBRARY_FOLDER_PORTAL_TYPE else: base_query['portal_type'] = \ [t for t in all_types if t != constants.LIBRARY_PORTAL_TYPE] if 'sort_on' not in base_query: base_query['sort_on'] = 'sortable_title' elif base_query['sort_on'] == 'getObjPositionInParent': base_query['sort_on'] = 'sortable_title' if name is not None: base_query['id'] = name if 'path' in base_query: del base_query['path'] local_query = base_query.copy() local_query['library'] = library_uid folders = list() non_folders = list() catalog = portal_api.get_tool('portal_catalog') if restricted: local_results = catalog.searchResults(**local_query) else: local_results = catalog.unrestrictedSearchResults(**local_query) local_results = [b for b in local_results] parent_results = list() if name is None or len(local_results) == 0: query = base_query.copy() query['portal_type'] = test_types folder_query = base_query.copy() folder_query['portal_type'] = constants.LIBRARY_FOLDER_PORTAL_TYPE parent_uids = library_utils.get_parent_libraries(self, uids=True) parent_uids = [u for u in parent_uids if u != library_uid] adapter = queryAdapter(library, interface=ILibraryAdditionalQuery) if adapter is not None: query.update(adapter()) folder_query.update(adapter.folder_query()) query['library'] = parent_uids folder_query['library'] = parent_uids if restricted: tmp_parent_folder_results = catalog.searchResults( **folder_query) else: tmp_parent_folder_results = catalog.unrestrictedSearchResults( **folder_query) for brain in tmp_parent_folder_results: test_query = query.copy() if 'id' in test_query: del test_query['id'] test_query['path_in_library'] = brain.obj_path_in_library test_query['portal_type'] = test_types if restricted: test_results = catalog.searchResults(**test_query) else: test_results = catalog.unrestrictedSearchResults( **test_query) if test_results: parent_results.append(brain) if not folders_only: if restricted: tmp_parent_results = catalog.searchResults(**query) else: tmp_parent_results = catalog.unrestrictedSearchResults( **query) for brain in tmp_parent_results: parent_results.append(brain) if len(parent_results): results = local_results + parent_results else: results = local_results seen = dict() for brain in results: portal_type = brain.portal_type _id = brain.id if _id in seen: continue else: seen[_id] = True if portal_type == constants.LIBRARY_FOLDER_PORTAL_TYPE: folders.append(brain) else: non_folders.append(brain) folders.sort(key=lambda b: b.sortable_title) non_folders.sort(key=lambda b: b.sortable_title) if objects: def _get_object(brain): portal_type = brain.portal_type if restricted: item = brain.getObject() else: item = brain._unrestrictedGetObject() if brain.library != library_uid: if portal_type == constants.LIBRARY_FOLDER_PORTAL_TYPE: item = LibraryFolderProxy( item, id=brain.id, title=brain.Title).__of__(self) else: item = ContentProxy(item).__of__(self) return item folders = [_get_object(f) for f in folders] non_folders = [_get_object(o) for o in non_folders] return folders + non_folders
def fix_internal_link_field(context): """ Parte della migrazione collective.smartlink to version 1100. Serve per togliere il contenuto del campo 'internal_link' e metterlo nel campo standard 'remoteUrl'. Si spazzola tutti i link del sito che hanno un internal_link e li corregge. """ logger.info('Fixing the internal_link field for all the Link objects.') site = api.portal.get() logger.info("Getting all the Link objects in the site...") link_brains = site.portal_catalog.unrestrictedSearchResults( portal_type=["Link"] ) objects_changed = 0 total_changed = 0 brokenlink = [] logger.info("Found {} Link objects".format(len(link_brains))) for brain in link_brains: link_obj = brain.getObject() # Facciamo un controllo che dovrebbe essere inutile ma stiamo dalla # parte dei bottoni. Se un oggetto ha un link interno NON può avere # anche un link esterno. if link_obj.internal_link: logger.info( '---Link: {} | points to: {}\n'.format( link_obj.absolute_url_path(), link_obj.internal_link.to_path ) ) linked_obj = link_obj.internal_link.to_object if linked_obj: uuid = get_uuid(linked_obj) else: uuid = u'notfound' brokenlink.append(link_obj.absolute_url_path()) link_obj.remoteUrl = u'${}/resolveuid/{}'.format( '{portal_url}', uuid ) link_obj.internal_link = None link_obj.reindexObject(idxs=["remoteUrl"]) objects_changed += 1 total_changed += 1 if objects_changed > 10: try: logger.info("Partial Commit...") transaction.commit() logger.info("Partial Commit: OK") except Exception as e: logger.error(u"Error while committing transaction.") logger.error(u"{}".format(e)) objects_changed = 0 logger.info("Changed a total of {} Link objects".format(total_changed)) if brokenlink: logger.info("Broken links found: ") for broken in brokenlink: logger.info(broken) transaction.commit()