X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/06545f5b48416ba0507d26499db2d57d6d69e13b..899f00e2a7f1bb703fa726be8e7159a6b12fb33e:/pokedex/db/multilang.py?ds=sidebyside diff --git a/pokedex/db/multilang.py b/pokedex/db/multilang.py index b5f87e4..44e532c 100644 --- a/pokedex/db/multilang.py +++ b/pokedex/db/multilang.py @@ -154,46 +154,38 @@ def create_translation_table(_table_name, foreign_class, relation_name, return Translations class MultilangSession(Session): - """A tiny Session subclass that adds support for a default language.""" - _default_language_id = 9 # English. XXX magic constant + """A tiny Session subclass that adds support for a default language. - @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 = query.filter_by(id=self._default_language_id) - return query.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 + Needs to be used with `MultilangScopedSession`, below. + """ + default_language_id = None + + def __init__(self, *args, **kwargs): + if 'default_language_id' in kwargs: + self.default_language_id = kwargs.pop('default_language_id') + + super(MultilangSession, self).__init__(*args, **kwargs) def execute(self, clause, params=None, *args, **kwargs): if not 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( clause, params, *args, **kwargs) class MultilangScopedSession(ScopedSession): """Dispatches language selection to the attached Session.""" - @property - def default_language(self): - return self.registry().default_language - - @default_language.setter - def default_language(self, new): - self.registry().default_language = new + def __init__(self, *args, **kwargs): + super(MultilangScopedSession, self).__init__(*args, **kwargs) - def remove(self): - del self.registry().default_language - super(MultilangScopedSession, self).remove() + @property + def default_language_id(self): + """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