mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-06-27 22:03:21 +03:00
Merge pull request #16 from MELScience/deserialization
Ability to deserialize string value
This commit is contained in:
commit
bf2995fd27
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Union
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db.utils import IntegrityError
|
from django.db.utils import IntegrityError
|
||||||
from django.utils import six, translation
|
from django.utils import six, translation
|
||||||
@ -114,7 +116,7 @@ class LocalizedField(HStoreField):
|
|||||||
|
|
||||||
return cls.attr_class(value)
|
return cls.attr_class(value)
|
||||||
|
|
||||||
def to_python(self, value: dict) -> LocalizedValue:
|
def to_python(self, value: Union[dict, str, None]) -> LocalizedValue:
|
||||||
"""Turns the specified database value into its Python
|
"""Turns the specified database value into its Python
|
||||||
equivalent.
|
equivalent.
|
||||||
|
|
||||||
@ -127,7 +129,8 @@ class LocalizedField(HStoreField):
|
|||||||
A :see:LocalizedValue instance containing the
|
A :see:LocalizedValue instance containing the
|
||||||
data extracted from the database.
|
data extracted from the database.
|
||||||
"""
|
"""
|
||||||
|
# make deserialization if need by parent method
|
||||||
|
value = super(LocalizedField, self).to_python(value)
|
||||||
if not value or not isinstance(value, dict):
|
if not value or not isinstance(value, dict):
|
||||||
return self.attr_class()
|
return self.attr_class()
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db.utils import IntegrityError
|
from django.db.utils import IntegrityError
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
@ -232,6 +233,20 @@ 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_to_python_str():
|
||||||
|
"""Tests whether the :see:to_python function produces
|
||||||
|
the expected :see:LocalizedValue when it is
|
||||||
|
passed serialized string value."""
|
||||||
|
|
||||||
|
serialized_str = json.dumps(get_init_values())
|
||||||
|
localized_value = LocalizedField().to_python(serialized_str)
|
||||||
|
assert isinstance(localized_value, LocalizedValue)
|
||||||
|
|
||||||
|
for language, value in get_init_values().items():
|
||||||
|
assert localized_value.get(language) == value
|
||||||
|
assert getattr(localized_value, language) == value
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def test_get_prep_value():
|
def test_get_prep_value():
|
||||||
""""Tests whether the :see:get_prep_value function
|
""""Tests whether the :see:get_prep_value function
|
||||||
|
Loading…
x
Reference in New Issue
Block a user