def order_by_name(query, table, language=None, *extra_languages, **kwargs):
 name_attribute = kwargs.pop('name', 'name')
 if kwargs:
  raise ValueError(('Unexpected keyword arguments: %s' % kwargs.keys()))
 order_columns = []
 if (language is None):
  query = query.outerjoin(table.names_local)
  order_columns.append(func.lower(getattr(table.names_table, name_attribute)))
 else:
  extra_languages = ((language,) + extra_languages)
 for language in extra_languages:
  names_table = aliased(table.names_table)
  query = query.outerjoin(names_table)
  query = query.filter((names_table.foreign_id == table.id))
  query = query.filter((names_table.local_language_id == language.id))
  order_columns.append(func.lower(getattr(names_table, name_attribute)))
 order_columns.append(table.identifier)
 query = query.order_by(coalesce(*order_columns))
 return query

def order_by_name ( query , table , language = None , * extra_languages , ** kwargs ) : name_attribute = kwargs . pop ( 'name' , 'name' ) if kwargs : raise ValueError ( ( 'Unexpected keyword arguments: %s' % kwargs . keys ( ) ) ) order_columns = [ ] if ( language is None ) : query = query . outerjoin ( table . names_local ) order_columns . append ( func . lower ( getattr ( table . names_table , name_attribute ) ) ) else : extra_languages = ( ( language , ) + extra_languages ) for language in extra_languages : names_table = aliased ( table . names_table ) query = query . outerjoin ( names_table ) query = query . filter ( ( names_table . foreign_id == table . id ) ) query = query . filter ( ( names_table . local_language_id == language . id ) ) order_columns . append ( func . lower ( getattr ( names_table , name_attribute ) ) ) order_columns . append ( table . identifier ) query = query . order_by ( coalesce ( * order_columns ) ) return query
order a query by name .