Skip to content

Commit

Permalink
Rename VatPercentageField to NormalizedDecimalField
Browse files Browse the repository at this point in the history
  • Loading branch information
ssinger committed Nov 15, 2024
1 parent 528cf4a commit 89349f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
),
]
15 changes: 2 additions & 13 deletions postgresqleu/invoices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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')

Expand Down
10 changes: 10 additions & 0 deletions postgresqleu/util/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 89349f4

Please sign in to comment.