From d580e15f5f4d2c212d82e69c5d243766405ff5dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Poulain?= Date: Wed, 15 May 2024 15:50:11 +0200 Subject: [PATCH] =?UTF-8?q?fix=20pr=C3=A9sentation=20des=20d=C3=A9cimaux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fournée/core/admin.py | 9 +-------- fournée/core/models.py | 8 +++++--- fournée/core/utils.py | 8 ++++++++ 3 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 fournée/core/utils.py 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()