def post(self, poll_id): poll = Poll.query.get_or_404(poll_id) parser = reqparse.RequestParser() parser.add_argument("user_login", type=str) parser.add_argument("answers", type=dict, action="append") answers = parser.parse_args() user = User.query.filter_by(login=answers.user_login).first() print(answers, user) for question in poll.questions: id_in_json = False for answer in answers.answers: if question.id == answer['question_id']: id_in_json = True new_answer = Answer(user=user, question=question) if question.type == 'variants': if question.multiple_answers: for option in answer['multiple_answers']: new_answer.selected_option.append(PossibleAnswer.query.get_or_404(option)) else: new_answer.selected_option.append(PossibleAnswer.query.get(answer['select_answer'])) else: new_answer.answer = answer['text_answer'] db.session.add(new_answer) break if not id_in_json: return f"question {question.id} does not exist", 400 db.session.commit() return 'OK', 200
def answer(poll_id): poll = Poll.query.get_or_404(poll_id) if current_user != poll.creator: if current_user not in poll.access_participation: if current_user in poll.access_results: return redirect(url_for('results', poll_id=poll_id)) else: return redirect(url_for('view_poll', poll_id=poll_id)) if poll.participation_in(current_user) and poll.repeat_type == 'false': if current_user in poll.access_results or current_user == poll.creator: return redirect(url_for('results', poll_id=poll_id)) else: return redirect(url_for('my_answers', poll_id=poll_id)) form = PollAnswerForm() if request.method == 'POST': for form_answer in form.answers: question = Question.query.get(form_answer.question_id.data) new_answer = Answer(user=current_user, question=question) if question.type == 'variants': if question.multiple_answers: for option in form_answer.multi_selected_option.data: new_answer.selected_option.append(PossibleAnswer.query.get(option)) else: new_answer.selected_option.append(PossibleAnswer.query.get(form_answer.selected_option.data)) else: new_answer.answer = form_answer.text_answer.data db.session.add(new_answer) db.session.commit() return redirect(url_for('index')) for question in poll.questions: answer_form = form.answers.append_entry() answer_form.question_id.data = question.id if question.type == 'variants': choices = [] for option in question.possible_answers: choices.append((option.id, option.option)) if question.multiple_answers: answer_form.kind.data = 'multiSelect' answer_form.multi_selected_option.choices = choices else: answer_form.kind.data = 'select' answer_form.selected_option.choices = choices else: answer_form.kind.data = 'text' answer_form.label = question.question return render_template('new_answer.html', form=form)
def send_answers(): cur_week = int(request.form.get('week_id')) for assqt in Assqt.query.filter( and_(Assqt.week_id == cur_week, Assqt.user_id == int(session["userid"]))).all(): db.session.delete(assqt) _a = Assqt() _a.week_id = cur_week _a.user_id = int(session["userid"]) _a.checks_left = checks_by_user db.session.add(_a) for key in request.form: if key == "week_id": continue cur_task_id = int(key) cur_answer = request.form.get(key) _v = Answer.query.filter( and_(Answer.user_id == int(session["userid"]), Answer.task_id == cur_task_id)).first() if _v is None: _v = Answer(user=User.query.get(int(session["userid"])), task=Task.query.get(cur_task_id)) db.session.add(_v) _v.answer = cur_answer _a.answers.append(_v) _ac = Applic.query.filter( and_(Applic.user_id == int(session["userid"]), Applic.week_id == cur_week)).first() if _ac is None: _ac = Applic() _ac.user = User.query.get(int(session["userid"])) _ac.week = Week.query.get(cur_week) db.session.add(_ac) _ac.is_all_done = True db.session.commit() return redirect('/')