dbcore: parse_sorted_query (#953)

This commit is contained in:
Adrian Sampson 2014-09-15 17:52:52 -07:00
parent 08b9b90f74
commit 0f37737168
3 changed files with 28 additions and 8 deletions

View file

@ -20,5 +20,6 @@ from .query import Query, FieldQuery, MatchQuery, AndQuery, OrQuery
from .types import Type
from .queryparse import query_from_strings
from .queryparse import sort_from_strings
from .queryparse import parse_sorted_query
# flake8: noqa

View file

@ -157,3 +157,28 @@ def sort_from_strings(model_cls, sort_parts):
for part in sort_parts:
sort.add_sort(construct_sort_part(model_cls, part))
return sort
def parse_sorted_query(model_cls, parts, prefixes={},
query_cls=query.AndQuery):
"""Given a list of strings, create the `Query` and `Sort` that they
represent.
Return a `SortedQuery` namedtuple, which is a pair of a `Query` and
`Sort`.
"""
# Separate query token and sort token.
query_parts = []
sort_parts = []
for part in parts:
if part.endswith((u'+', u'-')) and u':' not in part:
sort_parts.append(part)
else:
query_parts.append(part)
# Parse each.
q = query_from_strings(
query_cls, model_cls, prefixes, query_parts
)
s = sort_from_strings(model_cls, sort_parts)
return query.SortedQuery(q, s)

View file

@ -959,15 +959,9 @@ def get_query_sort(val, model_cls):
path_parts = ()
non_path_parts = val
# separate query token and sort token
query_val = [s for s in non_path_parts if not s.endswith(('+', '-'))]
sort_val = [s for s in non_path_parts if s.endswith(('+', '-'))]
# Parse remaining parts and construct an AndQuery.
query = dbcore.query_from_strings(
dbcore.AndQuery, model_cls, prefixes, query_val
query, sort = dbcore.parse_sorted_query(
model_cls, non_path_parts, prefixes
)
sort = dbcore.sort_from_strings(model_cls, sort_val)
# Add path queries to aggregate query.
if path_parts: