mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
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.
This commit is contained in:
parent
247f7ead43
commit
3c17720a1c
1 changed files with 7 additions and 3 deletions
|
@ -5,6 +5,8 @@ of pokemon, and filtering/ordering by name.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
|
from sqlalchemy.sql.expression import func
|
||||||
|
from sqlalchemy.sql.functions import coalesce
|
||||||
|
|
||||||
from pokedex.db import tables
|
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.
|
Uses the identifier as a fallback ordering.
|
||||||
"""
|
"""
|
||||||
|
order_columns = []
|
||||||
if language is None:
|
if language is None:
|
||||||
query = query.outerjoin(table.names_local)
|
query = query.outerjoin(table.names_local)
|
||||||
query = query.order_by(table.names_table.name)
|
order_columns.append(func.lower(table.names_table.name))
|
||||||
else:
|
else:
|
||||||
extra_languages = (language, ) + extra_languages
|
extra_languages = (language, ) + extra_languages
|
||||||
for language in 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.outerjoin(names_table)
|
||||||
query = query.filter(names_table.foreign_id == table.id)
|
query = query.filter(names_table.foreign_id == table.id)
|
||||||
query = query.filter(names_table.local_language_id == language.id)
|
query = query.filter(names_table.local_language_id == language.id)
|
||||||
query = query.order_by(names_table.name)
|
order_columns.append(func.lower(names_table.name))
|
||||||
query = query.order_by(table.identifier)
|
order_columns.append(table.identifier)
|
||||||
|
query = query.order_by(coalesce(*order_columns))
|
||||||
return query
|
return query
|
||||||
|
|
Loading…
Reference in a new issue