|
| 1 | +# setup_test_data.py |
| 2 | +import random |
| 3 | +from faker import Faker |
| 4 | +from datetime import datetime, timedelta |
| 5 | + |
| 6 | +from django.db import transaction |
| 7 | +from django.core.management.base import BaseCommand |
| 8 | + |
| 9 | +from store.models import Address, Cart, CartItem, Category, Comment, Order, OrderItem, Product, Discount, Customer |
| 10 | +from store.factories import ( |
| 11 | + CartFactory, |
| 12 | + CategoryFactory, |
| 13 | + CommentFactory, |
| 14 | + OrderItemFactory, |
| 15 | + ProductFactory, |
| 16 | + DiscountFactory, |
| 17 | + CustomerFactory, |
| 18 | + AddressFactory, |
| 19 | + OrderFactory, |
| 20 | + CartItemFactory, |
| 21 | +) |
| 22 | + |
| 23 | +faker = Faker() |
| 24 | + |
| 25 | +list_of_models = [CartItem, Cart, OrderItem, Order, Product, Category, Comment, Discount, Address, Customer] |
| 26 | + |
| 27 | +NUM_CATEGORIES = 100 |
| 28 | +NUM_DISCOUNTS = 10 |
| 29 | +NUM_PRODUCTS = 1000 |
| 30 | +NUM_CUSTOMERS = 100 |
| 31 | +NUM_ORDERS = 30 |
| 32 | +NUM_CARTS = 100 |
| 33 | + |
| 34 | +class Command(BaseCommand): |
| 35 | + help = "Generates fake data" |
| 36 | + |
| 37 | + @transaction.atomic |
| 38 | + def handle(self, *args, **kwargs): |
| 39 | + self.stdout.write("Deleting old data...") |
| 40 | + models = list_of_models |
| 41 | + for m in models: |
| 42 | + m.objects.all().delete() |
| 43 | + |
| 44 | + self.stdout.write("Creating new data...\n") |
| 45 | + |
| 46 | + # Categories data |
| 47 | + print(f"Adding {NUM_CATEGORIES} categories...", end='') |
| 48 | + all_categories = [CategoryFactory(top_product=None) for _ in range(NUM_CATEGORIES)] |
| 49 | + print('DONE') |
| 50 | + |
| 51 | + # Discounts data |
| 52 | + print(f"Adding {NUM_DISCOUNTS} discounts...", end='') |
| 53 | + all_discounts = [DiscountFactory() for _ in range(NUM_DISCOUNTS)] |
| 54 | + print('DONE') |
| 55 | + |
| 56 | + # Products data |
| 57 | + print(f"Adding {NUM_PRODUCTS} product...", end='') |
| 58 | + all_products = list() |
| 59 | + for _ in range(NUM_PRODUCTS): |
| 60 | + new_product = ProductFactory(category_id=random.choice(all_categories).id) |
| 61 | + new_product.datetime_created = faker.date_time_ad(start_datetime=datetime(2022,1,1), end_datetime=datetime(2023,1,1)) |
| 62 | + new_product.datetime_modified = new_product.datetime_created + timedelta(hours=random.randint(1, 5000)) |
| 63 | + all_products.append(new_product) |
| 64 | + print('DONE') |
| 65 | + |
| 66 | + # Customers data |
| 67 | + print(f"Adding {NUM_CUSTOMERS} customers...", end='') |
| 68 | + all_customers = [CustomerFactory() for _ in range(NUM_CUSTOMERS)] |
| 69 | + print('DONE') |
| 70 | + |
| 71 | + # Addresses data |
| 72 | + print(f"Adding customers addresses...", end='') |
| 73 | + all_addresses = [ |
| 74 | + AddressFactory(customer_id=customer.id) for customer in all_customers |
| 75 | + ] |
| 76 | + print('DONE') |
| 77 | + |
| 78 | + # Orders data |
| 79 | + print(f"Adding {NUM_ORDERS} orders...", end='') |
| 80 | + all_orders = [OrderFactory( |
| 81 | + customer_id=random.choice(all_customers).id |
| 82 | + ) for _ in range(NUM_ORDERS)] |
| 83 | + for order in all_orders: |
| 84 | + order.datetime_created = faker.date_time_ad(start_datetime=datetime(2022,6,1), end_datetime=datetime(2023,1,1)) |
| 85 | + print('DONE') |
| 86 | + |
| 87 | + # OrderItems data |
| 88 | + print(f"Adding order items...", end='') |
| 89 | + all_order_items = list() |
| 90 | + for order in all_orders: |
| 91 | + products = random.sample(all_products, random.randint(1, 10)) |
| 92 | + for product in products: |
| 93 | + order_item = OrderItemFactory( |
| 94 | + order_id=order.id, |
| 95 | + product_id=product.id, |
| 96 | + unit_price=product.unit_price, |
| 97 | + ) |
| 98 | + all_order_items.append(order_item) |
| 99 | + print('DONE') |
| 100 | + |
| 101 | + # Comments data |
| 102 | + print(f"Adding product comments...", end='') |
| 103 | + for product in all_products: |
| 104 | + for _ in range(random.randint(1, 5)): |
| 105 | + comment = CommentFactory(product_id=product.id) |
| 106 | + comment.datetime_created = faker.date_time_ad(start_datetime=datetime(2015,1,1), end_datetime=datetime(2023,1,1)) |
| 107 | + print('DONE') |
| 108 | + |
| 109 | + # Carts data |
| 110 | + print(f"Adding {NUM_CARTS} carts...", end='') |
| 111 | + all_carts = [CartFactory() for _ in range(NUM_CARTS)] |
| 112 | + print('DONE') |
| 113 | + |
| 114 | + # CartItems data |
| 115 | + print(f"Adding cart items...", end='') |
| 116 | + all_cart_items = list() |
| 117 | + for cart in all_carts: |
| 118 | + products = random.sample(all_products, random.randint(1, 10)) |
| 119 | + for product in products: |
| 120 | + cart_item = CartItemFactory( |
| 121 | + cart_id=cart.id, |
| 122 | + product_id=product.id, |
| 123 | + ) |
| 124 | + all_cart_items.append(cart_item) |
| 125 | + print('DONE') |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments