From 3c17720a1c6e0e01513aec1c69e9823ee3a335bb Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 19 Apr 2011 13:00:41 +0300 Subject: [PATCH] order_by_name: properly fall back to the specified languages, or identifier Sometimes, translations are incomplete. Handle this gracefully by allowing fallback languages. If there are none, fall back to the identifier to get at least some order. --- pokedex/db/util.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pokedex/db/util.py b/pokedex/db/util.py index 9131654..f4f151a 100644 --- a/pokedex/db/util.py +++ b/pokedex/db/util.py @@ -5,6 +5,8 @@ of pokemon, and filtering/ordering by name. """ from sqlalchemy.orm import aliased +from sqlalchemy.sql.expression import func +from sqlalchemy.sql.functions import coalesce from pokedex.db import tables @@ -108,9 +110,10 @@ def order_by_name(query, table, language=None, *extra_languages): Uses the identifier as a fallback ordering. """ + order_columns = [] if language is None: query = query.outerjoin(table.names_local) - query = query.order_by(table.names_table.name) + order_columns.append(func.lower(table.names_table.name)) else: extra_languages = (language, ) + extra_languages for language in extra_languages: @@ -118,6 +121,7 @@ def order_by_name(query, table, language=None, *extra_languages): query = query.outerjoin(names_table) query = query.filter(names_table.foreign_id == table.id) query = query.filter(names_table.local_language_id == language.id) - query = query.order_by(names_table.name) - query = query.order_by(table.identifier) + order_columns.append(func.lower(names_table.name)) + order_columns.append(table.identifier) + query = query.order_by(coalesce(*order_columns)) return query