def test_is_simple_search_considers_anyting_with_quotes_not_simple(self): self.assertTrue(isSimpleSearch('foo')) self.assertTrue(isSimpleSearch('foo bar')) self.assertFalse(isSimpleSearch('"bar"')) self.assertFalse(isSimpleSearch('foo "bar"')) self.assertFalse(isSimpleSearch('foo"bar'))
def test_simple_search_may_contain_single_qotes(self): self.assertTrue(isSimpleSearch("d'bar")) self.assertTrue(isSimpleSearch("it's"))
def test_simple_search_may_contain_hyphon(self): self.assertTrue(isSimpleSearch('foo-bar')) self.assertTrue(isSimpleSearch('foo- bar'))
def test_simple_search_may_contain_commas(self): self.assertTrue(isSimpleSearch('foo,bar')) self.assertTrue(isSimpleSearch('foo-, bar'))
def test_simple_searches_may_contain_dots(self): self.assertTrue(isSimpleSearch('foo.bar')) self.assertTrue(isSimpleSearch('foo.bar baz'))
def mangleQuery(keywords, config, schema): """ translate / mangle query parameters to replace zope specifics with equivalent constructs for solr """ extras = {} for key, value in keywords.items(): if key.endswith('_usage'): # convert old-style parameters category, spec = value.split(':', 1) extras[key[:-6]] = {category: spec} del keywords[key] elif isinstance(value, dict): # unify dict parameters keywords[key] = value['query'] del value['query'] extras[key] = value elif hasattr(value, 'query'): # unify object parameters keywords[key] = value.query extra = dict() for arg in query_args: arg_val = getattr(value, arg, None) if arg_val is not None: extra[arg] = arg_val extras[key] = extra elif key in ignored: del keywords[key] # find EPI indexes if schema: epi_indexes = {} for name in schema.keys(): parts = name.split('_') if parts[-1] in ['string', 'depth', 'parents']: count = epi_indexes.get(parts[0], 0) epi_indexes[parts[0]] = count + 1 epi_indexes = [k for k, v in epi_indexes.items() if v == 3] else: epi_indexes = ['path'] for key, value in keywords.items(): args = extras.get(key, {}) if key == 'SearchableText': pattern = getattr(config, 'search_pattern', '') if pattern and isSimpleSearch(value): value = mangle_searchable_text_query(value, pattern) keywords[key] = set([value]) # add literal query parameter continue elif isSimpleTerm(value): # use prefix/wildcard search keywords[key] = '(%s* OR %s)' % ( prepare_wildcard(value), value) continue if key in epi_indexes: path = keywords['%s_parents' % key] = value del keywords[key] if 'depth' in args: depth = int(args['depth']) if depth >= 0: if not isinstance(value, (list, tuple)): path = [path] tmpl = '(+%s_depth:[%d TO %d] AND +%s_parents:%s)' params = keywords['%s_parents' % key] = set() for p in path: base = len(p.split('/')) params.add(tmpl % (key, base, base + depth, key, p)) del args['depth'] elif key == 'effectiveRange': if isinstance(value, DateTime): steps = getattr(config, 'effective_steps', 1) if steps > 1: value = DateTime(value.timeTime() // steps * steps) value = iso8601date(value) del keywords[key] keywords['effective'] = '[* TO %s]' % value keywords['expires'] = '[%s TO *]' % value elif key == 'show_inactive': del keywords[key] # marker for `effectiveRange` elif 'range' in args: if not isinstance(value, (list, tuple)): value = [value] payload = map(iso8601date, value) keywords[key] = ranges[args['range']] % tuple(payload) del args['range'] elif 'operator' in args: if isinstance(value, (list, tuple)) and len(value) > 1: sep = ' %s ' % args['operator'].upper() value = sep.join(map(str, map(iso8601date, value))) keywords[key] = '(%s)' % value del args['operator'] elif key == 'allowedRolesAndUsers': if getattr(config, 'exclude_user', False): token = 'user$' + getSecurityManager().getUser().getId() if token in value: value.remove(token) elif isinstance(value, DateTime): keywords[key] = iso8601date(value) elif not isinstance(value, basestring): assert not args, 'unsupported usage: %r' % args
def mangleQuery(keywords, config, schema): """ translate / mangle query parameters to replace zope specifics with equivalent constructs for solr """ extras = {} for key, value in keywords.items(): if key.endswith('_usage'): # convert old-style parameters category, spec = value.split(':', 1) extras[key[:-6]] = {category: spec} del keywords[key] elif isinstance(value, dict): # unify dict parameters keywords[key] = value['query'] del value['query'] extras[key] = value elif hasattr(value, 'query'): # unify object parameters keywords[key] = value.query extra = dict() for arg in query_args: arg_val = getattr(value, arg, None) if arg_val is not None: extra[arg] = arg_val extras[key] = extra elif key in ignored: del keywords[key] # find EPI indexes if schema: epi_indexes = {} for name in schema.keys(): parts = name.split('_') if parts[-1] in ['string', 'depth', 'parents']: count = epi_indexes.get(parts[0], 0) epi_indexes[parts[0]] = count + 1 epi_indexes = [k for k, v in epi_indexes.items() if v == 3] else: epi_indexes = ['path'] for key, value in keywords.items(): args = extras.get(key, {}) if key == 'SearchableText': pattern = getattr(config, 'search_pattern', '') if pattern and isSimpleSearch(value): value = mangle_searchable_text_query(value, pattern) keywords[key] = set([value]) # add literal query parameter continue elif isSimpleTerm(value): # use prefix/wildcard search keywords[key] = '(%s* OR %s)' % (prepare_wildcard(value), value) continue if key in epi_indexes: path = keywords['%s_parents' % key] = value del keywords[key] if 'depth' in args: depth = int(args['depth']) if depth >= 0: if not isinstance(value, (list, tuple)): path = [path] tmpl = '(+%s_depth:[%d TO %d] AND +%s_parents:%s)' params = keywords['%s_parents' % key] = set() for p in path: base = len(p.split('/')) params.add(tmpl % (key, base, base + depth, key, p)) del args['depth'] elif key == 'effectiveRange': if isinstance(value, DateTime): steps = getattr(config, 'effective_steps', 1) if steps > 1: value = DateTime(value.timeTime() // steps * steps) value = iso8601date(value) del keywords[key] keywords['effective'] = '[* TO %s]' % value keywords['expires'] = '[%s TO *]' % value elif key == 'show_inactive': del keywords[key] # marker for `effectiveRange` elif 'range' in args: if not isinstance(value, (list, tuple)): value = [value] payload = map(iso8601date, value) keywords[key] = ranges[args['range']] % tuple(payload) del args['range'] elif 'operator' in args: if isinstance(value, (list, tuple)) and len(value) > 1: sep = ' %s ' % args['operator'].upper() value = sep.join(map(str, map(iso8601date, value))) keywords[key] = '(%s)' % value del args['operator'] elif key == 'allowedRolesAndUsers': if getattr(config, 'exclude_user', False): token = 'user$' + getSecurityManager().getUser().getId() if token in value: value.remove(token) elif isinstance(value, DateTime): keywords[key] = iso8601date(value) elif not isinstance(value, basestring): assert not args, 'unsupported usage: %r' % args