checkout/src/purchase/management/commands/generate_dummy_baskets.py

49 lines
1.7 KiB
Python
Raw Normal View History

2022-04-26 21:57:42 +02:00
import random
from datetime import timedelta
import freezegun
2022-04-26 22:41:26 +02:00
from django.core.management import call_command
2022-04-26 21:57:42 +02:00
from django.core.management.base import BaseCommand
from django.utils.timezone import now
from purchase.models import Basket, BasketItem, PaymentMethod, Product
class Command(BaseCommand):
2022-04-26 22:41:26 +02:00
help = "Generates dummy baskets" # noqa: A003
2022-04-26 21:57:42 +02:00
def handle(self, *args, **options):
2022-04-26 22:41:26 +02:00
call_command("loaddata", ["payment_methods", "products"])
products = list(Product.objects.all())
payment_methods = list(PaymentMethod.objects.all())
2022-04-26 21:57:42 +02:00
count = 0
hours = list(range(-29, -20))
hours += list(range(-10, -2))
for hour in hours:
with freezegun.freeze_time(now() + timedelta(hours=hour)):
count += self.generate_baskets(payment_methods, products)
self.stdout.write(self.style.SUCCESS(f"Successfully created {count} baskets."))
def delete(self, cls):
_, count = cls.objects.all().delete()
self.stdout.write(self.style.WARNING(f"Successfully deleted {count} {cls}."))
def generate_baskets(self, payment_methods, products):
count = int(random.normalvariate(20, 10))
for _ in range(count):
method = random.choice(payment_methods)
basket = Basket.objects.create(payment_method=method)
items = []
item_count = int(random.normalvariate(3, 2))
for _ in range(item_count):
product = random.choice(products)
items.append(
BasketItem(
product=product, basket=basket, quantity=random.randint(1, 3)
)
)
BasketItem.objects.bulk_create(items)
return count