def get_contact(self, principal, check_permissions=False): """Returns the contact object of this principal. """ if not self.is_contact(principal): raise ValueError('Principal %s is not a contact' % str(principal)) catalog = getToolByName(getSite(), 'portal_catalog') query = { 'portal_type': 'opengever.contact.contact', 'contactid': principal } if not check_permissions: # usually foreign users may not have access to the contacts, # but we want to be able to print the name etc. in this case too. # So we need to use ZCatalog for ignoring the allowedRolesAndUsers # index. contacts = ZCatalog.searchResults(catalog, **query) else: contacts = catalog.searchResults(**query) if len(contacts) == 0: return None elif len(contacts) > 1: raise ValueError('Found %i contacts with principal %s' % (len(contacts), principal)) else: return contacts[0]
def searchResults(self, query=None, **kw): # Calls ZCatalog.searchResults with extra arguments that # limit the results to what the user is allowed to see. # # This version uses the 'effectiveRange' DateRangeIndex. # # It also accepts a keyword argument show_inactive to disable # effectiveRange checking entirely even for those without portal # wide AccessInactivePortalContent permission. # Make sure any pending index tasks have been processed processQueue() kw = kw.copy() show_inactive = kw.get('show_inactive', False) if isinstance(query, dict) and not show_inactive: show_inactive = 'show_inactive' in query user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if not show_inactive and not self.allow_inactive(kw): kw['effectiveRange'] = DateTime() sort_on = kw.get('sort_on') if sort_on and sort_on not in self.indexes(): # I get crazy sort_ons like '194' or 'null'. kw.pop('sort_on') return ZCatalog.searchResults(self, query, **kw)
def get_contact(self, principal, check_permissions=False): """Returns the contact object of this principal. """ if not self.is_contact(principal): raise ValueError('Principal %s is not a contact' % str(principal)) catalog = getToolByName(getSite(), 'portal_catalog') query = {'portal_type': 'opengever.contact.contact', 'contactid': principal} if not check_permissions: # usually foreign users may not have access to the contacts, # but we want to be able to print the name etc. in this case too. # So we need to use ZCatalog for ignoring the allowedRolesAndUsers # index. contacts = ZCatalog.searchResults(catalog, **query) else: contacts = catalog.searchResults(**query) if len(contacts) == 0: return None elif len(contacts) > 1: raise ValueError('Found %i contacts with principal %s' % ( len(contacts), principal)) else: return contacts[0]
def searchResults(self, query=None, **kw): # Calls ZCatalog.searchResults with extra arguments that # limit the results to what the user is allowed to see. # # This version uses the 'effectiveRange' DateRangeIndex. # # It also accepts a keyword argument show_inactive to disable # effectiveRange checking entirely even for those without portal # wide AccessInactivePortalContent permission. # Make sure any pending index tasks have been processed processQueue() kw = kw.copy() show_inactive = kw.get('show_inactive', False) if isinstance(query, dict) and not show_inactive: show_inactive = 'show_inactive' in query user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if not show_inactive and not self.allow_inactive(kw): kw['effectiveRange'] = DateTime() sort_on = kw.get('sort_on') if sort_on and sort_on not in self.indexes(): # I get crazy sort_ons like '194' or 'null'. kw.pop('sort_on') return ZCatalog.searchResults(self, query, **kw)
def _data(self): query = { 'object_provides': ISubscribableSection.__identifier__, 'sort_on' : 'sortable_title', } # Use ZCatalog so ALL results are found, this is even more # unrestricted than portal_catalog.unrestrictedSearchResults return ZCatalog.searchResults(self.catalog, None, **query)
def searchResults(self, REQUEST=None, **kw): """ Calls ZCatalog.searchResults with extra arguments that limit the results to what the user is allowed to see. """ user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if not _checkPermission(AccessInactivePortalContent, self): base = aq_base(self) now = DateTime() self._convertQuery(kw) # Intersect query restrictions with those implicit to the tool for k in 'effective', 'expires': if kw.has_key(k): range = kw[k]['range'] or '' query = kw[k]['query'] if (not isinstance(query, TupleType) and not isinstance(query, ListType)): query = (query, ) else: range = '' query = None if range.find('min') > -1: lo = min(query) else: lo = None if range.find('max') > -1: hi = max(query) else: hi = None if k == 'effective': if hi is None or hi > now: hi = now if lo is not None and hi < lo: return () else: # 'expires': if lo is None or lo < now: lo = now if hi is not None and hi < lo: return () # Rebuild a query if lo is None: query = hi range = 'max' elif hi is None: query = lo range = 'min' else: query = (lo, hi) range = 'min:max' kw[k] = {'query': query, 'range': range} return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResults(self, REQUEST=None, **kw): """ Calls ZCatalog.searchResults with extra arguments that limit the results to what the user is allowed to see. """ user = _getAuthenticatedUser(self) kw[ 'allowedRolesAndUsers' ] = self._listAllowedRolesAndUsers( user ) if not _checkPermission( AccessInactivePortalContent, self ): base = aq_base( self ) now = DateTime() self._convertQuery(kw) # Intersect query restrictions with those implicit to the tool for k in 'effective', 'expires': if kw.has_key(k): range = kw[k]['range'] or '' query = kw[k]['query'] if (not isinstance(query, TupleType) and not isinstance(query, ListType)): query = (query,) else: range = '' query = None if range.find('min') > -1: lo = min(query) else: lo = None if range.find('max') > -1: hi = max(query) else: hi = None if k == 'effective': if hi is None or hi > now: hi = now if lo is not None and hi < lo: return () else: # 'expires': if lo is None or lo < now: lo = now if hi is not None and hi < lo: return () # Rebuild a query if lo is None: query = hi range = 'max' elif hi is None: query = lo range = 'min' else: query = (lo, hi) range = 'min:max' kw[k] = {'query': query, 'range': range} return ZCatalog.searchResults(self, REQUEST, **kw)
def __call__(self, request, **keywords): """ decide on a search backend and perform the given query """ if isActive(): try: return solrSearchResults(request, **keywords) except FallBackException: pass if getattr(aq_base(self.context), '_cs_old_searchResults', None): return self.context._cs_old_searchResults(request, **keywords) return ZCatalog.searchResults(self.context, request, **keywords)
def __call__(self, request, **keywords): """ decide on a search backend and perform the given query """ if isActive(): try: return solrSearchResults(request, **keywords) except FallBackException: pass if getattr(aq_base(self.context), '_cs_old_searchResults', None): return self.context._cs_old_searchResults(request, **keywords) return ZCatalog.searchResults(self.context, request, **keywords)
def list_contacts(self): """Returns a catalog result set of contact brains. """ catalog = getToolByName(getSite(), 'portal_catalog') query = {'portal_type': 'opengever.contact.contact'} # make catalog query without checking security (allowedRolesAndUsers) # since the contacts are not visible for foreign users but should be # in the vocabulary anyway... brains = ZCatalog.searchResults(catalog, **query) return brains
def list_contacts(self): """Returns a catalog result set of contact brains. """ catalog = getToolByName(getSite(), 'portal_catalog') query = {'portal_type': 'opengever.contact.contact'} # make catalog query without checking security (allowedRolesAndUsers) # since the contacts are not visible for foreign users but should be # in the vocabulary anyway... brains = ZCatalog.searchResults(catalog, **query) return brains
def searchResults(self, REQUEST=None, **kw): """Calls ZCatalog.searchResults with extra arguments that limit the results to what the user is allowed to see. This version uses the 'effectiveRange' DateRangeIndex. It also accepts a keyword argument show_inactive to disable effectiveRange checking entirely even for those without portal wide AccessInactivePortalContent permission. """ kw = kw.copy() return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResults(self, REQUEST=None, **kw): """ Calls ZCatalog.searchResults with extra arguments that limit the results to what the user is allowed to see. """ user = getSecurityManager().getUser() # we replace, rather than append the 'allowedRolesAndUsers' index, # anything else would be a really big security loophole kw[ 'allowedRolesAndUsers' ] = self._listAllowedRolesAndUsers( user ) return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResults(self, REQUEST=None, **kw): """Calls ZCatalog.searchResults with extra arguments that limit the results to what the user is allowed to see. This version uses the 'effectiveRange' DateRangeIndex. It also accepts a keyword argument show_inactive to disable effectiveRange checking entirely even for those without portal wide AccessInactivePortalContent permission. """ kw = kw.copy() return ZCatalog.searchResults(self, REQUEST, **kw)
def data(self): """Return a dictionary of requests and subscriptions. mydict = { 'requests': [ { 'title': '', 'url': '', 'description': '', }, ], 'subscriptions': [ # the same... ], } """ user_id = self.mtool.getAuthenticatedMember().id mydict = { 'requests': [], 'subscriptions': [], } query = { 'object_provides': ISubscribableSection.__identifier__, 'sort_on': 'sortable_title', 'full_objects': True, } # Use ZCatalog so ALL results are found, this is even more # unrestricted than portal_catalog.unrestrictedSearchResults brains = ZCatalog.searchResults(self.catalog, None, **query) for brain in brains: title = brain.Title url = brain.getURL() description = brain.Description relative_path = brain.getPath().split('/')[2:] obj = self.site.unrestrictedTraverse(relative_path) sm = SubscriptionsManager(obj) if sm.checkRequestForUser(user_id): mydict['requests'].append({ 'title': title, 'url': url, 'description': description, }) if sm.checkSubscriptionForUser(user_id): mydict['subscriptions'].append({ 'title': title, 'url': url, 'description': description, }) return mydict
def unrestrictedSearchResults(self, REQUEST=None, **kw): """Calls ZCatalog.searchResults directly without restrictions. This method returns every also not yet effective and already expired objects regardless of the roles the caller has. CAUTION: Care must be taken not to open security holes by exposing the results of this method to non authorized callers! If you're in doubt if you should use this method or 'searchResults' use the latter. """ return ZCatalog.searchResults(self, REQUEST, **kw)
def unrestrictedSearchResults(self, REQUEST=None, **kw): """Calls ZCatalog.searchResults directly without restrictions. This method returns every also not yet effective and already expired objects regardless of the roles the caller has. CAUTION: Care must be taken not to open security holes by exposing the results of this method to non authorized callers! If you're in doubt if you should use this method or 'searchResults' use the latter. """ return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResults(self, REQUEST=None, **kw): """ Calls ZCatalog.searchResults with extra arguments that limit the results to what the user is allowed to see. """ user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if not _checkPermission(AccessInactivePortalContent, self): now = DateTime() kw['effective'] = {'query': now, 'range': 'max'} kw['expires'] = {'query': now, 'range': 'min'} return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResults(self, REQUEST=None, **kw): """ Calls ZCatalog.searchResults with extra arguments that limit the results to what the user is allowed to see. """ user = _getAuthenticatedUser(self) kw[ 'allowedRolesAndUsers' ] = self._listAllowedRolesAndUsers( user ) if not _checkPermission( AccessInactivePortalContent, self ): now = DateTime() kw['effective'] = {'query': now, 'range': 'max'} kw['expires'] = {'query': now, 'range': 'min'} return ZCatalog.searchResults(self, REQUEST, **kw)
def data(self): """Return a dictionary of requests and subscriptions. mydict = { 'requests': [ { 'title': '', 'url': '', 'description': '', }, ], 'subscriptions': [ # the same... ], } """ user_id = self.mtool.getAuthenticatedMember().id mydict = { 'requests': [], 'subscriptions': [], } query = { 'object_provides': ISubscribableSection.__identifier__, 'sort_on' : 'sortable_title', 'full_objects': True, } # Use ZCatalog so ALL results are found, this is even more # unrestricted than portal_catalog.unrestrictedSearchResults brains = ZCatalog.searchResults(self.catalog, None, **query) for brain in brains: title = brain.Title url = brain.getURL() description = brain.Description relative_path = brain.getPath().split('/')[2:] obj = self.site.unrestrictedTraverse(relative_path) sm = SubscriptionsManager(obj) if sm.checkRequestForUser(user_id): mydict['requests'].append({ 'title': title, 'url': url, 'description': description, }) if sm.checkSubscriptionForUser(user_id): mydict['subscriptions'].append({ 'title': title, 'url': url, 'description': description, }) return mydict
def all_contact_brains(self, ignore_security=True): """Returns a catalog result set of contact brains. """ catalog = api.portal.get_tool('portal_catalog') query = {'portal_type': CONTACT_TYPE} if ignore_security: # make catalog query without checking security (allowedRolesAndUsers) # since the contacts are not visible for foreign users but should be # in the vocabulary anyway... brains = ZCatalog.searchResults(catalog, **query) else: brains = catalog(query) return brains
def query(self): query = {} if 'start' in self.request: query['start'] = { 'query': datetime.strptime(self.request['start'], '%Y-%m-%d %H:%M'), 'range': 'min', } if 'end' in self.request: query['end'] = { 'query': datetime.strptime(self.request['end'], '%Y-%m-%d %H:%M'), 'range': 'max', } result = ZCatalog.searchResults(self.portal_catalog, **query) return str(len(result))
def searchResults(self, REQUEST=None, **kw): """Calls ZCatalog.searchResults with extra arguments that limit the results to what the user is allowed to see. This version uses the 'effectiveRange' DateRangeIndex. It also accepts a keyword argument show_inactive to disable effectiveRange checking entirely even for those without portal wide AccessInactivePortalContent permission. """ kw = kw.copy() show_inactive = kw.get('show_inactive', False) user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if not show_inactive and not _checkPermission(AccessInactivePortalContent, self): kw['effectiveRange'] = DateTime() return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResultsTrashed(self, REQUEST=None, **kw): kw = kw.copy() show_inactive = kw.get('show_inactive', False) user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if (not show_inactive and not _checkPermission(AccessInactivePortalContent, self)): kw['effectiveRange'] = DateTime() request = getattr(self, 'REQUEST', None) if request is None: session = None else: session = getattr(self.REQUEST, 'SESSION', None) if 'trashed' not in kw: kw['trashed'] = session and session.get('trashcan', False) or False return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResultsTrashed(self, REQUEST=None, **kw): kw = kw.copy() show_inactive = kw.get('show_inactive', False) user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if (not show_inactive and not _checkPermission(AccessInactivePortalContent, self)): kw['effectiveRange'] = DateTime() request = getattr(self, 'REQUEST', None) if request is None: session = None else: session = getattr(self.REQUEST, 'SESSION', None) if 'trashed' not in kw: kw['trashed'] = session and session.get('trashcan', False) or False return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResults(self, query=None, **kw): # Calls ZCatalog.searchResults with extra arguments that # limit the results to what the user is allowed to see. # # This version uses the 'effectiveRange' DateRangeIndex. # # It also accepts a keyword argument show_inactive to disable # effectiveRange checking entirely even for those without portal # wide AccessInactivePortalContent permission. # Make sure any pending index tasks have been processed processQueue() kw = kw.copy() show_inactive = kw.get('show_inactive', False) if isinstance(query, dict) and not show_inactive: show_inactive = 'show_inactive' in query user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if not show_inactive and not self.allow_inactive(kw): kw['effectiveRange'] = DateTime() # filter out invalid sort_on indexes sort_on = kw.get('sort_on') or [] if isinstance(sort_on, six.string_types): sort_on = [sort_on] valid_indexes = self.indexes() try: sort_on = [idx for idx in sort_on if idx in valid_indexes] except TypeError: # sort_on is not iterable sort_on = [] if not sort_on: kw.pop('sort_on', None) else: kw['sort_on'] = sort_on return ZCatalog.searchResults(self, query, **kw)
def searchResults(self, query=None, **kw): # Calls ZCatalog.searchResults with extra arguments that # limit the results to what the user is allowed to see. # # This version uses the 'effectiveRange' DateRangeIndex. # # It also accepts a keyword argument show_inactive to disable # effectiveRange checking entirely even for those without portal # wide AccessInactivePortalContent permission. # Make sure any pending index tasks have been processed processQueue() kw = kw.copy() show_inactive = kw.get('show_inactive', False) if isinstance(query, dict) and not show_inactive: show_inactive = 'show_inactive' in query user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if not show_inactive and not self.allow_inactive(kw): kw['effectiveRange'] = DateTime() # filter out invalid sort_on indexes sort_on = kw.get('sort_on') or [] if isinstance(sort_on, six.string_types): sort_on = [sort_on] valid_indexes = self.indexes() try: sort_on = [idx for idx in sort_on if idx in valid_indexes] except TypeError: # sort_on is not iterable sort_on = [] if not sort_on: kw.pop('sort_on', None) else: kw['sort_on'] = sort_on return ZCatalog.searchResults(self, query, **kw)
def searchResults(self, REQUEST=None, **kw): # Calls ZCatalog.searchResults with extra arguments that # limit the results to what the user is allowed to see. # # This version uses the 'effectiveRange' DateRangeIndex. # # It also accepts a keyword argument show_inactive to disable # effectiveRange checking entirely even for those without portal # wide AccessInactivePortalContent permission. kw = kw.copy() show_inactive = kw.get('show_inactive', False) if isinstance(REQUEST, dict) and not show_inactive: show_inactive = 'show_inactive' in REQUEST user = _getAuthenticatedUser(self) kw['allowedRolesAndUsers'] = self._listAllowedRolesAndUsers(user) if not show_inactive \ and not _checkPermission(AccessInactivePortalContent, self): kw['effectiveRange'] = DateTime() return ZCatalog.searchResults(self, REQUEST, **kw)
def searchResults(self, **kw): user = getSecurityManager().getUser() kw['allowedRolesAndUsers'] = _allowedRoles(user) return ZCatalog.searchResults(self, **kw)
def unrestrictedSearchResults(self, **kw): return ZCatalog.searchResults(self, **kw)
class UnicodeTextIndexCatalogTest(unittest.TestCase): def setUp(self): self.cat = ZCatalog("catalog") self.cat.addIndex('text',"TextIndex") self.cat.addColumn('text') self.cat.addIndex('kw','KeywordIndex') self.cat.addColumn('kw') t1 = TO('the quick brown fox jumps over the lazy dog',['quick','fox']) t2 = TO('i am the nice alien from the future',['alien','future']) t3 = TO('i am a brown fox dancing with a future alien',['zerstört','könnten']) t4 = TO('i am a brown ' + unicode('fox') + ' dancing with a future alien',[]) t5 = TO(""" Die USA und Großbritannien können nach der Zerstörung der afghanischen Luftabwehr nun rund um die Uhr Angriffe fliegen. Das gab Verteidigungsminister Donald Rumsfeld bekannt. Bei den dreitägigen Angriffen seien auch bis auf einen alle Flugplätze der Taliban zerstört worden. Rumsfeld erklärte weiter, er könne die Berichte nicht bestätigen, wonach bei den amerikanischen Angriffen vier afghanische Mitarbeiter einer von den UN finanzierten Hilfsorganisation getötet wurden. Diese könnten auch durch Gegenfeuer der Taliban getötet worden sein. """,[unicode('dreitägigen','latin1'),'zerstört']) self.cat.catalog_object(t1,"o1") self.cat.catalog_object(t2,"o2") self.cat.catalog_object(t3,"o3") self.cat.catalog_object(t4,"o4") self.cat.catalog_object(t5,"o5") self.tests = [('quick',('o1',)), ('fox',('o1','o3','o4')), ('afghanischen', ('o5',)), ('dreitägigen',('o5',)) ] self.kw_tests = [ ('quick',('o1',) ), ('zerstört',('o3','o5')), ('dreitägigen',('o5',)) ] def _doTests(self,tests,field,test_unicode=0): for q,objs in tests: if test_unicode: res=self.cat.searchResults({field:{'query':unicode(q,'latin1')}}) else: res=self.cat.searchResults({field:{'query':q}}) got = [ x.getURL() for x in res] got.sort() expected = list(objs) expected.sort() assert got == expected, \ "%s: got: %s, expected: %s" % (q,got,expected) def testAsciiQuery(self): """ ascii query textindex """ self._doTests(self.tests, 'text', test_unicode=0) def testUnicodeQuery(self): """ unicode query textindex """ self._doTests(self.tests, 'text', test_unicode=1)
class TestCatalogReport(cleanup.CleanUp, unittest.TestCase): def setUp(self): cleanup.CleanUp.setUp(self) from Products.ZCatalog.ZCatalog import ZCatalog self.zcat = ZCatalog('catalog') self.zcat.long_query_time = 0.0 self._add_indexes() for i in range(9): obj = dummy(i) self.zcat.catalog_object(obj, str(i)) def _add_indexes(self): from Products.PluginIndexes.FieldIndex.FieldIndex import FieldIndex from Products.PluginIndexes.KeywordIndex.KeywordIndex import \ KeywordIndex num = FieldIndex('num') self.zcat._catalog.addIndex('num', num) big = FieldIndex('big') self.zcat._catalog.addIndex('big', big) numbers = KeywordIndex('numbers') self.zcat._catalog.addIndex('numbers', numbers) def test_ReportLength(self): """ tests the report aggregation """ self.zcat.manage_resetCatalogReport() self.zcat.searchResults(numbers=4, sort_on='num') self.zcat.searchResults(numbers=1, sort_on='num') self.zcat.searchResults(numbers=3, sort_on='num') self.zcat.searchResults(big=True, sort_on='num') self.zcat.searchResults(big=True, sort_on='num') self.zcat.searchResults(big=False, sort_on='num') self.zcat.searchResults(num=[5, 4, 3], sort_on='num') self.zcat.searchResults(num=(3, 4, 5), sort_on='num') self.assertEqual(4, len(self.zcat.getCatalogReport())) def test_ReportCounter(self): """ tests the counter of equal queries """ self.zcat.manage_resetCatalogReport() self.zcat.searchResults(numbers=5, sort_on='num') self.zcat.searchResults(numbers=6, sort_on='num') self.zcat.searchResults(numbers=8, sort_on='num') r = self.zcat.getCatalogReport()[0] self.assertEqual(r['counter'], 3) def test_ReportKey(self): """ tests the query keys for uniqueness """ # query key 1 key = ('sort_on', ('big', 'True')) self.zcat.manage_resetCatalogReport() self.zcat.searchResults(big=True, sort_on='num') self.zcat.searchResults(big=True, sort_on='num') r = self.zcat.getCatalogReport()[0] self.assertEqual(r['query'], key) self.assertEqual(r['counter'], 2) # query key 2 key = ('sort_on', ('big', 'False')) self.zcat.manage_resetCatalogReport() self.zcat.searchResults(big=False, sort_on='num') r = self.zcat.getCatalogReport()[0] self.assertEqual(r['query'], key) self.assertEqual(r['counter'], 1) # query key 3 key = ('sort_on', ('num', '[3, 4, 5]')) self.zcat.manage_resetCatalogReport() self.zcat.searchResults(num=[5, 4, 3], sort_on='num') self.zcat.searchResults(num=(3, 4, 5), sort_on='num') r = self.zcat.getCatalogReport()[0] self.assertEqual(r['query'], key) self.assertEqual(r['counter'], 2)
def searchResults(self, REQUEST=None, **kw): return ZCatalog.searchResults(self, REQUEST, **kw)
class TestZCatalog(unittest.TestCase): def setUp(self): from Products.ZCatalog.ZCatalog import ZCatalog self._catalog = ZCatalog('Catalog') self._catalog.resolve_path = self._resolve_num self._catalog.addIndex('title', 'KeywordIndex') self._catalog.addColumn('title') self.upper = 10 self.d = {} for x in range(0, self.upper): # make uid a string of the number ob = zdummy(x) self.d[str(x)] = ob self._catalog.catalog_object(ob, str(x)) def _resolve_num(self, num): return self.d[num] def test_z2interfaces(self): from Interface.Verify import verifyClass from Products.ZCatalog.IZCatalog import IZCatalog from Products.ZCatalog.ZCatalog import ZCatalog verifyClass(IZCatalog, ZCatalog) def test_z3interfaces(self): from Products.ZCatalog.interfaces import IZCatalog from Products.ZCatalog.ZCatalog import ZCatalog from zope.interface.verify import verifyClass verifyClass(IZCatalog, ZCatalog) def testGetMetadataForUID(self): testNum = str(self.upper - 3) # as good as any.. data = self._catalog.getMetadataForUID(testNum) self.assertEqual(data['title'], testNum) def testGetIndexDataForUID(self): testNum = str(self.upper - 3) data = self._catalog.getIndexDataForUID(testNum) self.assertEqual(data['title'][0], testNum) def testSearch(self): query = {'title': ['5','6','7']} sr = self._catalog.searchResults(query) self.assertEqual(len(sr), 3) sr = self._catalog.search(query) self.assertEqual(len(sr), 3) def testUpdateMetadata(self): self._catalog.catalog_object(zdummy(1), '1') data = self._catalog.getMetadataForUID('1') self.assertEqual(data['title'], '1') self._catalog.catalog_object(zdummy(2), '1', update_metadata=0) data = self._catalog.getMetadataForUID('1') self.assertEqual(data['title'], '1') self._catalog.catalog_object(zdummy(2), '1', update_metadata=1) data = self._catalog.getMetadataForUID('1') self.assertEqual(data['title'], '2') # update_metadata defaults to true, test that here self._catalog.catalog_object(zdummy(1), '1') data = self._catalog.getMetadataForUID('1') self.assertEqual(data['title'], '1') def testReindexIndexDoesntDoMetadata(self): self.d['0'].num = 9999 self._catalog.reindexIndex('title', {}) data = self._catalog.getMetadataForUID('0') self.assertEqual(data['title'], '0') def testReindexIndexesFalse(self): # setup false_id = self.upper + 1 ob = zdummyFalse(false_id) self.d[str(false_id)] = ob self._catalog.catalog_object(ob, str(false_id)) # test, object evaluates to false; there was bug which caused the # object to be removed from index ob.num = 9999 self._catalog.reindexIndex('title', {}) result = self._catalog(title='9999') self.assertEquals(1, len(result)) def testBooleanEvalOn_manage_catalogObject(self): self.d['11'] = dummyLenFail(11, self.fail) self.d['12'] = dummyNonzeroFail(12, self.fail) # create a fake response that doesn't bomb on manage_catalogObject() class myresponse: def redirect(self, url): pass # this next call should not fail self._catalog.manage_catalogObject(None, myresponse(), 'URL1', urls=('11', '12')) def testBooleanEvalOn_refreshCatalog_getobject(self): # wrap catalog under the fake parent providing unrestrictedTraverse() catalog = self._catalog.__of__(fakeparent(self.d)) # replace entries to test refreshCatalog self.d['0'] = dummyLenFail(0, self.fail) self.d['1'] = dummyNonzeroFail(1, self.fail) # this next call should not fail catalog.refreshCatalog() for uid in ('0', '1'): rid = catalog.getrid(uid) # neither should these catalog.getobject(rid) def test_getobject_doesntMaskTraversalErrorsAndDoesntDelegateTo_resolve_url(self): # wrap catalog under the fake parent providing unrestrictedTraverse() catalog = self._catalog.__of__(fakeparent(self.d)) # make resolve_url fail if ZCatalog falls back on it def resolve_url(path, REQUEST): self.fail(".resolve_url() should not be called by .getobject()") catalog.resolve_url = resolve_url # traversal should work at first rid0 = catalog.getrid('0') # lets set it up so the traversal fails del self.d['0'] self.assertRaises(FakeTraversalError, catalog.getobject, rid0, REQUEST=object()) # and if there is a None at the traversal point, that's where it should return self.d['0'] = None self.assertEquals(catalog.getobject(rid0), None)
def localQuery(self, node=None, REQUEST=None, **kw): """ a placeful query for tasks""" if node is not None: kw[ 'path' ] = '/'.join(node.getPhysicalPath()) return ZCatalog.searchResults(self, REQUEST, **kw)
def unrestrictedSearchResults(self, **kw): return ZCatalog.searchResults(self, **kw)
def searchResults(self, **kw): user = getSecurityManager().getUser() kw['allowedRolesAndUsers'] = _allowedRoles(user) return ZCatalog.searchResults(self, **kw)
class TestCatalogReport(cleanup.CleanUp, unittest.TestCase): def setUp(self): cleanup.CleanUp.setUp(self) from Products.ZCatalog.ZCatalog import ZCatalog self.zcat = ZCatalog('catalog') self.zcat.long_query_time = 0.0 self._add_indexes() for i in range(9): obj = dummy(i) self.zcat.catalog_object(obj, str(i)) def _add_indexes(self): from Products.PluginIndexes.FieldIndex.FieldIndex import FieldIndex from Products.PluginIndexes.KeywordIndex.KeywordIndex import \ KeywordIndex num = FieldIndex('num') self.zcat._catalog.addIndex('num', num) big = FieldIndex('big') self.zcat._catalog.addIndex('big', big) numbers = KeywordIndex('numbers') self.zcat._catalog.addIndex('numbers', numbers) def test_ReportLength(self): """ tests the report aggregation """ self.zcat.manage_resetCatalogReport() self.zcat.searchResults(numbers=4, sort_on='num') self.zcat.searchResults(numbers=1, sort_on='num') self.zcat.searchResults(numbers=3, sort_on='num') self.zcat.searchResults(big=True, sort_on='num') self.zcat.searchResults(big=True, sort_on='num') self.zcat.searchResults(big=False, sort_on='num') self.zcat.searchResults(num=[5, 4, 3], sort_on='num') self.zcat.searchResults(num=(3, 4, 5), sort_on='num') self.assertEqual(4, len(self.zcat.getCatalogReport())) def test_ReportCounter(self): """ tests the counter of equal queries """ self.zcat.manage_resetCatalogReport() self.zcat.searchResults(numbers=5, sort_on='num') self.zcat.searchResults(numbers=6, sort_on='num') self.zcat.searchResults(numbers=8, sort_on='num') r = self.zcat.getCatalogReport()[0] self.assertEqual(r['counter'], 3) def test_ReportKey(self): """ tests the query keys for uniqueness """ # query key 1 key = ('sort_on', ('big', 'True')) self.zcat.manage_resetCatalogReport() self.zcat.searchResults(big=True, sort_on='num') self.zcat.searchResults(big=True, sort_on='num') r = self.zcat.getCatalogReport()[0] self.assertEqual(r['query'], key) self.assertEqual(r['counter'], 2) # query key 2 key = ('sort_on', ('big', 'False')) self.zcat.manage_resetCatalogReport() self.zcat.searchResults(big=False, sort_on='num') r = self.zcat.getCatalogReport()[0] self.assertEqual(r['query'], key) self.assertEqual(r['counter'], 1) # query key 3 key = ('sort_on', ('num', '[3, 4, 5]')) self.zcat.manage_resetCatalogReport() self.zcat.searchResults(num=[5, 4, 3], sort_on='num') self.zcat.searchResults(num=(3, 4, 5), sort_on='num') r = self.zcat.getCatalogReport()[0] self.assertEqual(r['query'], key) self.assertEqual(r['counter'], 2)
class TestZCatalog(unittest.TestCase): def setUp(self): from Products.ZCatalog.ZCatalog import ZCatalog self._catalog = ZCatalog('Catalog') self._catalog.resolve_path = self._resolve_num self._catalog.addIndex('title', 'KeywordIndex') self._catalog.addColumn('title') self.upper = 10 self.d = {} for x in range(0, self.upper): # make uid a string of the number ob = zdummy(x) self.d[str(x)] = ob self._catalog.catalog_object(ob, str(x)) def _resolve_num(self, num): return self.d[num] def test_z2interfaces(self): from Interface.Verify import verifyClass from Products.ZCatalog.IZCatalog import IZCatalog from Products.ZCatalog.ZCatalog import ZCatalog verifyClass(IZCatalog, ZCatalog) def test_z3interfaces(self): from Products.ZCatalog.interfaces import IZCatalog from Products.ZCatalog.ZCatalog import ZCatalog from zope.interface.verify import verifyClass verifyClass(IZCatalog, ZCatalog) def testGetMetadataForUID(self): testNum = str(self.upper - 3) # as good as any.. data = self._catalog.getMetadataForUID(testNum) self.assertEqual(data['title'], testNum) def testGetIndexDataForUID(self): testNum = str(self.upper - 3) data = self._catalog.getIndexDataForUID(testNum) self.assertEqual(data['title'][0], testNum) def testSearch(self): query = {'title': ['5', '6', '7']} sr = self._catalog.searchResults(query) self.assertEqual(len(sr), 3) sr = self._catalog.search(query) self.assertEqual(len(sr), 3) def testUpdateMetadata(self): self._catalog.catalog_object(zdummy(1), '1') data = self._catalog.getMetadataForUID('1') self.assertEqual(data['title'], '1') self._catalog.catalog_object(zdummy(2), '1', update_metadata=0) data = self._catalog.getMetadataForUID('1') self.assertEqual(data['title'], '1') self._catalog.catalog_object(zdummy(2), '1', update_metadata=1) data = self._catalog.getMetadataForUID('1') self.assertEqual(data['title'], '2') # update_metadata defaults to true, test that here self._catalog.catalog_object(zdummy(1), '1') data = self._catalog.getMetadataForUID('1') self.assertEqual(data['title'], '1') def testReindexIndexDoesntDoMetadata(self): self.d['0'].num = 9999 self._catalog.reindexIndex('title', {}) data = self._catalog.getMetadataForUID('0') self.assertEqual(data['title'], '0') def testReindexIndexesFalse(self): # setup false_id = self.upper + 1 ob = zdummyFalse(false_id) self.d[str(false_id)] = ob self._catalog.catalog_object(ob, str(false_id)) # test, object evaluates to false; there was bug which caused the # object to be removed from index ob.num = 9999 self._catalog.reindexIndex('title', {}) result = self._catalog(title='9999') self.assertEquals(1, len(result)) def testBooleanEvalOn_manage_catalogObject(self): self.d['11'] = dummyLenFail(11, self.fail) self.d['12'] = dummyNonzeroFail(12, self.fail) # create a fake response that doesn't bomb on manage_catalogObject() class myresponse: def redirect(self, url): pass # this next call should not fail self._catalog.manage_catalogObject(None, myresponse(), 'URL1', urls=('11', '12')) def testBooleanEvalOn_refreshCatalog_getobject(self): # wrap catalog under the fake parent providing unrestrictedTraverse() catalog = self._catalog.__of__(fakeparent(self.d)) # replace entries to test refreshCatalog self.d['0'] = dummyLenFail(0, self.fail) self.d['1'] = dummyNonzeroFail(1, self.fail) # this next call should not fail catalog.refreshCatalog() for uid in ('0', '1'): rid = catalog.getrid(uid) # neither should these catalog.getobject(rid) def test_getobject_doesntMaskTraversalErrorsAndDoesntDelegateTo_resolve_url( self): # wrap catalog under the fake parent providing unrestrictedTraverse() catalog = self._catalog.__of__(fakeparent(self.d)) # make resolve_url fail if ZCatalog falls back on it def resolve_url(path, REQUEST): self.fail(".resolve_url() should not be called by .getobject()") catalog.resolve_url = resolve_url # traversal should work at first rid0 = catalog.getrid('0') # lets set it up so the traversal fails del self.d['0'] self.assertRaises(FakeTraversalError, catalog.getobject, rid0, REQUEST=object()) # and if there is a None at the traversal point, that's where it should return self.d['0'] = None self.assertEquals(catalog.getobject(rid0), None)