Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #116 from giginet/fix-local-language-id
Browse files Browse the repository at this point in the history
  • Loading branch information
Naramsim authored Jul 22, 2022
2 parents 8e79415 + c9b9998 commit f9e4a16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 15 additions & 0 deletions pokedex/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re

from sqlalchemy import engine_from_config, orm
from sqlalchemy import event

from ..defaults import get_default_db_uri
from .tables import metadata
Expand Down Expand Up @@ -55,6 +56,20 @@ def connect(uri=None, session_args={}, engine_args={}, engine_prefix=''):
default_language_id=ENGLISH_ID, **all_session_args)
session = MultilangScopedSession(sm)

@event.listens_for(session, "do_orm_execute")
def bind_default_language_id(state):
# Set _default_language_id param if it hasn't been set by the time the query is executed.
# XXX This is really hacky and we should figure out a cleaner method.
if not state.statement.is_select:
return

if not state.parameters:
state.parameters = {}

if not '_default_language_id' in state.parameters or state.parameters['_default_language_id'] == 'dummy':
state.parameters.update(_default_language_id=state.session.default_language_id)

return state.invoke_statement()
return session

def identifier_from_name(name):
Expand Down
8 changes: 1 addition & 7 deletions pokedex/db/multilang.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,7 @@ def creator(language, value):
return Translations

class MultilangQuery(Query):
def _execute_and_instances(self, *args, **kwargs):
# Set _default_language_id param if it hasn't been set by the time the query is executed.
# XXX This is really hacky and we should figure out a cleaner method.
if '_default_language_id' not in self._params or self._params['_default_language_id'] == 'dummy':
self._params = self._params.copy()
self._params['_default_language_id'] = self.session.default_language_id
return super(MultilangQuery, self)._execute_and_instances(*args, **kwargs)
pass

class MultilangSession(Session):
"""A tiny Session subclass that adds support for a default language.
Expand Down

0 comments on commit f9e4a16

Please sign in to comment.