mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Remove the responsibility of setting a default language from multilang.
Caller now has to do it. No need to avoid circular deps, no need to do much of anything at all.
This commit is contained in:
parent
4291c33c00
commit
9441ffb165
2 changed files with 19 additions and 17 deletions
|
@ -1,7 +1,7 @@
|
||||||
from sqlalchemy import MetaData, Table, engine_from_config, orm
|
from sqlalchemy import engine_from_config, orm
|
||||||
|
|
||||||
from ..defaults import get_default_db_uri
|
from ..defaults import get_default_db_uri
|
||||||
from .tables import metadata
|
from .tables import Language, metadata
|
||||||
from .multilang import MultilangSession, MultilangScopedSession
|
from .multilang import MultilangSession, MultilangScopedSession
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +41,13 @@ 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, **all_session_args)
|
sm = orm.sessionmaker(class_=MultilangSession, language_class=Language,
|
||||||
|
**all_session_args)
|
||||||
session = MultilangScopedSession(sm)
|
session = MultilangScopedSession(sm)
|
||||||
|
|
||||||
|
# Default to English
|
||||||
|
session.default_language = session.query(Language) \
|
||||||
|
.filter_by(identifier=u'en') \
|
||||||
|
.one()
|
||||||
|
|
||||||
return session
|
return session
|
||||||
|
|
|
@ -154,26 +154,22 @@ def create_translation_table(_table_name, foreign_class, relation_name,
|
||||||
return Translations
|
return Translations
|
||||||
|
|
||||||
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.
|
||||||
_default_language_id = 9 # English. XXX magic constant
|
|
||||||
|
Caller will need to assign something to `default_language` before this will
|
||||||
|
actually work.
|
||||||
|
"""
|
||||||
|
_default_language_id = 0 # Better fill this in, caller
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
try:
|
self.language_class = kwargs.pop('language_class')
|
||||||
self.language_class = kwargs.pop('language_class')
|
|
||||||
except KeyError:
|
|
||||||
# Set the default language_class
|
|
||||||
# We need to import here, to prevent a circular depencency
|
|
||||||
from pokedex.db.tables import Language
|
|
||||||
self.language_class = Language
|
|
||||||
super(MultilangSession, self).__init__(*args, **kwargs)
|
super(MultilangSession, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def default_language(self):
|
def default_language(self):
|
||||||
# Need to import tables here to avoid a circular dependency
|
return self.query(self.language_class) \
|
||||||
from pokedex.db import tables
|
.filter_by(id=self._default_language_id) \
|
||||||
query = self.query(self.language_class)
|
.one()
|
||||||
query = query.filter_by(id=self._default_language_id)
|
|
||||||
return query.one()
|
|
||||||
|
|
||||||
@default_language.setter
|
@default_language.setter
|
||||||
def default_language(self, new):
|
def default_language(self, new):
|
||||||
|
|
Loading…
Reference in a new issue