Allow sorting and filtering by camelCase field names (#1019)

The API exposes camelCase field names everywhere, but only accepted
underscore_field_names in 'filter' or 'sort' GET attributes. Now both
are allowed.
This commit is contained in:
Marti Raudsepp 2017-12-12 19:44:53 +02:00 committed by kevgliss
parent 6edc5180c7
commit b2d87940d6
1 changed files with 3 additions and 2 deletions

View File

@ -9,6 +9,7 @@
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com> .. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
""" """
from inflection import underscore
from sqlalchemy import exc from sqlalchemy import exc
from sqlalchemy.sql import and_, or_ from sqlalchemy.sql import and_, or_
from sqlalchemy.orm import make_transient from sqlalchemy.orm import make_transient
@ -198,7 +199,7 @@ def filter(query, model, terms):
:param terms: :param terms:
:return: :return:
""" """
column = get_model_column(model, terms[0]) column = get_model_column(model, underscore(terms[0]))
return query.filter(column.ilike('%{}%'.format(terms[1]))) return query.filter(column.ilike('%{}%'.format(terms[1])))
@ -212,7 +213,7 @@ def sort(query, model, field, direction):
:param field: :param field:
:param direction: :param direction:
""" """
column = get_model_column(model, field) column = get_model_column(model, underscore(field))
return query.order_by(column.desc() if direction == 'desc' else column.asc()) return query.order_by(column.desc() if direction == 'desc' else column.asc())