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_question(question_id): global update_views questions = connection.read_questions('data/questions.csv') print('update_views', update_views, type(update_views)) connection.write_questions('data/questions.csv', questions) questions = connection.read_questions('data/questions.csv') for elem in range(len(questions)): for key in questions[elem].keys(): if str(question_id) == str(questions[elem]['id']): pos = elem print(pos) if update_views == True and key == 'view_number': print('sum to be assigned:', str(int(questions[elem]['view_number']) + 1)) questions[elem]['view_number'] = str( int(questions[elem]['view_number']) + 1) connection.write_questions('data/questions.csv', questions) questions = connection.read_questions('data/questions.csv') questions = questions[pos] answers = connection.read_answers('data/answers.csv') update_views = False return render_template('question.html', questions=questions, answers=answers, question_id=question_id)
def delete_question(question_id): answers_list = connection.read_answers(ANSWERS_FILE) questions_list = connection.read_questions(QUESTIONS_FILE) for question in questions_list: if '../static/img/' != question['image']: if int(question['id']) == int(question_id): image_path = question['image'][3:] os.remove(image_path) for answers in answers_list: if '../static/img/' != answers['image']: if int(answers['question_id']) == int(question_id): image_path = answers['image'][3:] os.remove(image_path) data = [ element for element in questions_list if int(element['id']) != int(question_id) ] count = 0 for d in data: d['id'] = count count += 1 connection.write_questions(QUESTIONS_FILE, data) data_answers = [ element for element in answers_list if int(element['question_id']) != int(question_id) ] for elem in data_answers: if int(elem['question_id']) > int(question_id): elem['question_id'] = int(elem['question_id']) - 1 connection.write_answers(ANSWERS_FILE, data_answers) return connection.read_questions('data/questions.csv')
def vote_question(question_id, option): questions_list = connection.read_questions(QUESTIONS_FILE) for elem in range(len(questions_list)): for key in questions_list[elem].keys(): if str(questions_list[elem]['id']) == str(question_id): pos = elem vote_number = int(questions_list[pos]['vote_number']) if option == 'vote_up': questions_list[pos]['vote_number'] = str(vote_number + 1) connection.write_questions(QUESTIONS_FILE, questions_list) elif option == 'vote_down': questions_list[pos]['vote_number'] = str(vote_number - 1) connection.write_questions(QUESTIONS_FILE, questions_list)
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 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 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)
def question_list_size(): questions_list = connection.read_questions(data_manager.QUESTIONS_FILE) return len(questions_list)