def filterLatestVersion(self, brains, noOfItems=6): """ Take a list of catalog brains and return only the first noOfItems which are either latest versions or not versioned. """ cat = getToolByName(self.context, 'portal_catalog') res = [] res_urls_set = set() for brain in brains: # if object implements our versioning if 'eea.versions.interfaces.IVersionEnhanced' in brain.object_provides: obj = brain.getObject() versionsObj = IGetVersions(obj) brain_url = brain.getURL() try: is_latest = versionsObj.isLatest() except Exception: logger.warning("Couldn't check if object at %s is latest obj", brain_url) continue if is_latest: # keep it, this is latest object, first checking if the current # brain url is not already added within our results url set if brain_url not in res_urls_set: res_urls_set.add(brain_url) res.append(brain) else: # attempt to retrieve the latest versions of the given brain # if this brains doesn't contain the latest version of the # object latest = versionsObj.latest_version() uid = latest.UID() results = cat.searchResults(UID=uid) if not results: logger.warning("Couldn't find catalog entry for UID %s", uid) else: brain = cat.searchResults(UID=uid)[0] brain_url = brain.getURL() if brain_url not in res_urls_set: res_urls_set.add(brain_url) res.append(brain) else: #this object is not versioned, so keep it brain_url = brain.getURL() if brain_url not in res_urls_set: res_urls_set.add(brain_url) res.append(brain) if len(res) == noOfItems: break #we got enough items # because of performance optimization ticket and #14008 # resort based on effective date since getting the latest version could # mess up the sorting that came from the catalog search res.sort(key=lambda x: x.effective) res.reverse() return res
def find_not_archived_versions(self, p_types=[]): """ Find not archived older versions and return a detailed report """ info('Start searching for not archived older version!') versionable_interface = 'eea.versions.interfaces.IVersionEnhanced' cat = getToolByName(self, 'portal_catalog') result = [] if p_types: brains = cat.unrestrictedSearchResults( object_provides=versionable_interface, portal_type=p_types) else: brains = cat.unrestrictedSearchResults( object_provides=versionable_interface) info('Checking %s brains.', len(brains)) count = 0 count_not_expired = 0 total = len(brains) for brain in brains: count += 1 if (count % 500) == 0: info('PROCESSING: %s/%s', count, total) # expired, skip if brain.ExpirationDate != 'None': continue obj = brain.getObject() adapter = IGetVersions(obj) # latest version, skip if adapter.isLatest(): continue # if latest published versions, skip latest_versions = adapter.later_versions() latest_published = True for later_ver in latest_versions: if later_ver['review_state'] == 'published': latest_published = False break if latest_published: continue info('Old version not expired: %s', brain.getURL()) result.append(brain.getURL()) count_not_expired += 1 info('Found %s not expired old versions', count_not_expired) info('Done searching for not archived older version!') return result