diff --git a/fournée/core/admin.py b/fournée/core/admin.py index 958758f..7691c67 100644 --- a/fournée/core/admin.py +++ b/fournée/core/admin.py @@ -1,5 +1,4 @@ from datetime import date, timedelta -from decimal import Decimal from django.contrib import admin, messages from django.shortcuts import redirect @@ -9,13 +8,7 @@ from django.utils.safestring import mark_safe import nested_admin from . import models - - -def clean(d): - if d == d.to_integral(): - return d.quantize(Decimal(1)) - else: - return d.quantize(Decimal('1.000')).normalize() +from .utils import clean class MyAdminSite(admin.AdminSite): diff --git a/fournée/core/models.py b/fournée/core/models.py index a3ae08d..a4c2044 100644 --- a/fournée/core/models.py +++ b/fournée/core/models.py @@ -4,6 +4,8 @@ from django.db import models from colorfield.fields import ColorField +from .utils import clean + def upload_to(instance, filename): return "{}/{}".format(uuid4(), filename) @@ -69,7 +71,7 @@ class Ingrédient(models.Model): quantité = models.DecimalField(max_digits=6, decimal_places=3) def __str__(self): - return f"{self.fourniture.nom} ({self.quantité} kg)" + return f"{self.fourniture.nom} ({clean(self.quantité)} kg)" class Recette(models.Model): @@ -106,7 +108,7 @@ class Réservation(models.Model): commande = models.ForeignKey("Commande", on_delete=models.CASCADE) def __str__(self): - return f"{self.quantité.normalize()} {self.pain}" + return f"{clean(self.quantité)} {self.pain}" class Commande(models.Model): @@ -136,6 +138,6 @@ class Coulée(models.Model): fournée = models.ForeignKey("Fournée", on_delete=models.CASCADE) def __str__(self): - return f"{self.pâte} ({self.quantité.normalize()} kg)" + return f"{self.pâte} ({clean(self.quantité)} kg)" diff --git a/fournée/core/utils.py b/fournée/core/utils.py new file mode 100644 index 0000000..2087473 --- /dev/null +++ b/fournée/core/utils.py @@ -0,0 +1,8 @@ +from decimal import Decimal + + +def clean(d): + if d == d.to_integral(): + return d.quantize(Decimal(1)) + else: + return d.quantize(Decimal('1.000')).normalize()