def load_posts(cli, size, fake): """ Load posts """ nb_avg_posts_in_topic = size * 10 cli.stdout.write(u"Nombres de messages à poster en moyenne dans un sujet : {}".format(nb_avg_posts_in_topic)) tps1 = time.time() nb_topics = Topic.objects.count() if nb_topics == 0: cli.stdout.write(u"Il n'y a aucun topic actuellement. " u"Vous devez rajouter les topics dans vos fixtures (topic)") else: topics = list(Topic.objects.all()) nb_users = User.objects.count() if nb_users == 0: cli.stdout.write(u"Il n'y a aucun membre actuellement. " u"Vous devez rajouter les membres dans vos fixtures (member)") else: profiles = list(Profile.objects.all()) for i in range(0, nb_topics): nb_posts = randint(0, nb_avg_posts_in_topic * 2) + 1 for j in range(1, nb_posts): post = PostFactory(topic=topics[i], author=profiles[j % nb_users].user, position=j + 1) post.text = fake.paragraph(nb_sentences=5, variable_nb_sentences=True) post.text_html = emarkdown(post.text) if int(nb_posts * 0.3) > 0: if j % int(nb_posts * 0.3) == 0: post.is_useful = True post.save() sys.stdout.write(" Topic {}/{} \tPost {}/{} \r".format(i + 1, nb_topics, j + 1, nb_posts)) sys.stdout.flush() tps2 = time.time() cli.stdout.write(u"\nFait en {} sec".format(tps2 - tps1))
def __generate_topic_and_post(cli, fake, nb_avg_posts_in_topic, nb_topics, nb_users, topics, tps1): profiles = list(Profile.objects.all()) for topic_index in range(0, nb_topics): nb_posts = randint(0, nb_avg_posts_in_topic * 2) + 1 for post_index in range(1, nb_posts): post = PostFactory(topic=topics[topic_index], author=profiles[post_index % nb_users].user, position=post_index + 1) post.text = fake.paragraph(nb_sentences=5, variable_nb_sentences=True) post.text_html = emarkdown(post.text) post.is_useful = int(nb_posts * 0.3) > 0 and post_index % int(nb_posts * 0.3) == 0 post.save() sys.stdout.write(' Topic {}/{} \tPost {}/{} \r'.format(topic_index + 1, nb_topics, post_index + 1, nb_posts)) sys.stdout.flush() tps2 = time.time() cli.stdout.write('\nFait en {} sec'.format(tps2 - tps1))