Improve dummy baskets generation

This commit is contained in:
Gabriel Augendre 2022-04-26 22:41:26 +02:00
parent 2833b3b749
commit 7d217307a4
4 changed files with 72 additions and 25 deletions

View file

@ -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

View file

@ -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

View file

@ -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))

View file

@ -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: