Ejemplo n.º 1
0
    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")
Ejemplo n.º 2
0
 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'))"
     )
Ejemplo n.º 3
0
 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')))"
     )
Ejemplo n.º 4
0
 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'))")
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
 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')))"
     )
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
 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')))"
     )
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
 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')))")
Ejemplo n.º 14
0
 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')))")
Ejemplo n.º 15
0
 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'))")
Ejemplo n.º 16
0
 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')))")
Ejemplo n.º 17
0
 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'))")
Ejemplo n.º 18
0
 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'))")
Ejemplo n.º 19
0
 def test_field_query_exact(self):
     parser = ParseSQ()
     query = parser.parse('fullname:"normal"', ["fullname", "username"])
     self.assertEqual(clean(query), "(AND: ('fullname__exact', 'normal'))")
Ejemplo n.º 20
0
 def test_field_query(self):
     parser = ParseSQ()
     query = parser.parse("fullname:normal", ["fullname", "username"])
     self.assertEqual(clean(query), "(AND: ('fullname', 'normal'))")
Ejemplo n.º 21
0
 def test_normal_query(self):
     parser = ParseSQ()
     query = parser.parse("normal", ["fullname", "username"])
     self.assertEqual(clean(query), "(OR: ('fullname', 'normal'), ('username', 'normal'))")
Ejemplo n.º 22
0
 def test_searchUser_emptyquery(self):
     parser = ParseSQ()
     query = parser.parse("", ["fullname", "username"])
Ejemplo n.º 23
0
 def test_normal_query(self):
     parser = ParseSQ()
     query = parser.parse("normal", ["fullname", "username"])
     self.assertEqual(
         clean(query),
         "(OR: ('fullname', 'normal'), ('username', 'normal'))")
Ejemplo n.º 24
0
 def test_searchUser_emptyquery(self):
     parser = ParseSQ()
     query = parser.parse("", ["fullname", "username"])
Ejemplo n.º 25
0
 def test_field_query(self):
     parser = ParseSQ()
     query = parser.parse("fullname:normal", ["fullname", "username"])
     self.assertEqual(clean(query), "(AND: ('fullname', 'normal'))")
Ejemplo n.º 26
0
    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")
Ejemplo n.º 27
0
 def test_field_query_exact(self):
     parser = ParseSQ()
     query = parser.parse('fullname:"normal"', ["fullname", "username"])
     self.assertEqual(clean(query), "(AND: ('fullname__exact', 'normal'))")
Ejemplo n.º 28
0
 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'))")