From: Petr Viktorin Date: Mon, 4 Apr 2011 18:51:12 +0000 (+0300) Subject: Make MultilangSession's language class configurable X-Git-Tag: veekun-promotions/2011041101~8^2~1 X-Git-Url: http://git.veekun.com/zzz-pokedex.git/commitdiff_plain/40f283a7a673d7e52cd13c29ebecea8a4e370ed4?hp=06545f5b48416ba0507d26499db2d57d6d69e13b Make MultilangSession's language class configurable --- diff --git a/pokedex/db/multilang.py b/pokedex/db/multilang.py index b5f87e4..6690b8a 100644 --- a/pokedex/db/multilang.py +++ b/pokedex/db/multilang.py @@ -157,11 +157,21 @@ class MultilangSession(Session): """A tiny Session subclass that adds support for a default language.""" _default_language_id = 9 # English. XXX magic constant + def __init__(self, *args, **kwargs): + try: + 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) + @property def default_language(self): # Need to import tables here to avoid a circular dependency from pokedex.db import tables - query = self.query(tables.Language) + query = self.query(self.language_class) query = query.filter_by(id=self._default_language_id) return query.one() diff --git a/pokedex/tests/test_schema.py b/pokedex/tests/test_schema.py index 2ad795c..bd42502 100644 --- a/pokedex/tests/test_schema.py +++ b/pokedex/tests/test_schema.py @@ -66,7 +66,7 @@ def test_i18n_table_creation(): # OK, create all the tables and gimme a session Base.metadata.create_all() - sm = sessionmaker(class_=MultilangSession) + sm = sessionmaker(class_=MultilangSession, language_class=Language) sess = MultilangScopedSession(sm) # Create some languages and foos to bind together @@ -82,7 +82,7 @@ def test_i18n_table_creation(): # Commit so the above get primary keys filled in sess.commit() - sess.default_language = lang_en.id + sess.default_language = lang_en # Give our foo some names, as directly as possible foo_text = FooText()