From 0689c9223b7320ac1ba710ba9011403efe292293 Mon Sep 17 00:00:00 2001 From: Julian Dehm Date: Tue, 13 Feb 2024 16:26:44 +0100 Subject: [PATCH] apps/dev: add management command to create fake comments for testing --- changelog/_0001.md | 3 + .../management/commands/create_comments.py | 78 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 changelog/_0001.md create mode 100644 meinberlin/apps/dev/management/commands/create_comments.py diff --git a/changelog/_0001.md b/changelog/_0001.md new file mode 100644 index 0000000000..47e863f024 --- /dev/null +++ b/changelog/_0001.md @@ -0,0 +1,3 @@ +### Added + +- added management command to create fake comments for testing diff --git a/meinberlin/apps/dev/management/commands/create_comments.py b/meinberlin/apps/dev/management/commands/create_comments.py new file mode 100644 index 0000000000..eacb3dfec3 --- /dev/null +++ b/meinberlin/apps/dev/management/commands/create_comments.py @@ -0,0 +1,78 @@ +from argparse import RawTextHelpFormatter + +from django.contrib.auth import get_user_model +from django.core.management.base import BaseCommand +from faker import Faker + +from adhocracy4.comments.models import Comment +from meinberlin.apps.ideas.models import Idea + + +class Command(BaseCommand): + help = """ + Creates fake comments for testing. + The command creates comments as on the specified . + To generate comments with the fixed text "Fake Comment" specify "--fixed". + Usage: + + $ ./manage.py create_comments --user --idea --count + """ + + def create_parser(self, *args, **kwargs): + parser = super(Command, self).create_parser(*args, **kwargs) + parser.formatter_class = RawTextHelpFormatter + return parser + + def add_arguments(self, parser): + parser.add_argument( + "--user", + default=0, + type=int, + required=True, + help="primary key of user used to create the comments", + ) + parser.add_argument( + "--idea", + default=-1, + type=int, + required=True, + help="primary key of the idea to add the comments to", + ) + parser.add_argument( + "--count", + default=1, + type=int, + required=True, + help="number of comments to create", + ) + parser.add_argument( + "--fixed", + action="store_true", + help="use fixed comment text", + ) + + def handle(self, *args, **options): + user_pk = options["user"] + idea_pk = options["idea"] + count = options["count"] + fixed = options["fixed"] + + user_model = get_user_model() + user = user_model.objects.get(pk=user_pk) + idea = Idea.objects.get(pk=idea_pk) + + fake = Faker("en_US") + + comments = [] + for i in range(count): + comments.append( + Comment( + content_object=idea, + creator=user, + project=idea.project, + comment=( + fake.text(max_nb_chars=200) if not fixed else "Fake Comment" + ), + ) + ) + Comment.objects.bulk_create(comments)