X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/1d9c3a0147d0645c8ba992cca937e83dcda3df69..81970e6c67c9bd670835f76bb32a357916ab6c5b:/pokedex/db/multilang.py?ds=sidebyside diff --git a/pokedex/db/multilang.py b/pokedex/db/multilang.py index d58f6df..44e532c 100644 --- a/pokedex/db/multilang.py +++ b/pokedex/db/multilang.py @@ -99,8 +99,7 @@ def create_translation_table(_table_name, foreign_class, relation_name, 'foreign_id': synonym(foreign_key_name), 'local_language': relationship(language_class, primaryjoin=table.c.local_language_id == language_class.id, - innerjoin=True, - lazy='joined'), + innerjoin=True), }) # Add full-table relations to the original class @@ -157,46 +156,36 @@ def create_translation_table(_table_name, foreign_class, relation_name, class MultilangSession(Session): """A tiny Session subclass that adds support for a default language. - Caller will need to assign something to `default_language` before this will - actually work. + Needs to be used with `MultilangScopedSession`, below. """ - _default_language_id = 0 # Better fill this in, caller + default_language_id = None def __init__(self, *args, **kwargs): - self.language_class = kwargs.pop('language_class') - super(MultilangSession, self).__init__(*args, **kwargs) + if 'default_language_id' in kwargs: + self.default_language_id = kwargs.pop('default_language_id') - @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 + 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 + def __init__(self, *args, **kwargs): + super(MultilangScopedSession, self).__init__(*args, **kwargs) - @default_language.setter - def default_language(self, new): - self.registry().default_language = new + @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