def answer_new(question_id): answered = Answer.query.filter_by(author=current_user, question_id=question_id).first() if answered: flash( "already answered the question, please reply to your original answer..." ) return redirect( url_for('main.question_detail', question_id=question_id)) form = AnswerForm() if form.validate_on_submit(): answer = form.answer.data a = Answer(answer=answer, author=current_user, question_id=question_id) db.session.add(a) db.session.commit() flash("done answering the question...") return redirect( url_for('main.question_detail', question_id=question_id) + "#answer") #return redirect(url_for('main/answer_new', form=form)) return render_template('main/answer_new.html', form=form)
def question(id, respondent_id): form = Form() options_1_question = [] # READ ALL OPTIONS FROM DATABASE MATCHING QUESTION ID for option in options_objects_list: if option.question_id == id: options_1_question.append(option) # UPDATE OPTIONS (RADIOBUTTONS) form.question_options.choices = [(option.number, option.option_text) for option in options_1_question] form.question_options.coerce = int if form.validate_on_submit(): # WHEN SUBMITTED -> SAVE TO DATABASE, GO TO THE NEXT QUESTION answer = Answer(respondent_id=respondent_id, question_id=id, option_number=form.question_options.data) db.session.add(answer) db.session.commit() return redirect( url_for('question', id=id + 1, respondent_id=respondent_id)) # IF THE LAST QUESTION -> END if id - 1 >= len(questions_objects_list): return redirect(url_for('end', respondent_id=respondent_id)) return render_template('form.html', form=form, question=questions_objects_list[id - 1].text)
def make_answer(request, pk): question = Question.objects.get(pk=pk) if request.method == "POST": answer = Answer(question=question) answer.text = request.POST.get("answer") answer.save() question.answered = True question.save() return redirect("site:questions") else: if request.user == question.to_user: context = {"question": question} context.update(csrf(request)) return render(request, "website/make_answer.html", context) else: context = {"error": "Этот вопрос поставлен не вам"} return render(request, "website/make_answer.html", context)
def make_answer(request, pk): question = Question.objects.get(pk=pk) if request.method == 'POST': answer = Answer(question=question) answer.text = request.POST.get('answer') answer.save() question.answered = True question.save() return redirect('site:questions') else: if request.user == question.to_user: context = {'question': question} context.update(csrf(request)) return render(request, 'website/make_answer.html', context) else: context = {'error': 'Этот вопрос поставлен не вам'} return render(request, 'website/make_answer.html', context)
def question_answer(request): if request.method == 'POST': form = AnswerQuesitionForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data qid = cleaned_data['question'] body = cleaned_data['body'] question = get_object_or_404(Question, id=qid) answer = Answer() answer.uid = request.user.id answer.question = question answer.body = body.encode('unicode_escape') answer.save() if question.uid != request.user.id: notification = Notification() notification.uid = question.uid notification.pid = request.user.id notification.qid = qid notification.aid = answer.id notification.save() user = User.objects.get(id=question.uid) # Sending email when an answer is posted subject = 'Question has been answered' message = """ Dear {0}<br><br> Your question titled <b>"{1}"</b> has been answered.<br> Link: {2}<br><br> Regards,<br> Spoken Tutorial Forums """.format( user.username, question.title, 'http://forums.spoken-tutorial.org/question/' + str(question.id) + "#answer" + str(answer.id) ) email = EmailMultiAlternatives( subject, '', 'forums', [user.email], headers={"Content-type": "text/html;charset=iso-8859-1"} ) email.attach_alternative(message, "text/html") email.send(fail_silently=True) # End of email send return HttpResponseRedirect('/question/' + str(qid) + "#answer" + str(answer.id)) return HttpResponseRedirect('/')
def question_answer(request): if request.method == 'POST': form = AnswerQuesitionForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data qid = cleaned_data['question'] body = cleaned_data['body'] question = get_object_or_404(Question, id=qid) answer = Answer() answer.uid = request.user.id answer.question = question answer.body = body.encode('unicode_escape') answer.save() if question.uid != request.user.id: notification = Notification() notification.uid = question.uid notification.pid = request.user.id notification.qid = qid notification.aid = answer.id notification.save() user = User.objects.get(id=question.uid) # Sending email when an answer is posted subject = 'Question has been answered' message = """ Dear {0}<br><br> Your question titled <b>"{1}"</b> has been answered.<br> Link: {2}<br><br> Regards,<br> Spoken Tutorial Forums """.format( user.username, question.title, 'http://forums.spoken-tutorial.org/question/' + str(question.id) + "#answer" + str(answer.id) ) email = EmailMultiAlternatives( subject,'', 'forums', [user.email], headers={"Content-type":"text/html;charset=iso-8859-1"} ) email.attach_alternative(message, "text/html") email.send(fail_silently=True) # End of email send return HttpResponseRedirect('/question/'+ str(qid) + "#answer" + str(answer.id)) return HttpResponseRedirect('/')
db.session.add(q15) db.session.add(o15) db.session.add(o15_1) db.session.add(q16) db.session.add(o16) db.session.add(o17_1) db.session.add(q17) db.session.add(o17) db.session.add(o17_1) db.session.add(q18) db.session.add(o18) db.session.add(o18_1) db.session.commit() # DYNAMIC DATA (EXAMPLE) r1 = Respondent(age=22, gender=1, med_education=True, place=1) r2 = Respondent(age=20, gender=2, med_education=False, place=2) db.session.add(r1) db.session.add(r2) db.session.commit() a1 = Answer(respondent_id=r1.id, question_id=q1.id, option_number=o1_1.number) a2 = Answer(respondent_id=r2.id, question_id=q1.id, option_number=o1_1.number) db.session.add(a1) db.session.add(a2) db.session.commit()
def question_answer(request,qid): dict_context = {} if request.method == 'POST': form = AnswerQuestionForm(request.POST) question = get_object_or_404(Question, id=qid) answers = question.answer_set.all() answer = Answer() answer.uid = request.user.id if form.is_valid(): cleaned_data = form.cleaned_data qid = cleaned_data['question'] body = cleaned_data['body'] answer.question = question answer.body = body.encode('unicode_escape') answer.save() # if user_id of question not matches to user_id of answer that # question , no if question.user_id != request.user.id: notification = Notification() notification.uid = question.user_id notification.pid = request.user.id notification.qid = qid notification.aid = answer.id notification.save() user = User.objects.get(id=question.user_id) # Sending email when an answer is posted subject = 'Question has been answered' message = """ Dear {0}<br><br> Your question titled <b>"{1}"</b> has been answered.<br> Link: {2}<br><br> Regards,<br> Fossee Forums """.format( user.username, question.title, 'http://forums.fossee.in/question/' + str(question.id) + "#answer" + str(answer.id) ) email = EmailMultiAlternatives( subject,'', 'forums', [user.email], headers={"Content-type":"text/html;charset=iso-8859-1"} ) email.attach_alternative(message, "text/html") email.send(fail_silently=True) # End of email send return HttpResponseRedirect('/question/'+ str(qid) + "#answer" + str(answer.id)) else: dict_context = { 'question':question, 'answers': answers, 'form': form } return render(request, 'website/templates/get-question.html', dict_context) return HttpResponseRedirect('/')