def __call__(self, search, params): # If there are no uris to search for there is nothing to do so return early. if not ("uri" in params or "url" in params or "wildcard_uri" in params): return search if self.separate_keys: uris = [ add_default_scheme(u) for u in popall(params, "uri") + popall(params, "url") ] wildcard_uris = [ add_default_scheme(u) for u in popall(params, "wildcard_uri") ] else: uris = [ add_default_scheme(u) for u in popall(params, "uri") + popall(params, "url") ] # Split into wildcard uris and non wildcard uris. wildcard_uris = [u for u in uris if "*" in u or "_" in u] uris = [u for u in uris if "*" not in u and "_" not in u] # Only add valid uri's to the search list. wildcard_uris = self._normalize_uris( [u for u in wildcard_uris if wildcard_uri_is_valid(u)], normalize_method=self._wildcard_uri_normalized, ) uris = self._normalize_uris(uris) queries = [] if wildcard_uris: queries = [Q("wildcard", **{"target.scope": u}) for u in wildcard_uris] if uris: queries.append(Q("terms", **{"target.scope": uris})) return search.query("bool", should=queries)
def __call__(self, search, params): # If there are no uris to search for there is nothing to do so return early. if not ("uri" in params or "url" in params or "wildcard_uri" in params): return search if self.separate_keys: uris = [ add_default_scheme(u) for u in popall(params, "uri") + popall(params, "url") ] wildcard_uris = [ add_default_scheme(u) for u in popall(params, "wildcard_uri") ] else: uris = [ add_default_scheme(u) for u in popall(params, "uri") + popall(params, "url") ] # Split into wildcard uris and non wildcard uris. wildcard_uris = [u for u in uris if "*" in u or "_" in u] uris = [u for u in uris if "*" not in u and "_" not in u] # Only add valid uri's to the search list. wildcard_uris = self._normalize_uris( [u for u in wildcard_uris if wildcard_uri_is_valid(u)], normalize_method=self._wildcard_uri_normalized, ) uris = self._normalize_uris(uris) queries = [] if wildcard_uris: queries = [ Q("wildcard", **{"target.scope": u}) for u in wildcard_uris ] if uris: queries.append(Q("terms", **{"target.scope": uris})) return search.query("bool", should=queries)
def test_add_default_scheme(uri, expected): assert util.add_default_scheme(uri) == expected
def test_add_default_scheme(uri, expected): assert util.add_default_scheme(uri) == expected