Skip to content

Commit

Permalink
Fix validation of djmoney.forms.fields.MoneyField (#442)
Browse files Browse the repository at this point in the history
* Fix validation of ``djmoney.forms.fields.MoneyField`` when ``disabled=True`` is passed to it. Fixes #439

* Fix for Django 1.8
  • Loading branch information
Stranger6667 committed Jul 23, 2018
1 parent 02d3cfa commit c411976
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
9 changes: 9 additions & 0 deletions djmoney/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.forms import ChoiceField, DecimalField, MultiValueField

from djmoney.money import Money
from djmoney.utils import MONEY_CLASSES

from ..settings import CURRENCY_CHOICES
from .widgets import MoneyWidget
Expand Down Expand Up @@ -51,7 +52,15 @@ def compress(self, data_list):
return Money(*data_list[:2])
return None

def clean(self, value):
if isinstance(value, MONEY_CLASSES):
value = (value.amount, value.currency)
return super(MoneyField, self).clean(value)

def has_changed(self, initial, data): # noqa
# Django 1.8 has no 'disabled' attribute
if hasattr(self, 'disabled') and self.disabled:
return False
if initial is None:
initial = ['' for _ in range(0, len(data))]
else:
Expand Down
10 changes: 10 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
Changelog
=========

Unreleased
----------

Fixed
~~~~

- Validation of ``djmoney.forms.fields.MoneyField`` when ``disabled=True`` is passed to it. `#439`_ (`stinovlas`_, `Stranger6667`_)

`0.14.1`_ - 2018-07-17
--------------------

Expand Down Expand Up @@ -571,6 +579,7 @@ Added
.. _0.3: https://github.com/django-money/django-money/compare/0.2...0.3
.. _0.2: https://github.com/django-money/django-money/compare/0.2...a6d90348085332a393abb40b86b5dd9505489b04

.. _#439: https://github.com/django-money/django-money/issues/439
.. _#427: https://github.com/django-money/django-money/pull/427
.. _#425: https://github.com/django-money/django-money/issues/425
.. _#412: https://github.com/django-money/django-money/issues/412
Expand Down Expand Up @@ -699,6 +708,7 @@ Added
.. _sjdines: https://github.com/sjdines
.. _snbuchholz: https://github.com/snbuchholz
.. _spookylukey: https://github.com/spookylukey
.. _stinovlas: https://github.com/stinovlas
.. _synotna: https://github.com/synotna
.. _toudi: https://github.com/toudi
.. _tsouvarev: https://github.com/tsouvarev
Expand Down
15 changes: 15 additions & 0 deletions tests/test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .testapp.forms import (
DefaultMoneyModelForm,
DisabledFieldForm,
MoneyForm,
MoneyFormMultipleCurrencies,
MoneyModelForm,
Expand Down Expand Up @@ -190,3 +191,17 @@ def test_default_django_validator(self):
form = MoneyModelFormWithValidation(data={'balance_0': 0, 'balance_1': 'GBP'})
assert not form.is_valid()
assert form.errors == {'balance': ['Ensure this value is greater than or equal to 100.00 GBP.']}


@pytest.mark.skipif(VERSION[:2] == (1, 8), reason="Django 1.8 doesn't have `disabled` keyword in fields")
class TestDisabledField:

def test_validation(self):
instance = ModelWithVanillaMoneyField.objects.create(money=Money('42.00', 'USD'))
form = DisabledFieldForm(data={}, instance=instance)
assert not form.errors
assert form.is_valid()

def test_has_changed(self):
form = DisabledFieldForm(data={})
assert not form.has_changed()
12 changes: 11 additions & 1 deletion tests/testapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
from __future__ import unicode_literals

from django import forms
from django import VERSION, forms

from djmoney.forms import MoneyField

Expand All @@ -16,6 +16,7 @@
ModelWithVanillaMoneyField,
NullMoneyFieldModel,
PositiveValidatedMoneyModel,
SimpleModel,
ValidatedMoneyModel,
)

Expand Down Expand Up @@ -72,3 +73,12 @@ class PositiveValidatedMoneyModelForm(forms.ModelForm):
class Meta:
model = PositiveValidatedMoneyModel
fields = ('money', )


class DisabledFieldForm(forms.ModelForm):
if VERSION[:2] != (1, 8):
money = MoneyField(disabled=True)

class Meta:
fields = '__all__'
model = SimpleModel

0 comments on commit c411976

Please sign in to comment.