Change translation hybrid expr
Change translation hybrid expression to return the current translation if it exists and otherwise fallback to default translation.
This commit is contained in:
@@ -4,6 +4,12 @@ Changelog
|
|||||||
Here you can see the full list of changes between each SQLAlchemy-Utils release.
|
Here you can see the full list of changes between each SQLAlchemy-Utils release.
|
||||||
|
|
||||||
|
|
||||||
|
0.30.1 (2015-05-xx)
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
- Make translation_hybrid expression work the same way as SQLAlchemy-i18n translation expressions
|
||||||
|
|
||||||
|
|
||||||
0.30.0 (2015-04-15)
|
0.30.0 (2015-04-15)
|
||||||
^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
import sqlalchemy as sa
|
||||||
from sqlalchemy.ext.hybrid import hybrid_property
|
from sqlalchemy.ext.hybrid import hybrid_property
|
||||||
|
|
||||||
from .exceptions import ImproperlyConfigured
|
from .exceptions import ImproperlyConfigured
|
||||||
@@ -71,7 +72,11 @@ class TranslationHybrid(object):
|
|||||||
return setter
|
return setter
|
||||||
|
|
||||||
def expr_factory(self, attr):
|
def expr_factory(self, attr):
|
||||||
return lambda cls: attr
|
def expr(cls):
|
||||||
|
current_locale = self.cast_locale(cls, self.current_locale)
|
||||||
|
default_locale = self.cast_locale(cls, self.default_locale)
|
||||||
|
return sa.func.coalesce(attr[current_locale], attr[default_locale])
|
||||||
|
return expr
|
||||||
|
|
||||||
def __call__(self, attr):
|
def __call__(self, attr):
|
||||||
return hybrid_property(
|
return hybrid_property(
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from pytest import mark
|
||||||
from sqlalchemy.dialects.postgresql import HSTORE
|
from sqlalchemy.dialects.postgresql import HSTORE
|
||||||
|
|
||||||
from sqlalchemy_utils import TranslationHybrid
|
from sqlalchemy_utils import TranslationHybrid
|
||||||
@@ -26,9 +27,6 @@ class TestTranslationHybrid(TestCase):
|
|||||||
city = self.City(name='Helsinki')
|
city = self.City(name='Helsinki')
|
||||||
assert city.name_translations['fi'] == 'Helsinki'
|
assert city.name_translations['fi'] == 'Helsinki'
|
||||||
|
|
||||||
def test_hybrid_as_expression(self):
|
|
||||||
assert self.City.name == self.City.name_translations
|
|
||||||
|
|
||||||
def test_if_no_translation_exists_returns_none(self):
|
def test_if_no_translation_exists_returns_none(self):
|
||||||
city = self.City()
|
city = self.City()
|
||||||
assert city.name is None
|
assert city.name is None
|
||||||
@@ -52,11 +50,18 @@ class TestTranslationHybrid(TestCase):
|
|||||||
|
|
||||||
assert city.name == 'Helsinki'
|
assert city.name == 'Helsinki'
|
||||||
|
|
||||||
def test_hybrid_as_an_expression(self):
|
@mark.parametrize(
|
||||||
city = self.City(name_translations={'en': 'Helsinki'})
|
('name_translations', 'name'),
|
||||||
|
(
|
||||||
|
({'fi': 'Helsinki', 'en': 'Helsing'}, 'Helsinki'),
|
||||||
|
({'en': 'Helsinki'}, 'Helsinki'),
|
||||||
|
({'fi': 'Helsinki'}, 'Helsinki'),
|
||||||
|
({}, None),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
def test_hybrid_as_an_expression(self, name_translations, name):
|
||||||
|
city = self.City(name_translations=name_translations)
|
||||||
self.session.add(city)
|
self.session.add(city)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
|
|
||||||
assert city == self.session.query(self.City).filter(
|
assert self.session.query(self.City.name).scalar() == name
|
||||||
self.City.name['en'] == 'Helsinki'
|
|
||||||
).first()
|
|
||||||
|
Reference in New Issue
Block a user