diff options
-rw-r--r-- | askbot/doc/source/changelog.rst | 1 | ||||
-rw-r--r-- | askbot/doc/source/management-commands.rst | 2 | ||||
-rw-r--r-- | askbot/management/commands/askbot_add_test_content.py | 228 |
3 files changed, 231 insertions, 0 deletions
diff --git a/askbot/doc/source/changelog.rst b/askbot/doc/source/changelog.rst index 0ba88155..31b21813 100644 --- a/askbot/doc/source/changelog.rst +++ b/askbot/doc/source/changelog.rst @@ -21,6 +21,7 @@ Development version (not yet published) * Added support for LOGIN_REDIRECT_URL to the login app (hjwp, Evgeny) * Updated Italian localization (Luca Ferroni) * Added Catalan localization (Jordi Bofill) +* Added management command ``askbot_add_test_content`` (Dejan Noveski) 0.7.26 (Current Version) ------------------------ diff --git a/askbot/doc/source/management-commands.rst b/askbot/doc/source/management-commands.rst index d69c8673..b1ee5bc5 100644 --- a/askbot/doc/source/management-commands.rst +++ b/askbot/doc/source/management-commands.rst @@ -175,3 +175,5 @@ the developers of the Askbot project: | | remaining commands that are expected to be run from the | | | site root directory. | +--------------------------------+-------------------------------------------------------------+ +| `askbot_add_test_content` | Creates content with dummy data for testing | ++--------------------------------+-------------------------------------------------------------+ diff --git a/askbot/management/commands/askbot_add_test_content.py b/askbot/management/commands/askbot_add_test_content.py new file mode 100644 index 00000000..eb29d1c5 --- /dev/null +++ b/askbot/management/commands/askbot_add_test_content.py @@ -0,0 +1,228 @@ +from django.core.management.base import NoArgsCommand +from askbot.models import User + + +NUM_USERS = 40 +# KEEP NEXT 3 SETTINGS LESS THAN OR EQUAL TO NUM_USERS! +NUM_QUESTIONS = 40 +NUM_ANSWERS = 20 +NUM_COMMENTS = 20 + +# To ensure that all the actions can be made, repute each user high positive +# karma. This can be calculated dynamically - max of MIN_REP_TO_... settings +INITIAL_REPUTATION = 500 + +# Defining template inputs. +USERNAME_TEMPLATE = "test_user_%s" +PASSWORD_TEMPLATE = "test_password_%s" +EMAIL_TEMPLATE = "test_user_%s@askbot.org" +TITLE_TEMPLATE = "Test question title No.%s" +TAGS_TEMPLATE = ["tag-%s-0", "tag-%s-1"] # len(TAGS_TEMPLATE) tags per question + +CONTENT_TEMPLATE = """Lorem lean startup ipsum product market fit customer + development acquihire technical cofounder. User engagement + **A/B** testing *shrink* a market venture capital pitch.""" + +ANSWER_TEMPLATE = """Accelerator photo sharing business school drop out ramen + hustle crush it revenue traction platforms.""" + +COMMENT_TEMPLATE = """Main differentiators business model micro economics + marketplace equity augmented reality human computer""" + + +class Command(NoArgsCommand): + + def print_if_verbose(self, text): + "Only print if user chooses verbose output" + if self.verbosity > 0: + print text + + def create_users(self): + "Create the users and return an array of created users" + users = [] + + # Keeping the created users in array - we will iterate over them + # several times, we don't want querying the model each and every time. + for i in range(NUM_USERS): + s_idx = str(i) + user = User.objects.create_user(USERNAME_TEMPLATE % s_idx, + EMAIL_TEMPLATE % s_idx) + user.set_password(PASSWORD_TEMPLATE % s_idx) + user.reputation = INITIAL_REPUTATION + user.save() + self.print_if_verbose("Created User '%s'" % user.username) + users.append(user) + return users + + + def create_questions(self, users): + "Create the questions and return the last one as active question" + + # Keeping the last active question entry for later use. Questions API + # might change, so we rely solely on User data entry API. + active_question = None + last_vote = False + # Each user posts a question + for user in users[:NUM_QUESTIONS]: + # Downvote/upvote the questions - It's reproducible, yet + # gives good randomized data + if not active_question is None: + if last_vote: + user.downvote(active_question) + self.print_if_verbose("%s downvoted a question"%( + user.username + )) + else: + user.upvote(active_question) + self.print_if_verbose("%s upvoted a question"%( + user.username + )) + last_vote = ~last_vote + + # len(TAGS_TEMPLATE) tags per question - each tag is different + tags = " ".join([t%user.id for t in TAGS_TEMPLATE]) + active_question = user.post_question( + title = TITLE_TEMPLATE % user.id, + body_text = CONTENT_TEMPLATE, + tags = tags, + ) + self.print_if_verbose("Created Question '%s' with tags: '%s'" % ( + active_question.title, tags,) + ) + return active_question + + + def create_answers(self, users, active_question): + "Create the answers for the active question, return the active answer" + active_answer = None + last_vote = False + # Now, fill the last added question with answers + for user in users[:NUM_ANSWERS]: + # We don't need to test for data validation, so ONLY users + # that aren't authors can post answer to the question + if not active_question.author is user: + # Downvote/upvote the answers - It's reproducible, yet + # gives good randomized data + if not active_answer is None: + if last_vote: + user.downvote(active_answer) + self.print_if_verbose("%s downvoted an answer"%( + user.username + )) + else: + user.upvote(active_answer) + self.print_if_verbose("%s upvoted an answer"%( + user.username + )) + last_vote = ~last_vote + + active_answer = user.post_answer( + question = active_question, + body_text = ANSWER_TEMPLATE, + follow = True + ) + self.print_if_verbose("%s posted an answer to the active question"%( + user.username + )) + # Upvote the active question + user.upvote(active_question) + # Follow the active question + user.follow_question(active_question) + self.print_if_verbose("%s followed the active question"%( + user.username) + ) + # Subscribe to the active question + user.subscribe_for_followed_question_alerts() + self.print_if_verbose("%s subscribed to followed questions"%( + user.username) + ) + return active_answer + + + def create_comments(self, users, active_question, active_answer): + """Create the comments for the active question and the active answer, + return 2 active comments - 1 question comment and 1 answer comment""" + + active_question_comment = None + active_answer_comment = None + + for user in users[:NUM_COMMENTS]: + active_question_comment = user.post_comment( + parent_post = active_question, + body_text = COMMENT_TEMPLATE + ) + self.print_if_verbose("%s posted a question comment"%user.username) + active_answer_comment = user.post_comment( + parent_post = active_answer, + body_text = COMMENT_TEMPLATE + ) + self.print_if_verbose("%s posted an answer comment"%user.username) + + # Upvote the active answer + user.upvote(active_answer) + + # Upvote active comments + if active_question_comment and active_answer_comment: + num_upvotees = NUM_COMMENTS - 1 + for user in users[:num_upvotees]: + user.upvote(active_question_comment) + user.upvote(active_answer_comment) + + return active_question_comment, active_answer_comment + + + def handle_noargs(self, **options): + + self.verbosity = int(options.get("verbosity", 1)) + + # Create Users + users = self.create_users() + + # Create Questions, vote for questions + active_question = self.create_questions(users) + + # Create Answers, vote for the answers, vote for the active question + # vote for the active answer + active_answer = self.create_answers(users, active_question) + + # Create Comments, vote for the active answer + active_question_comment, active_answer_comment = self.create_comments( + users, active_question, active_answer) + + # Edit the active question, answer and comments + active_question.author.edit_question( + question = active_question, + title = TITLE_TEMPLATE % "EDITED", + body_text = CONTENT_TEMPLATE, + revision_comment = "EDITED", + force = True + ) + self.print_if_verbose("User has edited the active question") + + active_answer.author.edit_answer( + answer = active_answer, + body_text = COMMENT_TEMPLATE, + force = True + ) + self.print_if_verbose("User has edited the active answer") + + active_answer_comment.user.edit_comment( + comment = active_answer_comment, + body_text = ANSWER_TEMPLATE + ) + self.print_if_verbose("User has edited the active answer comment") + + active_question_comment.user.edit_comment( + comment = active_question_comment, + body_text = ANSWER_TEMPLATE + ) + self.print_if_verbose("User has edited the active question comment") + + # Accept best answer + active_question.author.accept_best_answer( + answer = active_answer, + force = True, + ) + self.print_if_verbose("User has accepted a best answer") + + self.print_if_verbose("DONE") |