Add customizable default value for TranslationHybrid
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.29.6 (2015-02-03)
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
- Added customizable TranslationHybrid default value
|
||||||
|
|
||||||
|
|
||||||
0.29.5 (2015-02-03)
|
0.29.5 (2015-02-03)
|
||||||
^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
@@ -90,4 +90,29 @@ Translation hybrids can also be used as expressions.
|
|||||||
|
|
||||||
session.query(Article).filter(Article.name['en'] == 'Some article')
|
session.query(Article).filter(Article.name['en'] == 'Some article')
|
||||||
|
|
||||||
|
|
||||||
|
By default if no value is found for either current or default locale the
|
||||||
|
translation hybrid returns `None`. You can customize this value with `default_value` parameter
|
||||||
|
of translation_hybrid. In the following example we make translation hybrid fallback to empty string instead of `None`.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
translation_hybrid = TranslationHybrid(
|
||||||
|
current_locale=get_locale,
|
||||||
|
default_locale='en',
|
||||||
|
default_value=''
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Article(Base):
|
||||||
|
__tablename__ = 'article'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
name_translations = Column(HSTORE)
|
||||||
|
|
||||||
|
name = translation_hybrid(name_translations, default)
|
||||||
|
|
||||||
|
|
||||||
|
Article().name # ''
|
||||||
|
|
||||||
.. _SQLAlchemy-i18n: https://github.com/kvesteri/sqlalchemy-i18n
|
.. _SQLAlchemy-i18n: https://github.com/kvesteri/sqlalchemy-i18n
|
||||||
|
@@ -87,7 +87,7 @@ from .types import (
|
|||||||
from .models import Timestamp
|
from .models import Timestamp
|
||||||
|
|
||||||
|
|
||||||
__version__ = '0.29.5'
|
__version__ = '0.29.6'
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
@@ -26,9 +26,10 @@ except ImportError:
|
|||||||
|
|
||||||
|
|
||||||
class TranslationHybrid(object):
|
class TranslationHybrid(object):
|
||||||
def __init__(self, current_locale, default_locale):
|
def __init__(self, current_locale, default_locale, default_value=None):
|
||||||
self.current_locale = current_locale
|
self.current_locale = current_locale
|
||||||
self.default_locale = default_locale
|
self.default_locale = default_locale
|
||||||
|
self.default_value = default_value
|
||||||
|
|
||||||
def cast_locale(self, obj, locale):
|
def cast_locale(self, obj, locale):
|
||||||
"""
|
"""
|
||||||
@@ -60,7 +61,7 @@ class TranslationHybrid(object):
|
|||||||
try:
|
try:
|
||||||
return getattr(obj, attr.key)[default_locale]
|
return getattr(obj, attr.key)[default_locale]
|
||||||
except (TypeError, KeyError):
|
except (TypeError, KeyError):
|
||||||
return None
|
return self.default_value
|
||||||
return getter
|
return getter
|
||||||
|
|
||||||
def setter_factory(self, attr):
|
def setter_factory(self, attr):
|
||||||
|
@@ -33,6 +33,11 @@ class TestTranslationHybrid(TestCase):
|
|||||||
city = self.City()
|
city = self.City()
|
||||||
assert city.name is None
|
assert city.name is None
|
||||||
|
|
||||||
|
def test_custom_default_value(self):
|
||||||
|
self.translation_hybrid.default_value = 'Some value'
|
||||||
|
city = self.City()
|
||||||
|
assert city.name is 'Some value'
|
||||||
|
|
||||||
def test_fall_back_to_default_translation(self):
|
def test_fall_back_to_default_translation(self):
|
||||||
city = self.City(name_translations={'en': 'Helsinki'})
|
city = self.City(name_translations={'en': 'Helsinki'})
|
||||||
self.translation_hybrid.current_locale = 'sv'
|
self.translation_hybrid.current_locale = 'sv'
|
||||||
|
Reference in New Issue
Block a user