From 89349f4d5f2bc35e4b81baad0c7e6f9df30caf12 Mon Sep 17 00:00:00 2001 From: Steve Singer Date: Fri, 15 Nov 2024 17:04:14 -0500 Subject: [PATCH] Rename VatPercentageField to NormalizedDecimalField --- .../migrations/0021_alter_vatrate_vatpercent.py | 2 +- postgresqleu/invoices/models.py | 15 ++------------- postgresqleu/util/fields.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/postgresqleu/invoices/migrations/0021_alter_vatrate_vatpercent.py b/postgresqleu/invoices/migrations/0021_alter_vatrate_vatpercent.py index c35f9a3f..47516c77 100644 --- a/postgresqleu/invoices/migrations/0021_alter_vatrate_vatpercent.py +++ b/postgresqleu/invoices/migrations/0021_alter_vatrate_vatpercent.py @@ -15,6 +15,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='vatrate', name='vatpercent', - field=postgresqleu.invoices.models.VatPercentage(decimal_places=6, default=0, max_digits=9, validators=[django.core.validators.MaxValueValidator(100), django.core.validators.MinValueValidator(0)], verbose_name='VAT percentage'), + field=postgresqleu.util.fields.NormalizedDecimalField(decimal_places=6, default=0, max_digits=9, validators=[django.core.validators.MaxValueValidator(100), django.core.validators.MinValueValidator(0)], verbose_name='VAT percentage'), ), ] diff --git a/postgresqleu/invoices/models.py b/postgresqleu/invoices/models.py index 933964ff..b1fd3c29 100644 --- a/postgresqleu/invoices/models.py +++ b/postgresqleu/invoices/models.py @@ -12,7 +12,7 @@ from postgresqleu.util.validators import ListOfEmailAddressValidator from postgresqleu.util.checksum import luhn -from postgresqleu.util.fields import LowercaseEmailField +from postgresqleu.util.fields import LowercaseEmailField, NormalizedDecimalField from postgresqleu.accounting.models import Account, JournalEntry @@ -235,21 +235,10 @@ class Meta: ordering = ('-id', ) -class VatPercentage(models.DecimalField): - def __str__(self): - return str(self.normalize()) - - def from_db_value(self, value, expression, connection): - return value.normalize() - - def to_python(self, value): - return super().to_python(value).normalize() - - class VatRate(models.Model): name = models.CharField(max_length=100, blank=False, null=False) shortname = models.CharField(max_length=16, blank=False, null=False, verbose_name="Short name") - vatpercent = VatPercentage(null=False, default=0, verbose_name="VAT percentage", max_digits=9, decimal_places=6, validators=[MaxValueValidator(100), MinValueValidator(0)]) + vatpercent = NormalizedDecimalField(null=False, default=0, verbose_name="VAT percentage", max_digits=9, decimal_places=6, validators=[MaxValueValidator(100), MinValueValidator(0)]) vataccount = models.ForeignKey(Account, null=False, blank=False, on_delete=models.CASCADE, verbose_name="VAT account") _safe_attributes = ('vatpercent', 'shortstr', 'shortname', 'name') diff --git a/postgresqleu/util/fields.py b/postgresqleu/util/fields.py index 061c5494..05553193 100644 --- a/postgresqleu/util/fields.py +++ b/postgresqleu/util/fields.py @@ -117,3 +117,13 @@ def to_python(self, value): class UserModelChoiceField(ModelChoiceField): def label_from_instance(self, obj): return "{0} - {1} {2} <{3}>".format(obj.username, obj.first_name, obj.last_name, obj.email) + +class NormalizedDecimalField(models.DecimalField): + def __str__(self): + return str(self.normalize()) + + def from_db_value(self, value, expression, connection): + return value.normalize() + + def to_python(self, value): + return super().to_python(value).normalize()