def handle(self, *args, **options): self.thread_count = options['thread_count'] self.root_comments = options['root_comments'] self. random_usernames = [self.get_random_username() for _ in range(100)] for index, _ in enumerate(range(self.thread_count)): print("Thread {} out of {}".format(str(index), self.thread_count)) selftext = self.get_random_sentence() title = self.get_random_sentence(max_words=100, max_word_len=10) author = self.get_or_create_author(choice(self.random_usernames)) ups = randint(0, 1000) url = None downs = int(ups) / 2 comments = 0 submission = Submission(author=author, title=title, url=url, text=selftext, ups=int(ups), downs=downs, score=ups - downs, comment_count=comments) submission.generate_html() submission.save() for _ in range(self.root_comments): print("Adding thread comments...") comment_author = self.get_or_create_author(choice(self.random_usernames)) raw_text = self.get_random_sentence(max_words=100) new_comment = Comment.create(comment_author, raw_text, submission) new_comment.save() another_child = choice([True, False]) while another_child: self.add_replies(new_comment) another_child = choice([True, False])
def test_save_discuss_url(url): sub = Submission(title="Test", external_url=url, discussion_url='/r/python/', submitter='Edu', punctuation=1, creation_date=timezone.now(), number_of_comments=1) sub.save() assert "https://www.reddit.com" in sub.discussion_url
def test_save_non_external_url(url, expected): sub = Submission(title="Test", external_url=url, discussion_url='/r/python/', submitter='Edu', punctuation=1, creation_date=timezone.now(), number_of_comments=1) sub.save() assert sub.external_url == expected
def test_save(): sub = Submission(title="Test", external_url="http://www.reddit.com", discussion_url='/r/python/', submitter='Edu', punctuation=1, creation_date=timezone.now(), number_of_comments=1) sub.save() assert sub.title == "Test" assert sub.submitter == "Edu"
def handle(self, *args, **options): self.thread_count = options['thread_count'] self.root_comments = options['root_comments'] self.random_usernames = [ self.get_random_username() for _ in range(100) ] for index, _ in enumerate(range(self.thread_count)): print("Thread {} out of {}".format(str(index), self.thread_count)) selftext = self.get_random_sentence() title = self.get_random_sentence(max_words=100, max_word_len=10) author = self.get_or_create_author(choice(self.random_usernames)) ups = randint(0, 1000) url = None downs = int(ups) / 2 comments = 0 submission = Submission(author=author, title=title, url=url, text=selftext, ups=int(ups), downs=downs, score=ups - downs, comment_count=comments) submission.generate_html() submission.save() for _ in range(self.root_comments): print("Adding thread comments...") comment_author = self.get_or_create_author( choice(self.random_usernames)) raw_text = self.get_random_sentence(max_words=100) new_comment = Comment.create(comment_author, raw_text, submission) new_comment.save() another_child = choice([True, False]) while another_child: self.add_replies(new_comment) another_child = choice([True, False])
def test_data(request): # pragma: no cover """ Quick and dirty way to create 10 random submissions random comments each and up to 100 users with usernames (their passwords are same as usernames) Should be removed in production. """ get_page = """ <form action="/populate/" method="POST"> Threads: <input type="number" name="threads" value=10></input> Root comments: <input type="number" name="comments" value=10></input> <button type="submit">Create</button> </form> """ if not request.user.is_authenticated() or not request.user.is_staff: return Http404() if request.method == "GET": return HttpResponse(get_page) thread_count = int(request.POST.get('threads', 10)) root_comments = int(request.POST.get('comments', 10)) from random import choice, randint from string import letters def get_random_username(length=6): return ''.join(choice(letters) for _ in range(length)) random_usernames = [get_random_username() for _ in range(100)] def get_random_sentence(min_words=3, max_words=50, min_word_len=3, max_word_len=15): sentence = '' for _ in range(0, randint(min_words, max_words)): sentence += ''.join(choice(letters) for i in range(randint(min_word_len, max_word_len))) sentence += ' ' return sentence def get_or_create_author(username): try: user = User.objects.get(username=username) author = RedditUser.objects.get(user=user) except (User.DoesNotExist, RedditUser.DoesNotExist): print "Creating user {}".format(username) new_author = User(username=username) new_author.set_password(username) new_author.save() author = RedditUser(user=new_author) author.save() return author def add_replies(root_comment, depth=1): print "Adding comment replies..." if depth > 5: return comment_author = get_or_create_author(choice(random_usernames)) raw_text = get_random_sentence() new_comment = Comment.create(comment_author, raw_text, root_comment) new_comment.save() if choice([True, False]): add_replies(new_comment, depth + 1) for _ in range(thread_count): print "Creating new submission." selftext = get_random_sentence() title = get_random_sentence(max_words=100, max_word_len=10) author = get_or_create_author(choice(random_usernames)) ups = randint(0, 1000) url = None downs = int(ups) / 2 comments = 0 submission = Submission(author=author, title=title, url=url, text=selftext, ups=int(ups), downs=downs, score=ups - downs, comment_count=comments) submission.generate_html() submission.save() for _ in range(root_comments): comment_author = get_or_create_author(choice(random_usernames)) raw_text = get_random_sentence(max_words=100) new_comment = Comment.create(comment_author, raw_text, submission) new_comment.save() another_child = choice([True, False]) while another_child: add_replies(new_comment) another_child = choice([True, False]) return redirect('/')
def test_data(request): # pragma: no cover """ Quick and dirty way to create 10 random submissions random comments each and up to 100 users with usernames (their passwords are same as usernames) Should be removed in production. """ get_page = """ <form action="/populate/" method="POST"> Threads: <input type="number" name="threads" value=10></input> Root comments: <input type="number" name="comments" value=10></input> <button type="submit">Create</button> </form> """ if not request.user.is_authenticated() or not request.user.is_staff: return Http404() if request.method == "GET": return HttpResponse(get_page) thread_count = int(request.POST.get('threads', 10)) root_comments = int(request.POST.get('comments', 10)) from random import choice, randint from string import letters def get_random_username(length=6): return ''.join(choice(letters) for _ in range(length)) random_usernames = [get_random_username() for _ in range(100)] def get_random_sentence(min_words=3, max_words=50, min_word_len=3, max_word_len=15): sentence = '' for _ in range(0, randint(min_words, max_words)): sentence += ''.join( choice(letters) for i in range(randint(min_word_len, max_word_len))) sentence += ' ' return sentence def get_or_create_author(username): try: user = User.objects.get(username=username) author = RedditUser.objects.get(user=user) except (User.DoesNotExist, RedditUser.DoesNotExist): print "Creating user {}".format(username) new_author = User(username=username) new_author.set_password(username) new_author.save() author = RedditUser(user=new_author) author.save() return author def add_replies(root_comment, depth=1): print "Adding comment replies..." if depth > 5: return comment_author = get_or_create_author(choice(random_usernames)) raw_text = get_random_sentence() new_comment = Comment.create(comment_author, raw_text, root_comment) new_comment.save() if choice([True, False]): add_replies(new_comment, depth + 1) for _ in range(thread_count): print "Creating new submission." selftext = get_random_sentence() title = get_random_sentence(max_words=100, max_word_len=10) author = get_or_create_author(choice(random_usernames)) ups = randint(0, 1000) url = None downs = int(ups) / 2 comments = 0 submission = Submission(author=author, title=title, url=url, text=selftext, ups=int(ups), downs=downs, score=ups - downs, comment_count=comments) submission.generate_html() submission.save() for _ in range(root_comments): comment_author = get_or_create_author(choice(random_usernames)) raw_text = get_random_sentence(max_words=100) new_comment = Comment.create(comment_author, raw_text, submission) new_comment.save() another_child = choice([True, False]) while another_child: add_replies(new_comment) another_child = choice([True, False]) return redirect('/')