def test_field_query_not_is_monoargument(self): parser = ParseSQ() query = parser.parse("NOT one two", ["fullname", "username"]) self.assertEqual( clean(query), "(AND: (NOT (AND: (OR: ('fullname', 'one'), ('username', 'one')))), (OR: ('fullname', 'two'), ('username', 'two')))" )
def test_field_query_or(self): parser = ParseSQ() query = parser.parse("one OR two", ["fullname", "username"]) self.assertEqual( clean(query), "(OR: ('fullname', 'one'), ('username', 'one'), ('fullname', 'two'), ('username', 'two'))" )
def searchResource(querytext, request, pagenum=1, maxresults=30, staff=False, scope=None, orderby=('-creation_date',)): sqs = SearchQuerySet().models(CatalogueResource).all() if len(querytext) > 0: parser = ParseSQ() query = parser.parse(querytext, CONTENT_FIELDS) # If there's any query if len(query) > 0: sqs = sqs.filter(query) sqs = sqs.order_by(*orderby).group_by("group_field", order_by='-version_sortable') # Filter resource type if scope is not None and len(scope) > 0: q = Q(type='_%s_' % scope.pop()) for s in scope: q |= Q(type='_%s_' % s) sqs = sqs.filter(q) # Filter available only if not staff: q = Q(public=True) | Q(users=request.user.id) # Add group filters for group in request.user.groups.values_list('id', flat=True): q |= Q(groups=group) sqs = sqs.filter(q) # Build response data return buildSearchResults(sqs, pagenum, maxresults, cleanResults, request)
def test_field_query_single_quotes(self): parser = ParseSQ() query = parser.parse("'several words whitespace'", ["fullname", "username"]) self.assertEqual( clean(query), "(AND: ('content__exact', 'several words whitespace'))")
def test_field_query_multiple_word(self): parser = ParseSQ() query = parser.parse("one two", ["fullname", "username"]) self.assertEqual( clean(query), "(AND: (OR: ('fullname', 'one'), ('username', 'one')), (OR: ('fullname', 'two'), ('username', 'two')))" )
def searchWorkspace(request, querytext, pagenum, maxresults, orderby=None): sqs = SearchQuerySet().models(Workspace).all() # Only searchable results sqs = sqs.filter(searchable=1) if len(querytext) > 0: parser = ParseSQ() query = parser.parse(querytext, CONTENT_FIELDS) # If there's any query if len(query) > 0: sqs = sqs.filter(query) q = Q(public=True) if request.user.is_authenticated(): q |= Q(users=request.user.username) for group in request.user.groups.values_list("name", flat=True): q |= Q(groups=group) sqs = sqs.filter(q) if orderby is not None: sqs = sqs.order_by(*orderby) # Build response data return buildSearchResults(sqs, pagenum, maxresults, cleanResults)
def test_field_query_nested_brakets(self): parser = ParseSQ() query = parser.parse("(one OR (two AND NOT four)) AND three", ["fullname", "username"]) self.assertEqual( clean(query), "(AND: (OR: ('fullname', 'one'), ('username', 'one'), (AND: (OR: ('fullname', 'two'), ('username', 'two')), (NOT (AND: (OR: ('fullname', 'four'), ('username', 'four')))))), (OR: ('fullname', 'three'), ('username', 'three')))" )
def test_field_query_unbalanced_braket(self): parser = ParseSQ() with self.assertRaises(NoMatchingBracketsFound) as cm: parser.parse("(one OR two AND three", ["fullname", "username"]) self.assertEqual( "%s" % cm.exception, "Matching brackets were not found: (one OR two AND three")
def searchGroup(request, querytext, pagenum, maxresults, orderby=None): sqs = SearchQuerySet().models(Group).all() if len(querytext) > 0: parser = ParseSQ() sqs = sqs.filter(parser.parse(querytext, GROUP_CONTENT_FIELDS)) if orderby is not None: sqs = sqs.order_by(*orderby) return buildSearchResults(sqs, pagenum, maxresults, cleanGroupResults)
def searchWorkspace(request, querytext, pagenum, maxresults, orderby=None): sqs = SearchQuerySet().models(Workspace).all() # Only searchable results sqs = sqs.filter(searchable=1) if len(querytext) > 0: parser = ParseSQ() query = parser.parse(querytext, CONTENT_FIELDS) # If there's any query if len(query) > 0: sqs = sqs.filter(query) q = Q(public=True) | Q(users=request.user.username) for group in request.user.groups.values_list("name", flat=True): q |= Q(groups=group) sqs = sqs.filter(q) if orderby is not None: sqs = sqs.order_by(*orderby) # Build response data return buildSearchResults(sqs, pagenum, maxresults, cleanResults)
def test_field_query_nested_brakets(self): parser = ParseSQ() query = parser.parse("(one OR (two AND NOT four)) AND three", ["fullname", "username"]) self.assertEqual(clean(query), "(AND: (OR: ('fullname', 'one'), ('username', 'one'), (AND: (OR: ('fullname', 'two'), ('username', 'two')), (NOT (AND: (OR: ('fullname', 'four'), ('username', 'four')))))), (OR: ('fullname', 'three'), ('username', 'three')))")
def test_field_query_multiple_word(self): parser = ParseSQ() query = parser.parse("one two", ["fullname", "username"]) self.assertEqual(clean(query), "(AND: (OR: ('fullname', 'one'), ('username', 'one')), (OR: ('fullname', 'two'), ('username', 'two')))")
def test_field_query_or(self): parser = ParseSQ() query = parser.parse("one OR two", ["fullname", "username"]) self.assertEqual(clean(query), "(OR: ('fullname', 'one'), ('username', 'one'), ('fullname', 'two'), ('username', 'two'))")
def test_field_query_not_is_monoargument(self): parser = ParseSQ() query = parser.parse("NOT one two", ["fullname", "username"]) self.assertEqual(clean(query), "(AND: (NOT (AND: (OR: ('fullname', 'one'), ('username', 'one')))), (OR: ('fullname', 'two'), ('username', 'two')))")
def test_field_query_single_quotes(self): parser = ParseSQ() query = parser.parse("'several words whitespace'", ["fullname", "username"]) self.assertEqual(clean(query), "(AND: ('content__exact', 'several words whitespace'))")
def test_field_query_exact_single_quotes(self): parser = ParseSQ() query = parser.parse("fullname:'normal'", ["fullname", "username"]) self.assertEqual(clean(query), "(AND: ('fullname__exact', 'normal'))")
def test_field_query_exact(self): parser = ParseSQ() query = parser.parse('fullname:"normal"', ["fullname", "username"]) self.assertEqual(clean(query), "(AND: ('fullname__exact', 'normal'))")
def test_field_query(self): parser = ParseSQ() query = parser.parse("fullname:normal", ["fullname", "username"]) self.assertEqual(clean(query), "(AND: ('fullname', 'normal'))")
def test_normal_query(self): parser = ParseSQ() query = parser.parse("normal", ["fullname", "username"]) self.assertEqual(clean(query), "(OR: ('fullname', 'normal'), ('username', 'normal'))")
def test_searchUser_emptyquery(self): parser = ParseSQ() query = parser.parse("", ["fullname", "username"])
def test_normal_query(self): parser = ParseSQ() query = parser.parse("normal", ["fullname", "username"]) self.assertEqual( clean(query), "(OR: ('fullname', 'normal'), ('username', 'normal'))")
def test_field_query_unbalanced_braket(self): parser = ParseSQ() with self.assertRaises(NoMatchingBracketsFound) as cm: parser.parse("(one OR two AND three", ["fullname", "username"]) self.assertEqual("%s" % cm.exception, "Matching brackets were not found: (one OR two AND three")