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

Fix failing to bind default_language_id #116

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is private. So it should not be overridden.

# 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