convert DecimalBlock value back to Decimal (#8694)

Fixes #4647
pull/8531/head
Yves Serrano 2022-06-16 14:05:48 +02:00 zatwierdzone przez Matt Westcott
rodzic f252aa83d1
commit c5c93cba67
4 zmienionych plików z 24 dodań i 0 usunięć

Wyświetl plik

@ -57,6 +57,7 @@ Changelog
* Fix: Prevent error on sending notifications for the legacy moderation process when no user was specified (Yves Serrano)
* Fix: Ensure `aria-label` is not set on locale selection dropdown within page chooser modal as it was a duplicate of the button contents (LB (Ben Johnston))
* Fix: Revise the `ModelAdmin` title column behaviour to only link to 'edit' if the user has the correct permissions, fallback to the 'inspect' view or a non-clickable title if needed (Stefan Hammer)
* Fix: Ensure that `DecimalBlock` preserves the `Decimal` type when retrieving from the database (Yves Serrano)
3.0.1 (16.06.2022)

Wyświetl plik

@ -70,6 +70,7 @@ When using a queryset to render a list of images, you can now use the ``prefetch
* Prevent error on sending notifications for the legacy moderation process when no user was specified (Yves Serrano)
* Ensure `aria-label` is not set on locale selection dropdown within page chooser modal as it was a duplicate of the button contents (LB (Ben Johnston))
* Revise the `ModelAdmin` title column behaviour to only link to 'edit' if the user has the correct permissions, fallback to the 'inspect' view or a non-clickable title if needed (Stefan Hammer)
* Ensure that `DecimalBlock` preserves the `Decimal` type when retrieving from the database (Yves Serrano)
## Upgrade considerations

Wyświetl plik

@ -1,4 +1,5 @@
import datetime
from decimal import Decimal
from django import forms
from django.db.models.fields import BLANK_CHOICE_DASH
@ -245,6 +246,9 @@ class DecimalBlock(FieldBlock):
)
super().__init__(*args, **kwargs)
def to_python(self, value):
return Decimal(value)
class Meta:
icon = "plus-inverse"

Wyświetl plik

@ -8,6 +8,7 @@ from decimal import Decimal
# non-standard import name for gettext_lazy, to prevent strings from being picked up for translation
from django import forms
from django.core.exceptions import ValidationError
from django.core.serializers.json import DjangoJSONEncoder
from django.forms.utils import ErrorList
from django.template.loader import render_to_string
from django.test import SimpleTestCase, TestCase
@ -411,6 +412,13 @@ class TestDecimalBlock(TestCase):
block_val = block.value_from_form(Decimal("1.63"))
self.assertEqual(type(block_val), Decimal)
def test_type_to_python(self):
block = blocks.DecimalBlock()
block_val = block.to_python(
"1.63"
) # decimals get saved as string in JSON field
self.assertEqual(type(block_val), Decimal)
def test_render(self):
block = blocks.DecimalBlock()
test_val = Decimal(1.63)
@ -446,6 +454,16 @@ class TestDecimalBlock(TestCase):
with self.assertRaises(ValidationError):
block.clean("3.0")
def test_round_trip_to_db_preserves_type(self):
block = blocks.DecimalBlock()
original_value = Decimal(1.63)
db_value = json.dumps(
block.get_prep_value(original_value), cls=DjangoJSONEncoder
)
restored_value = block.to_python(json.loads(db_value))
self.assertEqual(type(restored_value), Decimal)
self.assertEqual(original_value, restored_value)
class TestRegexBlock(TestCase):
def test_render(self):