mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Fix default language assignment once and for all.
Stop trying to be clever and magical and just make the caller pass in a damn primary key.
This commit is contained in:
parent
f666327fa0
commit
08bfd753e0
3 changed files with 28 additions and 38 deletions
|
@ -7,6 +7,8 @@ from ..defaults import get_default_db_uri
|
||||||
from .tables import Language, metadata
|
from .tables import Language, metadata
|
||||||
from .multilang import MultilangSession, MultilangScopedSession
|
from .multilang import MultilangSession, MultilangScopedSession
|
||||||
|
|
||||||
|
ENGLISH_ID = 9
|
||||||
|
|
||||||
|
|
||||||
def connect(uri=None, session_args={}, engine_args={}, engine_prefix=''):
|
def connect(uri=None, session_args={}, engine_args={}, engine_prefix=''):
|
||||||
"""Connects to the requested URI. Returns a session object.
|
"""Connects to the requested URI. Returns a session object.
|
||||||
|
@ -44,15 +46,10 @@ def connect(uri=None, session_args={}, engine_args={}, engine_prefix=''):
|
||||||
|
|
||||||
all_session_args = dict(autoflush=True, autocommit=False, bind=engine)
|
all_session_args = dict(autoflush=True, autocommit=False, bind=engine)
|
||||||
all_session_args.update(session_args)
|
all_session_args.update(session_args)
|
||||||
sm = orm.sessionmaker(class_=MultilangSession, language_class=Language,
|
sm = orm.sessionmaker(class_=MultilangSession,
|
||||||
**all_session_args)
|
default_language_id=ENGLISH_ID, **all_session_args)
|
||||||
session = MultilangScopedSession(sm)
|
session = MultilangScopedSession(sm)
|
||||||
|
|
||||||
# Default to English. Warning, magic constant, messing with internals,
|
|
||||||
# blah blah. Trying to fetch English here would kinda break on new
|
|
||||||
# databases. TODO still not an ideal solution, I guess.
|
|
||||||
session.registry()._default_language_id = 9
|
|
||||||
|
|
||||||
return session
|
return session
|
||||||
|
|
||||||
def identifier_from_name(name):
|
def identifier_from_name(name):
|
||||||
|
|
|
@ -157,46 +157,36 @@ def create_translation_table(_table_name, foreign_class, relation_name,
|
||||||
class MultilangSession(Session):
|
class MultilangSession(Session):
|
||||||
"""A tiny Session subclass that adds support for a default language.
|
"""A tiny Session subclass that adds support for a default language.
|
||||||
|
|
||||||
Caller will need to assign something to `default_language` before this will
|
Needs to be used with `MultilangScopedSession`, below.
|
||||||
actually work.
|
|
||||||
"""
|
"""
|
||||||
_default_language_id = 0 # Better fill this in, caller
|
default_language_id = None
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.language_class = kwargs.pop('language_class')
|
if 'default_language_id' in kwargs:
|
||||||
|
self.default_language_id = kwargs.pop('default_language_id')
|
||||||
|
|
||||||
super(MultilangSession, self).__init__(*args, **kwargs)
|
super(MultilangSession, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
@property
|
|
||||||
def default_language(self):
|
|
||||||
return self.query(self.language_class) \
|
|
||||||
.filter_by(id=self._default_language_id) \
|
|
||||||
.one()
|
|
||||||
|
|
||||||
@default_language.setter
|
|
||||||
def default_language(self, new):
|
|
||||||
self._default_language_id = new.id
|
|
||||||
|
|
||||||
@default_language.deleter
|
|
||||||
def default_language(self):
|
|
||||||
try:
|
|
||||||
del self._default_language_id
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def execute(self, clause, params=None, *args, **kwargs):
|
def execute(self, clause, params=None, *args, **kwargs):
|
||||||
if not params:
|
if not params:
|
||||||
params = {}
|
params = {}
|
||||||
params.setdefault('_default_language_id', self._default_language_id)
|
params.setdefault('_default_language_id', self.default_language_id)
|
||||||
|
|
||||||
return super(MultilangSession, self).execute(
|
return super(MultilangSession, self).execute(
|
||||||
clause, params, *args, **kwargs)
|
clause, params, *args, **kwargs)
|
||||||
|
|
||||||
class MultilangScopedSession(ScopedSession):
|
class MultilangScopedSession(ScopedSession):
|
||||||
"""Dispatches language selection to the attached Session."""
|
"""Dispatches language selection to the attached Session."""
|
||||||
|
|
||||||
@property
|
def __init__(self, *args, **kwargs):
|
||||||
def default_language(self):
|
super(MultilangScopedSession, self).__init__(*args, **kwargs)
|
||||||
return self.registry().default_language
|
|
||||||
|
|
||||||
@default_language.setter
|
@property
|
||||||
def default_language(self, new):
|
def default_language_id(self):
|
||||||
self.registry().default_language = new
|
"""Passes the new default language id through to the current session.
|
||||||
|
"""
|
||||||
|
return self.registry().default_language_id
|
||||||
|
|
||||||
|
@default_language_id.setter
|
||||||
|
def default_language_id(self, new):
|
||||||
|
self.registry().default_language_id = new
|
||||||
|
|
|
@ -66,7 +66,7 @@ def test_i18n_table_creation():
|
||||||
|
|
||||||
# OK, create all the tables and gimme a session
|
# OK, create all the tables and gimme a session
|
||||||
Base.metadata.create_all()
|
Base.metadata.create_all()
|
||||||
sm = sessionmaker(class_=MultilangSession, language_class=Language)
|
sm = sessionmaker(class_=MultilangSession)
|
||||||
sess = MultilangScopedSession(sm)
|
sess = MultilangScopedSession(sm)
|
||||||
|
|
||||||
# Create some languages and foos to bind together
|
# Create some languages and foos to bind together
|
||||||
|
@ -80,9 +80,12 @@ def test_i18n_table_creation():
|
||||||
foo = Foo()
|
foo = Foo()
|
||||||
sess.add(foo)
|
sess.add(foo)
|
||||||
|
|
||||||
# Commit so the above get primary keys filled in
|
# Commit so the above get primary keys filled in, then give the
|
||||||
|
# session the language id
|
||||||
sess.commit()
|
sess.commit()
|
||||||
sess.default_language = lang_en
|
# Note that this won't apply to sessions created in other threads, but that
|
||||||
|
# ought not be a problem!
|
||||||
|
sess.default_language_id = lang_en.id
|
||||||
|
|
||||||
# Give our foo some names, as directly as possible
|
# Give our foo some names, as directly as possible
|
||||||
foo_text = FooText()
|
foo_text = FooText()
|
||||||
|
|
Loading…
Reference in a new issue