Add experimental features flag for LocalizedField

With the flag set, LocalizedField will return None if there is no
database value.
This commit is contained in:
Bogdan Hopulele 2017-03-23 17:32:26 +02:00
parent 5df44b0d62
commit 52145ca7d3
5 changed files with 35 additions and 7 deletions

View File

@ -244,6 +244,16 @@ Besides ``LocalizedField``, there's also:
title = LocalizedField() title = LocalizedField()
description = LocalizedBleachField() description = LocalizedBleachField()
Experimental feature
^^^^^^^^^^^^^^^^^^^^
Enables the following experimental features:
* ``LocalizedField`` will return ``None`` instead of an empty ``LocalizedValue`` if there is no database value.
.. code-block:: python
LOCALIZED_FIELDS_EXPERIMENTAL = True
Frequently asked questions (FAQ) Frequently asked questions (FAQ)
-------------------------------- --------------------------------

View File

@ -1,8 +1,8 @@
from django.conf import settings from django.conf import settings
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from psqlextra.fields import HStoreField
from localized_fields import LocalizedFieldForm from localized_fields import LocalizedFieldForm
from psqlextra.fields import HStoreField
from ..localized_value import LocalizedValue from ..localized_value import LocalizedValue
@ -36,11 +36,15 @@ class LocalizedField(HStoreField):
""" """
if not value: if not value:
if getattr(settings, 'LOCALIZED_FIELDS_EXPERIMENTAL', False):
return None
else:
return LocalizedValue() return LocalizedValue()
return LocalizedValue(value) return LocalizedValue(value)
def to_python(self, value: dict) -> LocalizedValue: @staticmethod
def to_python(value: dict) -> LocalizedValue:
"""Turns the specified database value into its Python """Turns the specified database value into its Python
equivalent. equivalent.

View File

@ -15,7 +15,7 @@ class LocalizedModel(PostgresModel):
Here we set all the fields that are of :see:LocalizedField Here we set all the fields that are of :see:LocalizedField
to an instance of :see:LocalizedValue in case they are none to an instance of :see:LocalizedValue in case they are none
so that the user doesn't explicitely have to do so.""" so that the user doesn't explicitly have to do so."""
super(LocalizedModel, self).__init__(*args, **kwargs) super(LocalizedModel, self).__init__(*args, **kwargs)

View File

@ -27,3 +27,5 @@ INSTALLED_APPS = (
# set to a lower number than the default, since # set to a lower number than the default, since
# we want the tests to be fast, default is 100 # we want the tests to be fast, default is 100
LOCALIZED_FIELDS_MAX_RETRIES = 3 LOCALIZED_FIELDS_MAX_RETRIES = 3
LOCALIZED_FIELDS_EXPERIMENTAL = False

View File

@ -1,9 +1,9 @@
from django.conf import settings from django.conf import settings
from django.db.utils import IntegrityError
from django.test import TestCase from django.test import TestCase
from django.utils import translation from django.utils import translation
from django.db.utils import IntegrityError
from localized_fields import LocalizedField, LocalizedValue, LocalizedFieldForm from localized_fields import LocalizedField, LocalizedFieldForm, LocalizedValue
def get_init_values() -> dict: def get_init_values() -> dict:
@ -179,7 +179,7 @@ class LocalizedFieldTestCase(TestCase):
@staticmethod @staticmethod
def test_from_db_value_none(): def test_from_db_value_none():
"""Tests whether the :see:from_db_valuei function """Tests whether the :see:from_db_value function
correctly handles None values.""" correctly handles None values."""
localized_value = LocalizedField.from_db_value(None) localized_value = LocalizedField.from_db_value(None)
@ -187,6 +187,18 @@ class LocalizedFieldTestCase(TestCase):
for lang_code, _ in settings.LANGUAGES: for lang_code, _ in settings.LANGUAGES:
assert localized_value.get(lang_code) is None assert localized_value.get(lang_code) is None
@staticmethod
def test_from_db_value_none_return_none():
"""Tests whether the :see:from_db_value function
correctly handles None values when LOCALIZED_FIELDS_EXPERIMENTAL
is set to True."""
settings.LOCALIZED_FIELDS_EXPERIMENTAL = True
localized_value = LocalizedField.from_db_value(None)
settings.LOCALIZED_FIELDS_EXPERIMENTAL = False
assert localized_value is None
@staticmethod @staticmethod
def test_to_python(): def test_to_python():
"""Tests whether the :see:to_python function """Tests whether the :see:to_python function