Spaces:
Running
Running
Download api/seeding/db_content.py from Dayozoe/harvestconnect-api: direct link, hf CLI and curl.
- Browser
- Download file 1.91 kB
-
https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/seeding/db_content.py
- Command line
-
hf download hf://spaces/Dayozoe/harvestconnect-api/api/seeding/db_content.py
-
curl -L -o db_content.py https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/seeding/db_content.py
1.91 kB
| from api.factories import BlogPostFactory, ReviewFactory, OrderFactory, ArtistFactory | |
| from api.models import UserProfile | |
| def create_posts(command, users, count): | |
| command.stdout.write('Creating blog posts...') | |
| posts = [BlogPostFactory.create(author=users[index % len(users)]) for index in range(count)] | |
| command.stdout.write(command.style.SUCCESS(f'✓ Created {len(posts)} blog posts')) | |
| return posts | |
| def create_reviews(command, users, products, count): | |
| command.stdout.write('Creating reviews...') | |
| reviews, pairs = [], set() | |
| for attempt in range(count * 3): | |
| if len(reviews) >= count or not products: | |
| break | |
| product = products[attempt % len(products)] | |
| reviewer = users[(attempt + 1) % len(users)] | |
| pair = (product.id, reviewer.id) | |
| if pair in pairs: | |
| continue | |
| try: | |
| reviews.append(ReviewFactory.create(product=product, reviewer=reviewer)) | |
| pairs.add(pair) | |
| except Exception: | |
| continue | |
| command.stdout.write(command.style.SUCCESS(f'✓ Created {len(reviews)} reviews')) | |
| return reviews | |
| def create_orders(command, users, count): | |
| command.stdout.write('Creating orders...') | |
| orders = [OrderFactory.create(buyer=users[index % len(users)]) for index in range(count)] | |
| command.stdout.write(command.style.SUCCESS(f'✓ Created {len(orders)} orders')) | |
| return orders | |
| def create_artists(command, users, count): | |
| command.stdout.write('Creating featured artists...') | |
| artists = [] | |
| for index in range(count): | |
| artisan = users[index % len(users)] | |
| profile, _ = UserProfile.objects.get_or_create(user=artisan) | |
| profile.role = 'artisan' | |
| profile.save() | |
| artists.append(ArtistFactory.create(user=artisan)) | |
| command.stdout.write(command.style.SUCCESS(f'✓ Created {len(artists)} featured artists')) | |
| return artists | |