mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-05-22 11:21:54 +00:00
Merge pull request #152 from wrwrwr/feature/fallbacks-toggle
Fallbacks toggle
This commit is contained in:
commit
c17f3a2338
5 changed files with 48 additions and 2 deletions
|
|
@ -246,6 +246,11 @@ What is more, fallback languages order can be overridden per model, using ``Tran
|
|||
|
||||
Dict syntax is only allowed there.
|
||||
|
||||
Even more, all fallbacks may be switched on or off for just some exceptional block of code using::
|
||||
|
||||
with fallbacks(False):
|
||||
# Work with values for the active language only
|
||||
|
||||
Fallback values
|
||||
***************
|
||||
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ class TranslationFieldDescriptor(object):
|
|||
# Here we check only for None and '', because e.g. 0 should not fall back.
|
||||
if val is not None and val != '':
|
||||
return val
|
||||
if self.fallback_value is None:
|
||||
if self.fallback_value is None or not mt_settings.ENABLE_FALLBACKS:
|
||||
return self.field.get_default()
|
||||
else:
|
||||
return self.fallback_value
|
||||
|
|
|
|||
|
|
@ -51,3 +51,4 @@ for key, value in FALLBACK_LANGUAGES.iteritems():
|
|||
if lang not in AVAILABLE_LANGUAGES:
|
||||
raise ImproperlyConfigured(
|
||||
'MODELTRANSLATION_FALLBACK_LANGUAGES: "%s" not in LANGUAGES setting.' % lang)
|
||||
ENABLE_FALLBACKS = getattr(settings, 'MODELTRANSLATION_ENABLE_FALLBACKS', True)
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ from modeltranslation.tests.translation import (FallbackModel2TranslationOptions
|
|||
FieldInheritanceETranslationOptions)
|
||||
from modeltranslation.tests.test_settings import TEST_SETTINGS
|
||||
from modeltranslation.utils import (build_css_class, build_localized_fieldname,
|
||||
auto_populate)
|
||||
auto_populate, fallbacks)
|
||||
|
||||
try:
|
||||
from django.test.utils import override_settings
|
||||
|
|
@ -512,6 +512,22 @@ class FallbackTests(ModeltranslationTestBase):
|
|||
n.title_en = None
|
||||
self.assertEqual(n.title, '') # if all fallbacks fail, return field.get_default()
|
||||
|
||||
def test_fallbacks_toggle(self):
|
||||
with reload_override_settings(MODELTRANSLATION_FALLBACK_LANGUAGES=self.test_fallback):
|
||||
m = models.TestModel(title='foo')
|
||||
with fallbacks(True):
|
||||
self.assertEqual(m.title_de, 'foo')
|
||||
self.assertEqual(m.title_en, None)
|
||||
self.assertEqual(m.title, 'foo')
|
||||
with override('en'):
|
||||
self.assertEqual(m.title, 'foo')
|
||||
with fallbacks(False):
|
||||
self.assertEqual(m.title_de, 'foo')
|
||||
self.assertEqual(m.title_en, None)
|
||||
self.assertEqual(m.title, 'foo')
|
||||
with override('en'):
|
||||
self.assertEqual(m.title, '') # '' is the default
|
||||
|
||||
|
||||
class FileFieldsTest(ModeltranslationTestBase):
|
||||
|
||||
|
|
|
|||
|
|
@ -90,6 +90,8 @@ def resolution_order(lang, override=None):
|
|||
First is always the parameter language, later are fallback languages.
|
||||
Override parameter has priority over FALLBACK_LANGUAGES.
|
||||
"""
|
||||
if not settings.ENABLE_FALLBACKS:
|
||||
return (lang,)
|
||||
if override is None:
|
||||
override = {}
|
||||
fallback_for_lang = override.get(lang, settings.FALLBACK_LANGUAGES.get(lang, ()))
|
||||
|
|
@ -123,3 +125,25 @@ def auto_populate(mode='all'):
|
|||
yield
|
||||
finally:
|
||||
settings.AUTO_POPULATE = current_population_mode
|
||||
|
||||
|
||||
@contextmanager
|
||||
def fallbacks(enable=True):
|
||||
"""
|
||||
Temporarily switch all language fallbacks on or off.
|
||||
|
||||
Example:
|
||||
|
||||
with fallbacks(False):
|
||||
lang_has_slug = bool(self.slug)
|
||||
|
||||
May be used to enable fallbacks just when they're needed saving on some
|
||||
processing or check if there is a value for the current language (not
|
||||
knowing the language)
|
||||
"""
|
||||
current_enable_fallbacks = settings.ENABLE_FALLBACKS
|
||||
settings.ENABLE_FALLBACKS = enable
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
settings.ENABLE_FALLBACKS = current_enable_fallbacks
|
||||
|
|
|
|||
Loading…
Reference in a new issue