(query, field_to_col_map, order_field, order_dir, default=None)
return query.order_by(*order_cols) if order_cols else query
Apply sorting operations over a SQL query
Apply sorting operations over a SQL query
Apply sorting operations over a SQL query
def sort_results(query, field_to_col_map, order_field, order_dir, default=None):
    """
    Apply sorting operations over a SQL query
    """
    order_cols = field_to_col_map.get(order_field, None)

    if order_cols and order_dir in (ORDER_DIRECTIONS.ASCENDING, ORDER_DIRECTIONS.DESCENDING):
        # Apply the proper sqlalchemy function for sorting direction over every
        # column declared on field_to_col_map[order_field]
        dir_func = asc if order_dir == ORDER_DIRECTIONS.ASCENDING else desc
        order_cols = list(map(dir_func, order_cols))
    else:
        # Use default ordering if declared if any parameter didn't met the requirements
        order_cols = [default] if default is not None else None

    return query.order_by(*order_cols) if order_cols else query
def sort_results ( query , field_to_col_map , order_field , order_dir , default = None ) : order_cols = field_to_col_map . get ( order_field , None ) if order_cols and order_dir in ( ORDER_DIRECTIONS . ASCENDING , ORDER_DIRECTIONS . DESCENDING ) : # Apply the proper sqlalchemy function for sorting direction over every # column declared on field_to_col_map[order_field] dir_func = asc if order_dir == ORDER_DIRECTIONS . ASCENDING else desc order_cols = list ( map ( dir_func , order_cols ) ) else : # Use default ordering if declared if any parameter didn't met the requirements order_cols = [ default ] if default is not None else None return query . order_by ( * order_cols ) if order_cols else query