X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/40f283a7a673d7e52cd13c29ebecea8a4e370ed4..cfa8ca3c4c5718d724ec7a7a11d438af585a35e7:/pokedex/db/multilang.py diff --git a/pokedex/db/multilang.py b/pokedex/db/multilang.py index 6690b8a..f66ee20 100644 --- a/pokedex/db/multilang.py +++ b/pokedex/db/multilang.py @@ -48,7 +48,7 @@ def create_translation_table(_table_name, foreign_class, relation_name, are rows in the created tables. - `(relation_name)_local`, a relation to the row in the new table that matches the current default language. - - `(relation_name)_class`, the class created by this function. + - `(relation_name)_table`, the class created by this function. Note that these are distinct relations. Even though the former necessarily includes the latter, SQLAlchemy doesn't treat them as linked; loading one @@ -154,56 +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. + + Needs to be used with `MultilangScopedSession`, below. + """ + default_language_id = None 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) + if 'default_language_id' in kwargs: + self.default_language_id = kwargs.pop('default_language_id') - @property - def default_language(self): - # Need to import tables here to avoid a circular dependency - from pokedex.db import tables - query = self.query(self.language_class) - 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 + 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