summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDejan Noveski <dr.mote@gmail.com>2011-11-24 00:35:04 +0100
committerDejan Noveski <dr.mote@gmail.com>2011-11-24 00:35:04 +0100
commit02df54afdfa3c6cc19f66ca2ac29746d4c754aa2 (patch)
treed36a012c221a34fcee5246db92f120dce96f1751
parent080bfe4d92a2887eb5c70a834ba1486d1a4f59aa (diff)
downloadaskbot-02df54afdfa3c6cc19f66ca2ac29746d4c754aa2.tar.gz
askbot-02df54afdfa3c6cc19f66ca2ac29746d4c754aa2.tar.bz2
askbot-02df54afdfa3c6cc19f66ca2ac29746d4c754aa2.zip
Test data filler management command - fill_test_content_db added.
-rw-r--r--askbot/management/commands/fill_test_content_db.py223
1 files changed, 223 insertions, 0 deletions
diff --git a/askbot/management/commands/fill_test_content_db.py b/askbot/management/commands/fill_test_content_db.py
new file mode 100644
index 00000000..277c20e3
--- /dev/null
+++ b/askbot/management/commands/fill_test_content_db.py
@@ -0,0 +1,223 @@
+from django.core.management.base import NoArgsCommand
+from django.contrib.auth.models import User
+from askbot.models import User
+from askbot.models.repute import Repute
+
+
+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")
+
+ self.print_if_verbose("DONE") \ No newline at end of file