def answerPost(request, feedback_id): if request.method == 'POST': try: feedback = get_object_or_404(Feedback, feedback_id=feedback_id) received_data = request.body.decode("utf-8") body = json.loads(received_data) answer_id = body["answer_id"] answer_question = body["answer_question"] answer_before = body["answer_before"] answer_after = body["answer_after"] answer_feedback = feedback answer = Answer(answer_id=answer_id, answer_question=answer_question, answer_before=answer_before, answer_after=answer_after, answer_feedback=answer_feedback) # Answer model save answer.save() return HttpResponse("it was Answer post request!!__: " + json.dumps(body)) except Exception as e: return HttpResponse("Post data failed: " + str(e)) return HttpResponse("This is the POST api!!")
def post(self, request, *args, **kwargs): form = self.form_class(request.POST) if form.is_valid(): obj = form.save(commit=False) obj.creator = self.request.user obj.image = request.FILES.get('image') obj.save() # Without this next line the tags won't be saved. form.save_m2m() for f in request.FILES: print f for key, val in request.POST.iteritems(): if 'answer_txt_' in key: a = Answer(poll=obj, answer=val) a.save() return redirect(obj.get_absolute_url()) else: context = { 'title': _('Create new poll'), 'location': form.cleaned_data['location'], 'links': links['polls'], 'appname': 'poll-create', 'form': PollForm(request.POST), } return render(request, self.template_name, context)
def create(self, validated_data): request = self.context.get('request') if not request.session.session_key: request.session.save() session_id = request.session.session_key poll_pk = self.context.get('view').kwargs.get('poll_pk') question_pk = self.context.get('view').kwargs.get('question_pk') poll = get_object_or_404(Poll, pk=poll_pk) question = get_object_or_404(Question, pk=question_pk, poll=poll) date_answer = None if question.question_type == 'date_input': date_answer = validated_data['date_answer'] answer = Answer(session_id=session_id, question=question, poll=poll, date_answer=date_answer, numeric_answer=None, choice=None) try: answer.save() except: answer = get_object_or_404(Answer, session_id=session_id, question=question, poll=poll) answer.date_answer = date_answer answer.save() return answer
def randomAnswerGenerator(users, questions): user = User.objects.get(id=random.randint(1, users)) q = Question.objects.get(id=random.randint(1, questions)) filename = "/home/nikita/web/ask_chizhikov/polls/management/commands/" + "a" + str( random.randint(1, 10)) + ".txt" f = open(filename, 'r') text = "" for line in f: text += line a = Answer(author=user, text=text, question=q) a.save()
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 create(self, validated_data): request = self.context.get('request') if not request.session.session_key: request.session.save() session_id = request.session.session_key poll_pk = self.context.get('view').kwargs.get('poll_pk') question_pk = self.context.get('view').kwargs.get('question_pk') choice_pk = self.context.get('view').kwargs.get('choice_pk') poll = get_object_or_404(Poll, pk=poll_pk) question = get_object_or_404(Question, pk=question_pk, poll=poll) choice = None if question.question_type == 'multiple_choice': choice = get_object_or_404(Choice, pk=choice_pk, question=question) answer = Answer( session_id=session_id, question=question, poll=poll, choice=choice, numeric_answer=None, date_answer=None, ) else: raise serializers.ValidationError( "This is not a valid type question.") try: answer.save() except: answer = get_object_or_404(Answer, session_id=session_id, question=question, poll=poll) answer.choice = choice answer.save() return answer
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/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))