update
This commit is contained in:
0
core/apps/api/management/__init__.py
Normal file
0
core/apps/api/management/__init__.py
Normal file
0
core/apps/api/management/commands/__init__.py
Normal file
0
core/apps/api/management/commands/__init__.py
Normal file
400
core/apps/api/management/commands/create_fake_data.py
Normal file
400
core/apps/api/management/commands/create_fake_data.py
Normal file
@@ -0,0 +1,400 @@
|
||||
import random
|
||||
from decimal import Decimal
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import transaction
|
||||
from core.apps.api.models import (
|
||||
CategoryModel,
|
||||
AdModel,
|
||||
AdImageModel,
|
||||
AdVariantModel,
|
||||
AdOptionModel,
|
||||
TagsModel,
|
||||
ColorModel,
|
||||
BannerModel,
|
||||
FeedbackModel,
|
||||
OrderModel,
|
||||
OrderItemModel,
|
||||
AdTopPlanModel,
|
||||
)
|
||||
from core.apps.api.choices import AdType, AdCategoryType, AdVariantType, OrderStatus
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Create fake data for testing API endpoints'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--users',
|
||||
type=int,
|
||||
default=5,
|
||||
help='Number of users to create'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--ads',
|
||||
type=int,
|
||||
default=20,
|
||||
help='Number of ads to create'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--clear',
|
||||
action='store_true',
|
||||
help='Clear existing data before creating new'
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
users_count = options['users']
|
||||
ads_count = options['ads']
|
||||
clear_data = options['clear']
|
||||
|
||||
if clear_data:
|
||||
self.stdout.write(self.style.WARNING('Clearing existing data...'))
|
||||
self.clear_data()
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Starting fake data generation...'))
|
||||
|
||||
with transaction.atomic():
|
||||
# Create basic data
|
||||
plan = self.create_plan()
|
||||
colors = self.create_colors()
|
||||
tags = self.create_tags()
|
||||
categories = self.create_categories()
|
||||
users = self.create_users(users_count)
|
||||
|
||||
# Create banners
|
||||
banners = self.create_banners(categories[:3])
|
||||
|
||||
# Create ads with related data
|
||||
ads = self.create_ads(ads_count, users, categories, plan, tags, colors)
|
||||
|
||||
# Create feedbacks
|
||||
feedbacks = self.create_feedbacks(ads, users)
|
||||
|
||||
# Create orders
|
||||
orders = self.create_orders(ads, users)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('\n' + '='*50))
|
||||
self.stdout.write(self.style.SUCCESS('Fake data created successfully!'))
|
||||
self.stdout.write(self.style.SUCCESS('='*50))
|
||||
self.stdout.write(f'Users: {len(users)}')
|
||||
self.stdout.write(f'Categories: {len(categories)}')
|
||||
self.stdout.write(f'Tags: {len(tags)}')
|
||||
self.stdout.write(f'Colors: {len(colors)}')
|
||||
self.stdout.write(f'Ads: {len(ads)}')
|
||||
self.stdout.write(f'Feedbacks: {len(feedbacks)}')
|
||||
self.stdout.write(f'Orders: {len(orders)}')
|
||||
self.stdout.write(f'Banners: {len(banners)}')
|
||||
|
||||
def clear_data(self):
|
||||
"""Clear all existing data"""
|
||||
models = [
|
||||
OrderItemModel,
|
||||
OrderModel,
|
||||
FeedbackModel,
|
||||
AdImageModel,
|
||||
AdVariantModel,
|
||||
AdOptionModel,
|
||||
AdModel,
|
||||
BannerModel,
|
||||
CategoryModel,
|
||||
TagsModel,
|
||||
ColorModel,
|
||||
]
|
||||
|
||||
for model in models:
|
||||
count = model.objects.all().delete()[0]
|
||||
self.stdout.write(f'Deleted {count} {model.__name__} objects')
|
||||
|
||||
def create_plan(self):
|
||||
"""Create or get default plan"""
|
||||
plan, created = AdTopPlanModel.objects.get_or_create(
|
||||
name="Free",
|
||||
defaults={
|
||||
'price': Decimal('0.00'),
|
||||
'duration_days': 30,
|
||||
'description': 'Free basic plan'
|
||||
}
|
||||
)
|
||||
if created:
|
||||
self.stdout.write(self.style.SUCCESS('✓ Created default plan'))
|
||||
return plan
|
||||
|
||||
def create_colors(self):
|
||||
"""Create colors"""
|
||||
colors_data = [
|
||||
'Red', 'Blue', 'Green', 'Black', 'White',
|
||||
'Yellow', 'Orange', 'Purple', 'Pink', 'Brown'
|
||||
]
|
||||
colors = []
|
||||
for color_name in colors_data:
|
||||
color, created = ColorModel.objects.get_or_create(name=color_name)
|
||||
colors.append(color)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'✓ Created {len(colors)} colors'))
|
||||
return colors
|
||||
|
||||
def create_tags(self):
|
||||
"""Create tags"""
|
||||
tags_data = [
|
||||
'New', 'Hot', 'Sale', 'Popular', 'Featured',
|
||||
'Limited', 'Exclusive', 'Premium', 'Budget', 'Trending'
|
||||
]
|
||||
tags = []
|
||||
for tag_name in tags_data:
|
||||
tag, created = TagsModel.objects.get_or_create(
|
||||
name=tag_name,
|
||||
defaults={'slug': tag_name.lower()}
|
||||
)
|
||||
tags.append(tag)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'✓ Created {len(tags)} tags'))
|
||||
return tags
|
||||
|
||||
def create_categories(self):
|
||||
"""Create categories"""
|
||||
categories_data = [
|
||||
{'name': 'Electronics', 'show_home': True},
|
||||
{'name': 'Fashion', 'show_home': True},
|
||||
{'name': 'Home & Garden', 'show_home': True},
|
||||
{'name': 'Sports', 'show_home': False},
|
||||
{'name': 'Toys', 'show_home': False},
|
||||
{'name': 'Books', 'show_home': True},
|
||||
{'name': 'Automotive', 'show_home': False},
|
||||
{'name': 'Beauty', 'show_home': True},
|
||||
]
|
||||
|
||||
categories = []
|
||||
for cat_data in categories_data:
|
||||
cat, created = CategoryModel.objects.get_or_create(
|
||||
name=cat_data['name'],
|
||||
defaults={
|
||||
'show_home': cat_data['show_home'],
|
||||
'category_type': AdCategoryType.PRODUCT,
|
||||
'level': 0
|
||||
}
|
||||
)
|
||||
categories.append(cat)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'✓ Created {len(categories)} categories'))
|
||||
return categories
|
||||
|
||||
def create_users(self, count):
|
||||
"""Create test users"""
|
||||
users = []
|
||||
|
||||
# Create admin user
|
||||
admin, created = User.objects.get_or_create(
|
||||
username='admin',
|
||||
defaults={
|
||||
'email': 'admin@example.com',
|
||||
'is_staff': True,
|
||||
'is_superuser': True,
|
||||
}
|
||||
)
|
||||
if created:
|
||||
admin.set_password('admin123')
|
||||
admin.save()
|
||||
users.append(admin)
|
||||
|
||||
# Create regular users
|
||||
for i in range(1, count):
|
||||
user, created = User.objects.get_or_create(
|
||||
username=f'user{i}',
|
||||
defaults={
|
||||
'email': f'user{i}@example.com',
|
||||
'first_name': f'User',
|
||||
'last_name': f'{i}',
|
||||
}
|
||||
)
|
||||
if created:
|
||||
user.set_password('password123')
|
||||
user.save()
|
||||
users.append(user)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'✓ Created {len(users)} users'))
|
||||
return users
|
||||
|
||||
def create_banners(self, categories):
|
||||
"""Create banners"""
|
||||
banners = []
|
||||
banner_data = [
|
||||
{
|
||||
'title': 'Summer Sale',
|
||||
'description': 'Up to 50% off on selected items',
|
||||
'bg_color': '#FF5733',
|
||||
'text_color': '#FFFFFF',
|
||||
},
|
||||
{
|
||||
'title': 'New Arrivals',
|
||||
'description': 'Check out our latest products',
|
||||
'bg_color': '#3498DB',
|
||||
'text_color': '#FFFFFF',
|
||||
},
|
||||
{
|
||||
'title': 'Free Shipping',
|
||||
'description': 'On orders over $50',
|
||||
'bg_color': '#2ECC71',
|
||||
'text_color': '#FFFFFF',
|
||||
},
|
||||
]
|
||||
|
||||
for i, data in enumerate(banner_data):
|
||||
banner = BannerModel.objects.create(
|
||||
title=data['title'],
|
||||
description=data['description'],
|
||||
link=f'/category/{categories[i].id}/' if i < len(categories) else '/',
|
||||
bg_color=data['bg_color'],
|
||||
text_color=data['text_color'],
|
||||
order=i,
|
||||
is_active=True,
|
||||
)
|
||||
banners.append(banner)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'✓ Created {len(banners)} banners'))
|
||||
return banners
|
||||
|
||||
def create_ads(self, count, users, categories, plan, tags, colors):
|
||||
"""Create ads with variants, images, and options"""
|
||||
ads = []
|
||||
|
||||
products = [
|
||||
'Smartphone', 'Laptop', 'T-Shirt', 'Jeans', 'Sofa',
|
||||
'Chair', 'Football', 'Basketball', 'Book', 'Watch',
|
||||
'Headphones', 'Camera', 'Shoes', 'Bag', 'Sunglasses',
|
||||
'Jacket', 'Perfume', 'Toy Car', 'Bicycle', 'Tablet'
|
||||
]
|
||||
|
||||
for i in range(count):
|
||||
product_name = products[i % len(products)]
|
||||
category = random.choice(categories)
|
||||
user = random.choice(users)
|
||||
|
||||
ad = AdModel.objects.create(
|
||||
user=user,
|
||||
name=f'{product_name} - Model {i+1}',
|
||||
ad_type=random.choice([AdType.SALE, AdType.RENT]),
|
||||
category=category,
|
||||
ad_category_type=AdCategoryType.PRODUCT,
|
||||
_price=Decimal(random.randint(10, 1000)),
|
||||
discount=Decimal(random.randint(0, 30)),
|
||||
is_available=True,
|
||||
physical_product=True,
|
||||
plan=plan,
|
||||
description=f'High quality {product_name.lower()} with amazing features. '
|
||||
f'Perfect condition, great price. Limited stock available!',
|
||||
)
|
||||
|
||||
# Add random tags
|
||||
ad.tags.set(random.sample(tags, random.randint(1, 3)))
|
||||
|
||||
# Create variants
|
||||
for color in random.sample(colors, random.randint(1, 3)):
|
||||
AdVariantModel.objects.create(
|
||||
ad=ad,
|
||||
variant=AdVariantType.COLOR,
|
||||
value=color.name,
|
||||
color=color,
|
||||
price=Decimal(random.randint(50, 500)),
|
||||
stock_quantity=random.randint(1, 100),
|
||||
is_available=True,
|
||||
)
|
||||
|
||||
# Create size variants
|
||||
sizes = ['S', 'M', 'L', 'XL']
|
||||
for size in random.sample(sizes, random.randint(1, 3)):
|
||||
AdVariantModel.objects.create(
|
||||
ad=ad,
|
||||
variant=AdVariantType.SIZE,
|
||||
value=size,
|
||||
price=Decimal(random.randint(50, 500)),
|
||||
stock_quantity=random.randint(1, 100),
|
||||
is_available=True,
|
||||
)
|
||||
|
||||
# Create options
|
||||
options = [
|
||||
('Brand', random.choice(['Samsung', 'Apple', 'Sony', 'LG', 'Generic'])),
|
||||
('Warranty', f'{random.choice([6, 12, 24])} months'),
|
||||
('Condition', random.choice(['New', 'Like New', 'Used - Good'])),
|
||||
]
|
||||
for opt_name, opt_value in options:
|
||||
AdOptionModel.objects.create(
|
||||
ad=ad,
|
||||
name=opt_name,
|
||||
value=opt_value,
|
||||
)
|
||||
|
||||
ads.append(ad)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'✓ Created {len(ads)} ads with variants and options'))
|
||||
return ads
|
||||
|
||||
def create_feedbacks(self, ads, users):
|
||||
"""Create feedbacks for ads"""
|
||||
feedbacks = []
|
||||
|
||||
comments = [
|
||||
'Great product! Highly recommended.',
|
||||
'Good quality, fast delivery.',
|
||||
'Exactly as described. Very satisfied.',
|
||||
'Amazing! Worth every penny.',
|
||||
'Nice product but delivery was slow.',
|
||||
'Excellent quality and service.',
|
||||
'Very good, will buy again.',
|
||||
'Perfect condition, thank you!',
|
||||
'Good value for money.',
|
||||
'Satisfied with the purchase.',
|
||||
]
|
||||
|
||||
for ad in random.sample(ads, min(len(ads), 15)):
|
||||
for _ in range(random.randint(1, 5)):
|
||||
user = random.choice(users)
|
||||
feedback = FeedbackModel.objects.create(
|
||||
user=user,
|
||||
ad=ad,
|
||||
star=random.randint(3, 5),
|
||||
comment=random.choice(comments),
|
||||
)
|
||||
feedbacks.append(feedback)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'✓ Created {len(feedbacks)} feedbacks'))
|
||||
return feedbacks
|
||||
|
||||
def create_orders(self, ads, users):
|
||||
"""Create orders"""
|
||||
orders = []
|
||||
|
||||
for user in random.sample(users, min(len(users), 3)):
|
||||
for _ in range(random.randint(1, 3)):
|
||||
order = OrderModel.objects.create(
|
||||
user=user,
|
||||
status=random.choice([
|
||||
OrderStatus.PENDING,
|
||||
OrderStatus.PROCESSING,
|
||||
OrderStatus.COMPLETED
|
||||
]),
|
||||
total_amount=Decimal('0.00'),
|
||||
)
|
||||
|
||||
# Add order items
|
||||
total = Decimal('0.00')
|
||||
for ad in random.sample(ads, random.randint(1, 3)):
|
||||
quantity = random.randint(1, 3)
|
||||
price = ad.price
|
||||
|
||||
OrderItemModel.objects.create(
|
||||
order=order,
|
||||
ad=ad,
|
||||
price=price,
|
||||
quantity=quantity,
|
||||
)
|
||||
total += price * quantity
|
||||
|
||||
order.total_amount = total
|
||||
order.save()
|
||||
orders.append(order)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'✓ Created {len(orders)} orders'))
|
||||
return orders
|
||||
Reference in New Issue
Block a user