diff --git a/fournée/core/admin.py b/fournée/core/admin.py index cf11929..70b1451 100644 --- a/fournée/core/admin.py +++ b/fournée/core/admin.py @@ -32,6 +32,16 @@ class PainAdmin(admin.ModelAdmin): admin_site.register(models.Pain, PainAdmin) +class FournitureAdmin(admin.ModelAdmin): + list_display = ["nom", "ordre", "couleur"] + list_editable = ["ordre", "couleur"] + actions = None + save_as = True + + +admin_site.register(models.Fourniture, FournitureAdmin) + + class IngrédientInline(admin.TabularInline): model = models.Ingrédient sortable_field_name = "ordre" diff --git a/fournée/core/migrations/0008_fourniture_alter_ingrédient_options_and_more.py b/fournée/core/migrations/0008_fourniture_alter_ingrédient_options_and_more.py new file mode 100644 index 0000000..a1cd874 --- /dev/null +++ b/fournée/core/migrations/0008_fourniture_alter_ingrédient_options_and_more.py @@ -0,0 +1,55 @@ +# Generated by Django 4.2.13 on 2024-05-14 07:23 + +import django.db.models.deletion +from django.db import migrations, models + +import colorfield.fields + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0007_ingrédient_couleur"), + ] + + operations = [ + migrations.CreateModel( + name="Fourniture", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("nom", models.CharField(max_length=64)), + ("ordre", models.SmallIntegerField(default=0)), + ( + "couleur", + colorfield.fields.ColorField( + default="#000000", image_field=None, max_length=25, samples=None + ), + ), + ], + options={ + "ordering": ["ordre"], + }, + ), + migrations.AlterModelOptions( + name="ingrédient", + options={"ordering": ["fourniture__ordre", "-quantité"]}, + ), + migrations.AddField( + model_name="ingrédient", + name="fourniture", + field=models.ForeignKey( + blank=True, + on_delete=django.db.models.deletion.CASCADE, + null=True, + to="core.fourniture", + ), + preserve_default=False, + ), + ] diff --git a/fournée/core/migrations/0009_peuple_les_fournitures.py b/fournée/core/migrations/0009_peuple_les_fournitures.py new file mode 100644 index 0000000..d8ee62d --- /dev/null +++ b/fournée/core/migrations/0009_peuple_les_fournitures.py @@ -0,0 +1,39 @@ +# Generated by Django 4.2.13 on 2024-05-14 07:24 + +from django.db import migrations + + +def ingredients_vers_fournitures(apps, schema_editor): + Ingrédient = apps.get_model("core", "Ingrédient") + Fourniture = apps.get_model("core", "Fourniture") + for ingrédient in Ingrédient.objects.all(): + fourniture, created = Fourniture.objects.get_or_create( + nom=ingrédient.nom, + defaults={ + 'ordre': ingrédient.ordre, + 'couleur': ingrédient.couleur, + } + ) + ingrédient.fourniture = fourniture + ingrédient.save() + + +def fournitures_vers_ingredients(apps, schema_editor): + Ingrédient = apps.get_model("core", "Ingrédient") + Fourniture = apps.get_model("core", "Fourniture") + + for fourniture in Fourniture.objects.all(): + ingrédients = Ingrédient.objects.filter(fourniture=fourniture) + ingrédients.update( + nom=fourniture.nom, + ordre=fourniture.ordre, + couleur=fourniture.couleur, + ) + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0008_fourniture_alter_ingrédient_options_and_more"), + ] + + operations = [migrations.RunPython(ingredients_vers_fournitures, fournitures_vers_ingredients)] diff --git a/fournée/core/models.py b/fournée/core/models.py index 270e388..1bb4378 100644 --- a/fournée/core/models.py +++ b/fournée/core/models.py @@ -25,18 +25,32 @@ class Fournée(models.Model): for c in poids_qté ]) + +class Fourniture(models.Model): + class Meta: + ordering = ["ordre"] + + nom = models.CharField(max_length=64) + ordre = models.SmallIntegerField(default=0) + couleur = ColorField(default='#000000') + + def __str__(self): + return f"{self.nom}" + + class Ingrédient(models.Model): class Meta: - ordering = ["ordre", "-quantité"] + ordering = ["fourniture__ordre", "-quantité"] recette = models.ForeignKey("Recette", on_delete=models.CASCADE) nom = models.CharField(max_length=64) + fourniture = models.ForeignKey("Fourniture", on_delete=models.CASCADE, blank=True, null=True) quantité = models.DecimalField(max_digits=6, decimal_places=3) ordre = models.SmallIntegerField(default=0) couleur = ColorField(default='#000000') def __str__(self): - return f"{self.nom} ({self.quantité} kg)" + return f"{self.fourniture.nom} ({self.quantité} kg)" class Recette(models.Model):