diff --git a/manuels/migrations/0023_auto_20180604_1827.py b/manuels/migrations/0023_auto_20180604_1827.py new file mode 100644 index 0000000..3b4f141 --- /dev/null +++ b/manuels/migrations/0023_auto_20180604_1827.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.5 on 2018-06-04 16:27 + +from django.db import migrations, models +import manuels.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('manuels', '0022_teacher_has_confirmed_list'), + ] + + operations = [ + migrations.AlterField( + model_name='book', + name='price', + field=models.FloatField(validators=[manuels.models.positive_float_validator], verbose_name='prix'), + ), + ] diff --git a/manuels/models.py b/manuels/models.py index 7b2acb7..c5380d8 100644 --- a/manuels/models.py +++ b/manuels/models.py @@ -131,6 +131,17 @@ def isbn_validator(value): raise ValidationError("%(value)s n'est pas un ISBN valide.", params={'value': value}) +def positive_float_validator(value): + try: + value = float(value) + except ValueError: + raise ValidationError("%(value)s doit être un nombre décimal") + + if value < 0: + raise ValidationError("%(value)s doit être un nombre positif") + + + class Book(BaseModel): class Meta: verbose_name = 'livre' @@ -150,7 +161,7 @@ class Book(BaseModel): "suivis de la lettre X", validators=[isbn_validator] ) - price = models.PositiveIntegerField('prix') + price = models.FloatField('prix', validators=[positive_float_validator]) YES_NO_CHOICE = ( (False, 'Non'), (True, 'Oui'),