-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Overseas-Student-Living/sorting
Add apply sort functionality
- Loading branch information
Showing
15 changed files
with
401 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ install: | |
- pip install tox | ||
|
||
env: | ||
- TOX_ENV=py33 | ||
- TOX_ENV=py34 | ||
|
||
script: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Release Notes | ||
============= | ||
|
||
Here you can see the full list of changes between sqlalchemy-filters | ||
versions, where semantic versioning is used: *major.minor.patch*. | ||
|
||
Backwards-compatible changes increment the minor version number only. | ||
|
||
Version 0.2.0 | ||
------------- | ||
|
||
Released 2017-01-06 | ||
|
||
* Adds apply query pagination | ||
* Adds apply query sort | ||
* Adds Travis CI | ||
* Starts using Tox | ||
* Refactors Makefile and conftest | ||
|
||
Version 0.1.0 | ||
------------- | ||
|
||
Released 2016-09-08 | ||
|
||
* Initial version | ||
* Adds apply query filters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .filters import apply_filters, get_query_models # noqa: F401 | ||
from .filters import apply_filters # noqa: F401 | ||
from .models import get_query_models # noqa: F401 | ||
from .pagination import apply_pagination # noqa: F401 | ||
from .sorting import apply_sort # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from sqlalchemy.inspection import inspect | ||
|
||
from .exceptions import FieldNotFound, BadQuery | ||
|
||
|
||
class Field(object): | ||
|
||
def __init__(self, models, field_name): | ||
# TODO: remove this check once we start supporing multiple models | ||
if len(models) > 1: | ||
raise BadQuery('The query should contain only one model.') | ||
|
||
self.model = self._get_model(models) | ||
self.field_name = field_name | ||
|
||
def _get_model(self, models): | ||
# TODO: add model_name argument once we start supporing multiple models | ||
return [v for (k, v) in models.items()][0] # first (and only) model | ||
|
||
def get_sqlalchemy_field(self): | ||
if self.field_name not in inspect(self.model).columns.keys(): | ||
raise FieldNotFound( | ||
'Model {} has no column `{}`.'.format( | ||
self.model, self.field_name | ||
) | ||
) | ||
return getattr(self.model, self.field_name) | ||
|
||
|
||
def get_query_models(query): | ||
"""Get models from query. | ||
:param query: | ||
A :class:`sqlalchemy.orm.Query` instance. | ||
:returns: | ||
A dictionary with all the models included in the query. | ||
""" | ||
return { | ||
entity['type'].__name__: entity['type'] | ||
for entity in query.column_descriptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .exceptions import BadQuery, BadSortFormat | ||
from .models import Field, get_query_models | ||
|
||
def apply_sort(query, sort): # pragma: no cover | ||
# TODO | ||
raise NotImplemented() | ||
|
||
SORT_ASCENDING = 'asc' | ||
SORT_DESCENDING = 'desc' | ||
|
||
|
||
class Sort(object): | ||
|
||
def __init__(self, sort, models): | ||
try: | ||
field_name = sort['field'] | ||
direction = sort['direction'] | ||
except KeyError: | ||
raise BadSortFormat( | ||
'`field` and `direction` are mandatory attributes.' | ||
) | ||
except TypeError: | ||
raise BadSortFormat( | ||
'Sort `{}` should be a dictionary.'.format(sort) | ||
) | ||
|
||
if direction not in [SORT_ASCENDING, SORT_DESCENDING]: | ||
raise BadSortFormat('Direction `{}` not valid.'.format(direction)) | ||
|
||
self.field = Field(models, field_name) | ||
self.direction = direction | ||
|
||
def format_for_sqlalchemy(self): | ||
field = self.field.get_sqlalchemy_field() | ||
|
||
if self.direction == SORT_ASCENDING: | ||
return field.asc() | ||
elif self.direction == SORT_DESCENDING: | ||
return field.desc() | ||
|
||
|
||
def apply_sort(query, order_by): | ||
"""Apply sorting to a :class:`sqlalchemy.orm.Query` instance. | ||
:param order_by: | ||
A list of dictionaries, where each one of them includes | ||
the necesary information to order the elements of the query. | ||
Example:: | ||
order_by = [ | ||
{'field': 'name', 'direction': 'asc'}, | ||
{'field': 'id', 'direction': 'desc'}, | ||
] | ||
:returns: | ||
The :class:`sqlalchemy.orm.Query` instance after the provided | ||
sorting has been applied. | ||
""" | ||
models = get_query_models(query) | ||
if not models: | ||
raise BadQuery('The query does not contain any models.') | ||
|
||
sqlalchemy_order_by = [ | ||
Sort(sort, models).format_for_sqlalchemy() for sort in order_by | ||
] | ||
if sqlalchemy_order_by: | ||
query = query.order_by(*sqlalchemy_order_by) | ||
|
||
return query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
def error_value(exception): | ||
return exception.value.args[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from sqlalchemy_filters import get_query_models | ||
from test.models import Bar, Qux | ||
|
||
|
||
class TestGetQueryModels(object): | ||
|
||
def test_query_with_no_models(self, session): | ||
query = session.query() | ||
|
||
entities = get_query_models(query) | ||
|
||
assert {} == entities | ||
|
||
def test_query_with_one_model(self, session): | ||
query = session.query(Bar) | ||
|
||
entities = get_query_models(query) | ||
|
||
assert {'Bar': Bar} == entities | ||
|
||
def test_query_with_multiple_models(self, session): | ||
query = session.query(Bar, Qux) | ||
|
||
entities = get_query_models(query) | ||
|
||
assert {'Bar': Bar, 'Qux': Qux} == entities | ||
|
||
def test_query_with_duplicated_models(self, session): | ||
query = session.query(Bar, Qux, Bar) | ||
|
||
entities = get_query_models(query) | ||
|
||
assert {'Bar': Bar, 'Qux': Qux} == entities |
Oops, something went wrong.