def handle(self, *args, **options): usernames = ['Vasya', 'Petya', 'Vova', 'Gena', 'Dima', 'Sasha', 'Kolya'] tagsnames = ['Google', 'Yandex', 'Mail', 'MySql', 'Postgre', 'MongoDB', 'InnoDB'] questions_count = 3 answers_max = 5 users = [] for name in usernames: a = User(username=name, password='******') try: a.save() users.append(a) except IntegrityError: # Уже такой есть users.append(User.objects.filter(username=name)) tags = [] for tagname in tagsnames: t = Tag(word=tagname) try: t.save() tags.append(t) except IntegrityError: # Уже такой есть tags.append(Tag.objects.filter(word=tagname)) for num in range(questions_count): q = Question( header='test title' + str(num), content='test content' + str(num), author=users[random.randint(0, len(users) - 1)].first() ) q.save() q.tags.add(tags[random.randint(0, len(tags) - 1)]) q.tags.add(tags[random.randint(0, len(tags) - 1)]) answers_count = random.randint(0, answers_max) for i in range(0, answers_count): a = Answer( content='test answer' + str(num), author=users[random.randint(0, len(users) - 1)].first(), question=q ) a.save()
def TagGenerator(): filename = "/home/nikita/web/ask_chizhikov/polls/management/commands/tags.txt" f = open(filename, 'r') for line in f: t = Tag(title=line) t.save()
def handle(self, *args, **options): start_time = datetime.now() #Заполнение юзеров for user_id in range(1, users_count + 1): user = CustomUser( avatar='default.jpg', password=make_password("password"), last_login=self.random_date(), is_superuser=False, username="******" % (user_id), first_name="", last_name="", email="", is_staff=False, is_active=True, date_joined=datetime(2015, 1, 1, 1, 0, 0, 0, pytz.UTC) ) self.stdout.write("User#%d" % user_id) user.save() # Заполнение вопросов for question_id in range(1, questions_count + 1): text = '' for i in get_paragraphs(randint(1, 4)): text += i question = Question( title=get_sentence(), content=text, author_id=randint(1, users_count), created=self.random_date(), rating=0 ) self.stdout.write("Question#%d" % question_id) question.save() # Заполнение ответов for answer_id in range(1, answers_count + 1): text = '' for i in get_paragraphs(randint(1, 2)): text += i answer = Answer( id=answer_id, content=text, author_id=randint(1, users_count), created=self.random_date(), question_id=randint(1, questions_count), rating=0 ) self.stdout.write("Answer#%d" % answer_id) answer.save() # Заполнение тэгов words = open('polls/words', 'r') for tag_id in range(1, tags_count + 1): tag = Tag(title=words.readline()[:-1]) tag.save() words.close() # Заполнение лайков вопросов for like_id in range(1, votes_count): l_value = randint(0, 1) if l_value == 0: l_value = -1 rand_user = randint(1, users_count) rand_question = randint(1, questions_count) try: like = LikeForQuestion(user_id=rand_user, value=l_value, question_related_id=rand_question) like.save() self.stdout.write("Like#%d" % like_id) quest = Question.objects.get(id=rand_question) quest.rating = str(l_value + quest.rating) quest.save() except django.db.IntegrityError: pass # Для ответов for like_id in range(1, votes_count): l_value = randint(0, 1) if l_value == 0: l_value = -1 rand_user = randint(1, users_count) rand_question = randint(1, questions_count) try: like = LikeForAnswers(user_id=rand_user, value=l_value, answer_related_id=rand_question) like.save() self.stdout.write("Like#%d" % like_id) quest = Answer.objects.get(id=rand_question) quest.rating = str(l_value + quest.rating) quest.save() except django.db.IntegrityError: pass # Добавление тегов к вопросам for question_id in range(1, questions_count + 1): question = Question.objects.get(pk=question_id) self.stdout.write("Linking tags#%d" % question_id) for i in range(1, 4): question.tags.add(Tag.objects.get(pk=randint(1, tags_count))) end_time = datetime.now() self.stdout.write( 'Database filled successfully' + str(end_time - start_time))
def handle(self, *args, **options): start_time = datetime.now() #Заполнение юзеров for user_id in range(1, users_count + 1): user = CustomUser(avatar='default.jpg', password=make_password("password"), last_login=self.random_date(), is_superuser=False, username="******" % (user_id), first_name="", last_name="", email="", is_staff=False, is_active=True, date_joined=datetime(2015, 1, 1, 1, 0, 0, 0, pytz.UTC)) self.stdout.write("User#%d" % user_id) user.save() # Заполнение вопросов for question_id in range(1, questions_count + 1): text = '' for i in get_paragraphs(randint(1, 4)): text += i question = Question(title=get_sentence(), content=text, author_id=randint(1, users_count), created=self.random_date(), rating=0) self.stdout.write("Question#%d" % question_id) question.save() # Заполнение ответов for answer_id in range(1, answers_count + 1): text = '' for i in get_paragraphs(randint(1, 2)): text += i answer = Answer(id=answer_id, content=text, author_id=randint(1, users_count), created=self.random_date(), question_id=randint(1, questions_count), rating=0) self.stdout.write("Answer#%d" % answer_id) answer.save() # Заполнение тэгов words = open('polls/words', 'r') for tag_id in range(1, tags_count + 1): tag = Tag(title=words.readline()[:-1]) tag.save() words.close() # Заполнение лайков вопросов for like_id in range(1, votes_count): l_value = randint(0, 1) if l_value == 0: l_value = -1 rand_user = randint(1, users_count) rand_question = randint(1, questions_count) try: like = LikeForQuestion(user_id=rand_user, value=l_value, question_related_id=rand_question) like.save() self.stdout.write("Like#%d" % like_id) quest = Question.objects.get(id=rand_question) quest.rating = str(l_value + quest.rating) quest.save() except django.db.IntegrityError: pass # Для ответов for like_id in range(1, votes_count): l_value = randint(0, 1) if l_value == 0: l_value = -1 rand_user = randint(1, users_count) rand_question = randint(1, questions_count) try: like = LikeForAnswers(user_id=rand_user, value=l_value, answer_related_id=rand_question) like.save() self.stdout.write("Like#%d" % like_id) quest = Answer.objects.get(id=rand_question) quest.rating = str(l_value + quest.rating) quest.save() except django.db.IntegrityError: pass # Добавление тегов к вопросам for question_id in range(1, questions_count + 1): question = Question.objects.get(pk=question_id) self.stdout.write("Linking tags#%d" % question_id) for i in range(1, 4): question.tags.add(Tag.objects.get(pk=randint(1, tags_count))) end_time = datetime.now() self.stdout.write('Database filled successfully' + str(end_time - start_time))
def handle(self, *args, **options): start_time = datetime.now() # Заполнение юзеров for user_id in range(1, users_count + 1): user = CustomUser( avatar='http://lorempixel.com/10%s/10%s/cats' % ( user_id % 10, user_id % 10), password=make_password("password"), last_login=self.random_date(), is_superuser=False, username="******" % (user_id), first_name="", last_name="", email="", is_staff=False, is_active=True, date_joined=datetime(2015, 1, 1, 1, 0, 0, 0, pytz.UTC) ) self.stdout.write("User#%d" % user_id) user.save() # Заполнение вопросов for question_id in range(1, questions_count + 1): text = '' for i in get_paragraphs(randint(1, 4)): text += i question = Question( title=get_sentence(), content=text, author_id=randint(1, users_count), created=self.random_date(), rating=0 ) self.stdout.write("Question#%d" % question_id) question.save() # Заполнение ответов for answer_id in range(1, answers_count + 1): text = '' for i in get_paragraphs(randint(1, 2)): text += i answer = Answer( id=answer_id, content=text, author_id=randint(1, users_count), created=self.random_date(), question_id=randint(1, questions_count), rating=0 ) self.stdout.write("Answer#%d" % answer_id) answer.save() # Заполнение тэгов words = open('polls/words', 'r') for tag_id in range(1, tags_count + 1): tag = Tag(title=words.readline()[:-1]) tag.save() words.close() # Заполнение лайков for like_id in range(1, votes_count): l_value = randint(0, 1) if l_value == 0: l_value = -1 like = Like(author_id=randint(1, users_count), value=l_value) like.save() self.stdout.write("Like#%d" % like_id) # Добавление тегов к вопросам for question_id in range(1, questions_count + 1): question = Question.objects.get(pk=question_id) self.stdout.write("Linking tags#%d" % question_id) for i in range(1, 4): question.tags.add(Tag.objects.get(pk=randint(1, tags_count))) for q_like in range(1, votes_count / 2): question = Question.objects.get(pk=randint(1, questions_count)) like = Like.objects.get(pk=q_like) question.likes.add(like) question.rating += like.value question.save() self.stdout.write("Linking question likes#%d" % q_like) for a_like in range(votes_count / 2 + 1, votes_count): answer = Answer.objects.get(pk=randint(1, answers_count)) like = Like.objects.get(pk=a_like) answer.likes.add(like) answer.rating += like.value answer.save() self.stdout.write( "Linking answer likes#%d, like value=%d" % (a_like, like.value)) end_time = datetime.now() self.stdout.write( 'Database filled successfully' + str(end_time - start_time))
def handle(self, *args, **options): start_time = datetime.now() # for user_id in range(1, users_count + 1): user = CustomUser(avatar='http://lorempixel.com/10%s/10%s/people' % (user_id % 10, user_id % 10), password=make_password("password"), last_login=self.random_date(), is_superuser=False, username="******" % (user_id), first_name="", last_name="", email="", is_staff=False, is_active=True, date_joined=datetime(2014, 1, 1, 1, 0, 0, 0, pytz.UTC)) self.stdout.write("User#%d" % user_id) user.save() # for question_id in range(1, questions_count + 1): text = '' for i in get_paragraphs(randint(1, 4)): text += i question = Question(title=get_sentence(), content=text, author_id=randint(1, users_count), created=self.random_date(), rating=0, counts=0) self.stdout.write("Question#%d" % question_id) question.save() # for answer_id in range(1, answers_count + 1): text = '' for i in get_paragraphs(randint(1, 2)): text += i """ answer = Answer( id=answer_id, content=text, author_id=randint(1, users_count), created=self.random_date(), question_id=randint(1, questions_count), rating=0 ) self.stdout.write("Answer#%d" % answer_id) answer.save()""" answer = Answer(id=answer_id, content=text, author_id=randint(1, users_count), created=self.random_date(), question_id=randint(1, questions_count), rating=0) self.stdout.write("Answer#%d" % answer_id) answer.save() for question_id in range(1, questions_count + 1): question = Question.objects.get(pk=question_id) answer_ = Answer.objects.filter(question_id=question_id) i = 0 for elem in answer_: i = i + 1 question.counts = i question.save() answer.save() # words = open('polls/words', 'r') for tag_id in range(1, tags_count + 1): tag = Tag(title=words.readline()[:-1]) tag.save() words.close() # for like_id in range(1, votes_count + 1): l_value = randint(0, 1) if l_value == 0: l_value = -1 like = Like(author_id=randint(1, users_count), value=l_value) like.save() self.stdout.write("Like#%d" % like_id) # for question_id in range(1, questions_count + 1): question = Question.objects.get(pk=question_id) self.stdout.write("Linking tags#%d" % question_id) #for i in range(1, ): question.tags.add(Tag.objects.get(pk=randint(1, tags_count))) for q_like in range(1, votes_count / 2): question = Question.objects.get(pk=randint(1, questions_count)) for i in range(1, q_like): like = Like.objects.get(pk=i) question.likes.add(like) question.rating += like.value question.save() self.stdout.write("Linking question likes#%d" % q_like) for a_like in range(votes_count / 2 + 1, votes_count): answer = Answer.objects.get(pk=randint(1, answers_count)) for i in range(1, q_like): like = Like.objects.get(pk=i) answer.likes.add(like) answer.rating += like.value answer.save() self.stdout.write("Linking answer likes#%d" % a_like) end_time = datetime.now() self.stdout.write('Database filled successfully' + str(end_time - start_time))