Merge pull request #12 from SectorLabs/experimental

Add experimental features flag for LocalizedField
This commit is contained in:
Swen Kooij 2017-03-24 15:13:55 +02:00 committed by GitHub
commit bf90131e4f
7 changed files with 39 additions and 11 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,13 +1,15 @@
from .util import get_language_codes
from .forms import LocalizedFieldForm, LocalizedFieldWidget from .forms import LocalizedFieldForm, LocalizedFieldWidget
from .fields import (LocalizedField, LocalizedAutoSlugField, from .fields import (LocalizedAutoSlugField, LocalizedField,
LocalizedUniqueSlugField) LocalizedUniqueSlugField)
from .mixins import AtomicSlugRetryMixin
from .localized_value import LocalizedValue from .localized_value import LocalizedValue
from .mixins import AtomicSlugRetryMixin
from .models import LocalizedModel
from .util import get_language_codes
__all__ = [ __all__ = [
'get_language_codes', 'get_language_codes',
'LocalizedField', 'LocalizedField',
'LocalizedModel',
'LocalizedValue', 'LocalizedValue',
'LocalizedAutoSlugField', 'LocalizedAutoSlugField',
'LocalizedUniqueSlugField', 'LocalizedUniqueSlugField',

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:
return LocalizedValue() if getattr(settings, 'LOCALIZED_FIELDS_EXPERIMENTAL', False):
return None
else:
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

@ -7,7 +7,7 @@ with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
setup( setup(
name='django-localized-fields', name='django-localized-fields',
version='3.5', version='3.6',
packages=find_packages(), packages=find_packages(),
include_package_data=True, include_package_data=True,
license='MIT License', license='MIT License',

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,16 @@ 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
def test_from_db_value_none_return_none(self):
"""Tests whether the :see:from_db_value function
correctly handles None values when LOCALIZED_FIELDS_EXPERIMENTAL
is set to True."""
with self.settings(LOCALIZED_FIELDS_EXPERIMENTAL=True):
localized_value = LocalizedField.from_db_value(None)
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