From 9ed2726ea98028120152d9fc0bbfcea3b1b11a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Poulain?= Date: Wed, 9 Oct 2024 20:05:59 +0200 Subject: [PATCH] =?UTF-8?q?feat(fourn=C3=A9e):=20ajoute=20les=20masses=20c?= =?UTF-8?q?ommand=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fournée/core/admin.py | 1 + fournée/core/models.py | 17 +++++++++++++++-- .../templates/admin/fournée_change_form.html | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/fournée/core/admin.py b/fournée/core/admin.py index f66abf3..2c29525 100644 --- a/fournée/core/admin.py +++ b/fournée/core/admin.py @@ -366,6 +366,7 @@ class FournéeAdmin(nested_admin.NestedModelAdmin): )) totaux.append(total) extra_context["totaux_divisions"] = totaux + extra_context["fournée"] = obj return super().change_view( request, diff --git a/fournée/core/models.py b/fournée/core/models.py index 372391a..da8441c 100644 --- a/fournée/core/models.py +++ b/fournée/core/models.py @@ -2,6 +2,7 @@ import datetime from uuid import uuid4 from django.db import models +from django.db.models import F from colorfield.fields import ColorField @@ -22,6 +23,11 @@ class Fournée(models.Model): def __str__(self): return f"Fournée du {self.date: %d-%m-%Y} pour {self.pour}" + def poids_total_commandé(self): + return clean(sum( + [c.poids_total_commandé() for c in self.commande_set.all()] + )) + def masse_commandée(self, pâte_id): "Renvoie la masse commandée d'une pâte donnée" poids_qté = ( @@ -163,12 +169,19 @@ class Commande(models.Model): pains = models.ManyToManyField("Pain", through="Réservation") fournée = models.ForeignKey("Fournée", on_delete=models.CASCADE) + def poids_total_commandé(self): + return sum([r.poids for r in self.réservation_set.annotate( + poids=F('quantité')*F('pain__poids') + )]) + def __str__(self): details = "" if self.id and self.réservation_set.exists(): - details = " (" + ", ".join( + details = f" ({clean(self.poids_total_commandé())} kg : " + details += ", ".join( [str(r) for r in self.réservation_set.all()] - ) + ")" + ) + details += ")" return f"{self.pour}" + details diff --git a/fournée/templates/admin/fournée_change_form.html b/fournée/templates/admin/fournée_change_form.html index 2049a75..aba3984 100644 --- a/fournée/templates/admin/fournée_change_form.html +++ b/fournée/templates/admin/fournée_change_form.html @@ -15,7 +15,7 @@ {% block after_field_sets %} {% if commandes %} -

Résumé des commandes

+

Résumé des commandes ({{ fournée.poids_total_commandé }} kg)