Skip to content

Commit

Permalink
Fixes sorting by multiple fields on a relation
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinkels committed Mar 16, 2016
1 parent 6fd4671 commit 64a5711
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Not yet released.
- #431: adds a ``url_prefix`` keyword argument to the :class:`APIManager`
constructor, so one can specify a URL prefix once for all created APIs.
- #449: roll back the session on any SQLAlchemy error, not just a few.
- #432, #462: alias relation names when sorting by multiple attributes on a
relationship.
- #436, #453: use ``__table__.name`` instead of ``__tablename__`` to infer the
collection name for the SQLAlchemy model.
- #440, #475: uses the serialization function provided at the time of invoking
Expand Down
3 changes: 2 additions & 1 deletion flask_restless/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from sqlalchemy import and_
from sqlalchemy import or_
from sqlalchemy.ext.associationproxy import AssociationProxy
from sqlalchemy.orm import aliased
from sqlalchemy.orm.attributes import InstrumentedAttribute

from .helpers import get_model
Expand Down Expand Up @@ -414,7 +415,7 @@ def search(session, model, filters=None, sort=None, group_by=None,
direction_name = 'asc' if symbol == '+' else 'desc'
if '.' in field_name:
field_name, field_name_in_relation = field_name.split('.')
relation_model = get_related_model(model, field_name)
relation_model = aliased(get_related_model(model, field_name))
field = getattr(relation_model, field_name_in_relation)
direction = getattr(field, direction_name)
query = query.join(relation_model)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_jsonapi/test_fetching_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,32 @@ def test_sort_relationship_attributes(self):
articles = document['data']
assert ['2', '1', '3'] == [c['id'] for c in articles]


def test_sort_multiple_relationship_attributes(self):
"""Tests that the client can sort by multiple relationship
attributes.
For more information, see the `Sorting`_ section of the JSON API
specification.
.. _Sorting: http://jsonapi.org/format/#fetching-sorting
"""
person1 = self.Person(age=2, name=u'd')
person2 = self.Person(age=1, name=u'b')
person3 = self.Person(age=1, name=u'a')
person4 = self.Person(age=2, name=u'c')
people = [person1, person2, person3, person4]
articles = [self.Article(id=i, author=person)
for i, person in enumerate(people, start=1)]
self.session.add_all(people + articles)
self.session.commit()
query_string = {'sort': 'author.age,author.name'}
response = self.app.get('/api/article', query_string=query_string)
document = loads(response.data)
articles = document['data']
assert ['3', '2', '4', '1'] == [c['id'] for c in articles]

def test_sorting_relationship(self):
"""Tests for sorting relationship objects when requesting
information from a to-many relationship endpoint.
Expand Down

0 comments on commit 64a5711

Please sign in to comment.