Spaces:
Running
Running
Download api/seeding/db_core.py from Dayozoe/harvestconnect-api: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/seeding/db_core.py
- Command line
-
hf download hf://spaces/Dayozoe/harvestconnect-api/api/seeding/db_core.py
-
curl -L -o db_core.py https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/seeding/db_core.py
1.26 kB
| from api.factories import CategoryFactory, UserFactory, ProductFactory | |
| from api.models import UserProfile | |
| def create_categories(command, count): | |
| command.stdout.write('Creating categories...') | |
| items = CategoryFactory.create_batch(count) | |
| command.stdout.write(command.style.SUCCESS(f'✓ Created {len(items)} categories')) | |
| return items | |
| def create_users(command, count): | |
| command.stdout.write('Creating users and profiles...') | |
| users = [] | |
| for _ in range(count): | |
| user = UserFactory.create() | |
| UserProfile.objects.get_or_create(user=user) | |
| users.append(user) | |
| command.stdout.write(command.style.SUCCESS(f'✓ Created {len(users)} users with profiles')) | |
| return users | |
| def create_products(command, users, categories, count): | |
| command.stdout.write('Creating products...') | |
| products = [] | |
| for index in range(count): | |
| seller = users[index % len(users)] | |
| profile, _ = UserProfile.objects.get_or_create(user=seller) | |
| profile.role = 'seller' | |
| profile.save() | |
| products.append(ProductFactory.create(seller=seller, category=categories[index % len(categories)])) | |
| command.stdout.write(command.style.SUCCESS(f'✓ Created {len(products)} products')) | |
| return products | |