From 94cfbc94e61b621e9a4f34c0d566a2fc0835ce3e Mon Sep 17 00:00:00 2001 From: oguzpancuk <44545164+oguzpancuk@users.noreply.github.com> Date: Mon, 16 Dec 2024 21:04:44 +0300 Subject: [PATCH 1/3] Added some features to fake post generation Create fake user if no users in database to make the fake user author of fake posts. Also added to general docker-compose.yaml --- .../management/commands/generate_posts.py | 63 ++++++++++--------- docker-compose.yaml | 1 + 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/backend/marketfeed/management/commands/generate_posts.py b/backend/marketfeed/management/commands/generate_posts.py index cc78c9a2..87056961 100644 --- a/backend/marketfeed/management/commands/generate_posts.py +++ b/backend/marketfeed/management/commands/generate_posts.py @@ -2,39 +2,40 @@ from faker import Faker from django.core.management.base import BaseCommand from marketfeed.models import Post -from onboarding.models import User +from onboarding.models import User # Replace with your custom user model if applicable 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], - f"Is {fake.bs()} really the future?"[:100], - f"{fake.company()} just did something amazing with {fake.bs()}!"[:100], - f"I can’t believe how {fake.bs()} is changing the game"[:100], - f"Anyone else seeing the hype around {fake.bs()}?"[:100], - f"{fake.bs()} and what it means for us all"[:100], - f"The cool ways {fake.company()} uses {fake.bs()}"[:100], - f"{fake.currency_name()} in {fake.bs()}—what’s happening?"[:100], - f"Big moves in {fake.bs()}—check this out!"[:100], - f"How {fake.bs()} is reshaping the world"[:100], - f"What’s your take on {fake.bs()} trends?"[:100], - f"Let’s talk about {fake.bs()} innovations"[:100], - f"I just read about {fake.bs()} and it’s wild"[:100], - f"{fake.bs()} in the news—here’s what I think"[:100], - f"Do you think {fake.bs()} is overhyped?"[:100], - f"{fake.bs()} updates—what’s happening now?"[:100], - f"How {fake.company()} is mastering {fake.bs()}"[:100], - f"{fake.bs()} is trending, but why?"[:100], - f"Let’s chat about {fake.bs()} strategies"[:100], - f"What’s so exciting about {fake.bs()}?"[:100] + f"Why is everyone talking about {fake.bs()}?"[:50], + f"Is {fake.bs()} really the future?"[:50], + f"{fake.company()} just did something amazing with {fake.bs()}!"[:50], + f"I can’t believe how {fake.bs()} is changing the game"[:50], + f"Anyone else seeing the hype around {fake.bs()}?"[:50], + f"{fake.bs()} and what it means for us all"[:50], + f"The cool ways {fake.company()} uses {fake.bs()}"[:50], + f"{fake.currency_name()} in {fake.bs()}—what’s happening?"[:50], + f"Big moves in {fake.bs()}—check this out!"[:50], + f"How {fake.bs()} is reshaping the world"[:50], + f"What’s your take on {fake.bs()} trends?"[:50], + f"Let’s talk about {fake.bs()} innovations"[:50], + f"I just read about {fake.bs()} and it’s wild"[:50], + f"{fake.bs()} in the news—here’s what I think"[:50], + f"Do you think {fake.bs()} is overhyped?"[:50], + f"{fake.bs()} updates—what’s happening now?"[:50], + f"How {fake.company()} is mastering {fake.bs()}"[:50], + f"{fake.bs()} is trending, but why?"[:50], + f"Let’s chat about {fake.bs()} strategies"[:50], + f"What’s so exciting about {fake.bs()}?"[:50] ] economy_contents = [ @@ -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) @@ -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.')) + diff --git a/docker-compose.yaml b/docker-compose.yaml index 1b8c1c77..2daab7f9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -31,6 +31,7 @@ services: python3 manage.py collectstatic --noinput && python3 manage.py update_currencies && python3 manage.py update_stocks && + python3 manage.py generate_posts gunicorn backend.wsgi:application --bind 0.0.0.0:8000' restart: always environment: From 2a2f8ed494b9c85eb6357d10d7f94692492e3f5b Mon Sep 17 00:00:00 2001 From: oguzpancuk <44545164+oguzpancuk@users.noreply.github.com> Date: Mon, 16 Dec 2024 21:10:20 +0300 Subject: [PATCH 2/3] Minor fix Removed comment from external resource --- backend/marketfeed/management/commands/generate_posts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/marketfeed/management/commands/generate_posts.py b/backend/marketfeed/management/commands/generate_posts.py index 87056961..0bd4c17f 100644 --- a/backend/marketfeed/management/commands/generate_posts.py +++ b/backend/marketfeed/management/commands/generate_posts.py @@ -2,7 +2,7 @@ from faker import Faker from django.core.management.base import BaseCommand from marketfeed.models import Post -from onboarding.models import User # Replace with your custom user model if applicable +from onboarding.models import User class Command(BaseCommand): help = 'Generate fake data for the Post model if there are fewer than 100 posts in the database' From d5cf477c954b29ef6728835e1ea524b5662bf00d Mon Sep 17 00:00:00 2001 From: oguzpancuk <44545164+oguzpancuk@users.noreply.github.com> Date: Mon, 16 Dec 2024 21:13:11 +0300 Subject: [PATCH 3/3] Minor fix Increased title length to 100 --- .../management/commands/generate_posts.py | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/backend/marketfeed/management/commands/generate_posts.py b/backend/marketfeed/management/commands/generate_posts.py index 0bd4c17f..c7d827df 100644 --- a/backend/marketfeed/management/commands/generate_posts.py +++ b/backend/marketfeed/management/commands/generate_posts.py @@ -16,26 +16,26 @@ def handle(self, *args, **kwargs): return economy_topics = [ - f"Why is everyone talking about {fake.bs()}?"[:50], - f"Is {fake.bs()} really the future?"[:50], - f"{fake.company()} just did something amazing with {fake.bs()}!"[:50], - f"I can’t believe how {fake.bs()} is changing the game"[:50], - f"Anyone else seeing the hype around {fake.bs()}?"[:50], - f"{fake.bs()} and what it means for us all"[:50], - f"The cool ways {fake.company()} uses {fake.bs()}"[:50], - f"{fake.currency_name()} in {fake.bs()}—what’s happening?"[:50], - f"Big moves in {fake.bs()}—check this out!"[:50], - f"How {fake.bs()} is reshaping the world"[:50], - f"What’s your take on {fake.bs()} trends?"[:50], - f"Let’s talk about {fake.bs()} innovations"[:50], - f"I just read about {fake.bs()} and it’s wild"[:50], - f"{fake.bs()} in the news—here’s what I think"[:50], - f"Do you think {fake.bs()} is overhyped?"[:50], - f"{fake.bs()} updates—what’s happening now?"[:50], - f"How {fake.company()} is mastering {fake.bs()}"[:50], - f"{fake.bs()} is trending, but why?"[:50], - f"Let’s chat about {fake.bs()} strategies"[:50], - f"What’s so exciting about {fake.bs()}?"[:50] + f"Why is everyone talking about {fake.bs()}?"[:100], + f"Is {fake.bs()} really the future?"[:100], + f"{fake.company()} just did something amazing with {fake.bs()}!"[:100], + f"I can’t believe how {fake.bs()} is changing the game"[:100], + f"Anyone else seeing the hype around {fake.bs()}?"[:100], + f"{fake.bs()} and what it means for us all"[:100], + f"The cool ways {fake.company()} uses {fake.bs()}"[:100], + f"{fake.currency_name()} in {fake.bs()}—what’s happening?"[:100], + f"Big moves in {fake.bs()}—check this out!"[:100], + f"How {fake.bs()} is reshaping the world"[:100], + f"What’s your take on {fake.bs()} trends?"[:100], + f"Let’s talk about {fake.bs()} innovations"[:100], + f"I just read about {fake.bs()} and it’s wild"[:100], + f"{fake.bs()} in the news—here’s what I think"[:100], + f"Do you think {fake.bs()} is overhyped?"[:100], + f"{fake.bs()} updates—what’s happening now?"[:100], + f"How {fake.company()} is mastering {fake.bs()}"[:100], + f"{fake.bs()} is trending, but why?"[:100], + f"Let’s chat about {fake.bs()} strategies"[:100], + f"What’s so exciting about {fake.bs()}?"[:100] ] economy_contents = [