def route_index(): global update_views update_views = True question_headers = connection.return_questions_headers() if request.method == 'GET': questions = connection.read_questions('data/questions.csv') param = request.values.get('param') sort_ord = request.values.get('sort_ord') questions = util.make_compat_display(questions, 'not_textarea') questions_ordered = util.order_by_value(questions, param, sort_ord) if questions_ordered == None: update_views = True connection.write_questions('data/questions.csv', questions) questions_ordered = util.order_by_value(questions, 'submission_time', 'desc') return render_template('index.html', question_headers=question_headers, questions=questions_ordered) else: update_views = True connection.write_questions('data/questions.csv', questions_ordered) return render_template('index.html', question_headers=question_headers, questions=questions_ordered) elif request.metho == 'POST': questions = connection.read_questions('data/questions.csv') param = request.values.get('param') sort_ord = request.values.get('sort_ord') questions = util.make_compat_display(questions, 'not_textarea') questions_ordered = util.order_by_value(questions, param, sort_ord)
def route_edit_answer(question_id, answer_id): global update_views update_views = False edit_me = True answer_id = answer_id.rstrip('}') if request.method == 'POST': answer_id_conv = int(answer_id) question_id_conv = int(question_id) message = request.form['message'] message = util.make_compat_display(message, 'not_textarea') data_manager.update_answer(answer_id_conv, message) return redirect(url_for('route_question', question_id=question_id_conv)) elif request.method == 'GET': answer_id_conv = int(answer_id) question_id_conv = int(question_id) answer = data_manager.display_answers(question_id_conv) for i in range(len(answer)): answer[i] = dict(answer[i]) print('This is answer after conv', answer) for i in range(len(answer)): if answer[i]['id'] == answer_id_conv: answer_editable = answer[i] print('\n', 'This is the editable answer.', answer_editable) answer_editable['message'] = util.make_compat_display(answer_editable['message'], 'textarea') id_a = answer_editable['id'] id_q = answer_editable['question_id'] return render_template('add_answer.html', edit_me=edit_me, answer=answer_editable, id_q=id_q, id_a=id_a)
def route_edit_question(question_id): question_id_conv = int(question_id) global update_views update_views = False edit_me = True if request.method == 'POST': title = request.form['title'] title = title.rstrip('?') message = request.form['message'] message = util.make_compat_display(message, 'not_textarea') data_manager.update_question(question_id_conv, message, title) return redirect(url_for("route_index")) if request.method == 'GET': question = data_manager.display_question(question_id_conv) question[0]['message'] = util.make_compat_display(question[0]['message'], 'textarea') id_q = question[0]['id'] return render_template('add_question.html', edit_me=edit_me, question=question, id_q=id_q)
def route_edit_question(question_id): global update_views update_views = False edit_me = True questions = connection.read_questions('data/questions.csv') for elem in range(len(questions)): if str(questions[elem]['id']) == str(question_id): pos = elem question = questions[pos] if request.method == 'POST': title = request.form['title'] message = request.form['message'] message = util.make_compat_display(message, 'not_textarea') data_manager.edit_question(question_id, title, message) return redirect(url_for("route_index")) if request.method == 'GET': question['message'] = util.make_compat_display(question['message'], 'textarea') id_q = question['id'] return render_template('add_question.html', edit_me=edit_me, question=question, id_q=id_q)
def route_add_answer(question_id): global update_views update_views = False if request.method == 'POST': random_file_name = util.random_string() message = util.make_compat_display(request.form['message']) message = message.replace("'", "''") file = request.files['file'] filename = secure_filename(file.filename) if file and data_manager.allowed_file(file.filename): extension = filename[-4:] filename = str(random_file_name) + extension file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) data_manager.add_answer(question_id, message, filename) return redirect(url_for('route_question', question_id=question_id)) return render_template('add_answer.html', question_id=question_id)
def add_answer(question_id, message, file): answers_list = connection.read_answers(ANSWERS_FILE) if len(answers_list) == 0: id = 0 else: id = int(answers_list[-1]['id']) + 1 new_answer = { 'id': str(id), 'submission_time': util.today.strftime("%Y-%m-%d"), 'vote_number': '0', 'question_id': question_id, 'message': message, 'image': "../" + UPLOAD_FOLDER + "/" + str(file) } new_answer = util.make_compat_display([new_answer], 'not_textarea') answers_list.append(new_answer[0]) connection.write_answers(ANSWERS_FILE, answers_list)
def route_add_question(): global update_views if request.method == 'POST': random_file_name = util.random_string() title = request.form['title'] message = util.make_compat_display(request.form['message']) file = request.files['file'] filename = secure_filename(file.filename) if file and data_manager.allowed_file(file.filename): # filename = secure_filename(file.filename) extension = filename[-4:] filename = str(random_file_name) + extension file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) data_manager.add_question(title, message, filename) update_views = False return redirect(url_for("route_index")) else: return render_template('add_question.html')
def add_question(title, message, file): date = util.datetime.today() questions_list = connection.read_questions(QUESTIONS_FILE) questions_list = util.order_by_value(questions_list, 'id', 'asc') if len(questions_list) == 0: id = 0 else: id = int(questions_list[-1]['id']) + 1 print('id: ', id) new_question = { 'id': str(id), 'submission_time': date.strftime("%Y-%m-%d-%H:%M:%S"), 'view_number': '0', 'vote_number': '0', 'title': title, 'message': message, 'image': "../" + UPLOAD_FOLDER + "/" + str(file) } new_question = util.make_compat_display([new_question], 'not_textarea') questions_list.append(new_question[0]) connection.write_questions(QUESTIONS_FILE, questions_list)
def edit_question(question_id, title, message): all_questions = connection.read_questions(QUESTIONS_FILE) for elem in range(len(all_questions)): if str(all_questions[elem]['id']) == str(question_id): pos = elem submission_time = str(all_questions[elem]['submission_time']) view_number = str(all_questions[elem]['view_number']) vote_number = str(all_questions[elem]['vote_number']) img = str(all_questions[elem]['image']) q_id = str(all_questions[elem]['id']) all_questions.pop(pos) question_edit = { 'id': q_id, 'submission_time': submission_time, 'view_number': view_number, 'vote_number': vote_number, 'title': title, 'message': message, 'image': img } question_edit = util.make_compat_display([question_edit], 'not_textarea') all_questions.append(question_edit[0]) connection.write_questions(QUESTIONS_FILE, all_questions)