move CurrencyField to counter

This commit is contained in:
imperosol authored and Thomas Girod committed 2025-04-04 10:35:17 +02:00
1 parent 6dfd4e16e2
commit 002554b802
11 files changed
+55 -53

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
from decimal import Decimal
from django.conf import settings
from django.db import models
class CurrencyField(models.DecimalField):
"""Custom database field used for currency."""
def __init__(self, *args, **kwargs):
kwargs["max_digits"] = 12
kwargs["decimal_places"] = 2
super().__init__(*args, **kwargs)
def to_python(self, value):
if value is None:
return None
return super().to_python(value).quantize(Decimal("0.01"))
if settings.TESTING:
from model_bakery import baker
baker.generators.add(
CurrencyField,
lambda: baker.random_gen.gen_decimal(max_digits=8, decimal_places=2),
)
else: # pragma: no cover
# baker is only used in tests, so we don't need coverage for this part
pass