Skip to content

Commit

Permalink
Merge pull request #542 from bounswe/feature/BE-faker
Browse files Browse the repository at this point in the history
Fix/be faker
  • Loading branch information
m-erkam authored Dec 16, 2024
2 parents 76c673b + d5cf477 commit 2c8691d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
21 changes: 12 additions & 9 deletions backend/marketfeed/management/commands/generate_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from onboarding.models import User

class Command(BaseCommand):
help = 'Generate fake data for the Post model'

def add_arguments(self, parser):
parser.add_argument('total', type=int, help='Number of fake posts to generate')
help = 'Generate fake data for the Post model if there are fewer than 100 posts in the database'

def handle(self, *args, **kwargs):
fake = Faker()
total = kwargs['total']
existing_posts = Post.objects.count()

if existing_posts>=100:
self.stdout.write(self.style.SUCCESS('There are already 100 or more posts in the database. No new posts created.'))
return

economy_topics = [
f"Why is everyone talking about {fake.bs()}?"[:100],
Expand Down Expand Up @@ -61,10 +62,11 @@ def handle(self, *args, **kwargs):

users = list(User.objects.all())
if not users:
self.stdout.write(self.style.ERROR('No users found in the database. Please create some users first.'))
return
fake_user = User.objects.create_user(username=fake.user_name(), email=fake.email(), password="Password123*")
users = [fake_user]
self.stdout.write(self.style.WARNING('No users found in the database. A new user has been created.'))

for _ in range(total):
for _ in range(100):
title = random.choice(economy_topics)
content = random.choice(economy_contents)
author = random.choice(users)
Expand All @@ -78,4 +80,5 @@ def handle(self, *args, **kwargs):
)
post.save()

self.stdout.write(self.style.SUCCESS(f'Successfully created {total} fake posts.'))
self.stdout.write(self.style.SUCCESS(f'Successfully created 100 fake posts.'))

3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ services:
python3 manage.py collectstatic --noinput &&
python3 manage.py update_currencies &&
python3 manage.py update_stocks &&
python3 manage.py update_indices &&
python3 manage.py update_indices &&
python3 manage.py generate_posts &&
gunicorn backend.wsgi:application --bind 0.0.0.0:8000'
restart: always
environment:
Expand Down

0 comments on commit 2c8691d

Please sign in to comment.