def get_question_details(question_id): """ Input: question_id = string Output: render_template with different attributes This function collects the questions'/answers' details for the table creating on the question_details.html ATTRIBUTES: question = {dictionary} question_header = {list} title = {string} answers = {list which has dictionaries} answers_header = {list} edit_question_url = {string} """ question = data_manager.read_from_csv(id=question_id) title = copy.deepcopy(question["title"]) del question["title"] question_header = util.create_header(question) all_answers = data_manager.read_from_csv(data_manager.ANSWER_FILE_PATH) answers = [ answer for answer in all_answers if answer["question_id"] == question_id ] answers_header = util.create_header(question) edit_question_url = url_for('route_edit_question', question_id=question_id) return render_template('question_details.html', question=question, question_header=question_header, title=title, answers=answers, answers_header=answers_header, edit_question_url=edit_question_url)
def sort_by_key(key='submission_time', reverse=False, database='questions'): if database == 'questions': database = data_manager.QUESTION_FILE_PATH else: database = data_manager.ANSWER_FILE_PATH file = data_manager.read_from_csv(database) file.sort(key=lambda row: row[key], reverse=True) return file
def route_edit_question(question_id): question = data_manager.read_from_csv(id=question_id) if request.method == 'GET': question_title = question['title'] question_message = question['message'] return render_template('edit_question.html', question_title=question_title, question_message=question_message) else: question['title'] = request.form.get('title') question['message'] = request.form.get('message') question['submission_time'] = data_manager.get_time() question_url = url_for('get_question_details', question_id=question_id) data_manager.write_to_csv(question, is_new=False) return redirect(question_url)
def delete_answer(answer_id): # Reading all data from answer.csv to a list of dictionaries answer_reader = data_manager.read_from_csv( file=data_manager.ANSWER_FILE_PATH) # Deleting the answer's dict from the list for row in answer_reader: if row['id'] == answer_id: answer_reader.remove(row) question_id = row['question_id'] # Writing back the list to the csv data_manager.overwrite_old_csv(answer_reader, file=data_manager.ANSWER_FILE_PATH) question_url = url_for('get_question_details', question_id=question_id) return redirect(question_url)
def delete_question(question_id): # Reading all data from question.csv to a list of dictionaries question_reader = data_manager.read_from_csv() # Deleting the question's dict from the list for row in question_reader: if row['id'] == question_id: question_reader.remove(row) # Writing back the list to the csv data_manager.overwrite_old_csv(question_reader) # Deleting the answers given to that question answer_ids = data_manager.find_answer_id(question_id) for id in answer_ids: delete_answer(id) return redirect('/')