From 7d217307a4fe48c5c9ee0e55e06d9fa5fb44f874 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 26 Apr 2022 22:41:26 +0200 Subject: [PATCH] Improve dummy baskets generation --- src/purchase/fixtures/payment_methods.yaml | 15 +++++++++ src/purchase/fixtures/products.yaml | 32 +++++++++++++++++++ ...ummy_data.py => generate_dummy_baskets.py} | 28 +++------------- src/purchase/models.py | 22 +++++++++++-- 4 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 src/purchase/fixtures/payment_methods.yaml create mode 100644 src/purchase/fixtures/products.yaml rename src/purchase/management/commands/{generate_dummy_data.py => generate_dummy_baskets.py} (62%) diff --git a/src/purchase/fixtures/payment_methods.yaml b/src/purchase/fixtures/payment_methods.yaml new file mode 100644 index 0000000..4876d7a --- /dev/null +++ b/src/purchase/fixtures/payment_methods.yaml @@ -0,0 +1,15 @@ +- model: purchase.paymentmethod + fields: + created_at: 2022-04-26 20:30:22.960932+00:00 + updated_at: 2022-04-26 20:30:22.960941+00:00 + name: Espèces +- model: purchase.paymentmethod + fields: + created_at: 2022-04-26 20:30:22.960948+00:00 + updated_at: 2022-04-26 20:30:22.960952+00:00 + name: CB +- model: purchase.paymentmethod + fields: + created_at: 2022-04-26 20:30:22.960958+00:00 + updated_at: 2022-04-26 20:30:22.960962+00:00 + name: Chèque diff --git a/src/purchase/fixtures/products.yaml b/src/purchase/fixtures/products.yaml new file mode 100644 index 0000000..13068be --- /dev/null +++ b/src/purchase/fixtures/products.yaml @@ -0,0 +1,32 @@ +- model: purchase.product + fields: + created_at: 2022-04-26 20:30:22.959558+00:00 + updated_at: 2022-04-26 20:30:22.959576+00:00 + name: Clou + image: '' + unit_price_cents: 134 + display_order: 1 +- model: purchase.product + fields: + created_at: 2022-04-26 20:30:22.959595+00:00 + updated_at: 2022-04-26 20:30:22.959600+00:00 + name: Villard'Ain + image: '' + unit_price_cents: 290 + display_order: 1 +- model: purchase.product + fields: + created_at: 2022-04-26 20:30:22.959610+00:00 + updated_at: 2022-04-26 20:30:22.959614+00:00 + name: Herbier + image: '' + unit_price_cents: 330 + display_order: 1 +- model: purchase.product + fields: + created_at: 2022-04-26 20:30:22.959624+00:00 + updated_at: 2022-04-26 20:30:22.959628+00:00 + name: Blanc vache + image: '' + unit_price_cents: 650 + display_order: 1 diff --git a/src/purchase/management/commands/generate_dummy_data.py b/src/purchase/management/commands/generate_dummy_baskets.py similarity index 62% rename from src/purchase/management/commands/generate_dummy_data.py rename to src/purchase/management/commands/generate_dummy_baskets.py index eca3258..a12d146 100644 --- a/src/purchase/management/commands/generate_dummy_data.py +++ b/src/purchase/management/commands/generate_dummy_baskets.py @@ -2,6 +2,7 @@ import random from datetime import timedelta import freezegun +from django.core.management import call_command from django.core.management.base import BaseCommand from django.utils.timezone import now @@ -9,31 +10,12 @@ from purchase.models import Basket, BasketItem, PaymentMethod, Product class Command(BaseCommand): - help = "Generates dummy data" # noqa: A003 + help = "Generates dummy baskets" # noqa: A003 def handle(self, *args, **options): - products = [ - Product(name="Clou", unit_price_cents=134), - Product(name="Villard'Ain", unit_price_cents=290), - Product(name="Herbier", unit_price_cents=330), - Product(name="Blanc vache", unit_price_cents=650), - ] - products = Product.objects.bulk_create(products) - self.stdout.write( - self.style.SUCCESS(f"Successfully created {len(products)} products.") - ) - - payment_methods = [ - PaymentMethod(name="Espèces"), - PaymentMethod(name="CB"), - PaymentMethod(name="Chèque"), - ] - payment_methods = PaymentMethod.objects.bulk_create(payment_methods) - self.stdout.write( - self.style.SUCCESS( - f"Successfully created {len(payment_methods)} payment methods." - ) - ) + call_command("loaddata", ["payment_methods", "products"]) + products = list(Product.objects.all()) + payment_methods = list(PaymentMethod.objects.all()) count = 0 hours = list(range(-29, -20)) diff --git a/src/purchase/models.py b/src/purchase/models.py index 612c9c3..b93a901 100644 --- a/src/purchase/models.py +++ b/src/purchase/models.py @@ -33,10 +33,15 @@ class PaymentMethodQuerySet(models.QuerySet): return self.annotate(sold=Count("baskets", distinct=True)) +class PaymentMethodManager(models.Manager): + def get_by_natural_key(self, name): + return self.get(name=name) + + class PaymentMethod(Model): name = models.CharField(max_length=50, unique=True, verbose_name=_("name")) - objects = PaymentMethodQuerySet.as_manager() + objects = PaymentMethodManager.from_queryset(PaymentMethodQuerySet) class Meta: verbose_name = _("payment method") @@ -45,6 +50,10 @@ class PaymentMethod(Model): def __str__(self): return self.name + @property + def natural_key(self): + return (self.name,) + def default_product_display_order(): last = Product.objects.last() @@ -65,6 +74,11 @@ class ProductQuerySet(models.QuerySet): return self.annotate(sold=Coalesce(Sum("basket_items__quantity"), 0)) +class ProductManager(models.Manager): + def get_by_natural_key(self, name): + return self.get(name=name) + + class Product(Model): name = models.CharField(max_length=250, unique=True, verbose_name=_("name")) image = models.ImageField(null=True, blank=True, verbose_name=_("image")) @@ -75,7 +89,7 @@ class Product(Model): default=default_product_display_order, verbose_name=_("display order") ) - objects = ProductQuerySet.as_manager() + objects = ProductManager.from_queryset(ProductQuerySet) class Meta: ordering = ["display_order", "name"] @@ -85,6 +99,10 @@ class Product(Model): def __str__(self): return self.name + @property + def natural_key(self): + return (self.name,) + def save(self, *args, **kwargs): super().save() if not self.image: