def views_updated(item_id): question_list = connection.read_csv("sample_data/question.csv") for question in question_list: if question["id"] == item_id: question["view_number"] = int(question["view_number"]) + 1 break connection.write_csv("sample_data/question.csv", question_list)
def edit_question_get(question_id): questions = connection.read_csv("sample_data/question.csv") question = data_handler.get_item_by_id(questions, str(question_id)) if question is None: return redirect(url_for("display_question", question_id=question_id)) else: return render_template("add_update_question.html", question=question)
def delete_answer_from_answers(question_id, answer_id): all_answers = connection.read_csv("sample_data/answer.csv") for answer in all_answers: if answer["question_id"] == question_id and answer["id"] == answer_id: if answer.get("image") != None: if os.path.exists(answer["image"]): os.remove(answer["image"]) all_answers.remove(answer) return all_answers
def update_question(edited_question): questions = connection.read_csv("sample_data/question.csv") for question in questions: if question["id"] == edited_question["id"]: question["title"] = edited_question["title"] question["message"] = edited_question["message"] question["image"] = edited_question["image"] return questions
def prepare_answers_for_display(question_id): all_answers = connection.read_csv("sample_data/answer.csv") answers = get_answers_for_question(all_answers, question_id) # for answer in answers: # czy to nie spowoduje komplikacji przy zapisywaniu do pliku csv? # chyba nie powinno, bo updateować bedziemy tylko te pozycje, które się zmieniają, # a submission_time nie będzie edytowalne. Druga opcja taka, że # znowu trzeba będzie użyć datetime, żeby wygenerować timestamp do zapisu do csv # answer["submission_time"] = transform_timestamp(answer["submission_time"]) return answers
def delete_all_answers_for_question(question_id): all_answers = connection.read_csv("sample_data/answer.csv") updated_answers = [] for answer in all_answers: if answer["question_id"] == question_id: if answer.get("image") != None: if os.path.exists(answer["image"]): os.remove(answer["image"]) else: updated_answers.append(answer) return updated_answers
def question_page(): headers = ["Title", "Message", "Submission Time", "Views", "Votes"] story_keys = [ "title", "message", "submission_time", "view_number", "vote_number" ] questions = connection.read_csv("sample_data/question.csv") if len(request.args) != 0: questions = data_handler.sorting_questions( questions, request.args.get("order_by"), request.args.get("order_direction")) return render_template("question_list.html", headers=headers, questions=questions, story_keys=story_keys)
def answer_vote(question_id, answer_id): post_result = dict(request.form) print(post_result) answers = data_handler.get_answers_for_question( connection.read_csv("sample_data/answer.csv"), question_id) answers = data_handler.update_votes(answers, answer_id, post_result) # for answer in answers: # if answer["id"] == answer_id: # if post_result["vote_answer"] == "vote_down": # answer["vote_number"] = int(answer.get("vote_number", 0)) - 1 # elif post_result["vote_answer"] == "vote_up": # answer["vote_number"] = int(answer.get("vote_number", 0)) + 1 connection.write_csv("sample_data/answer.csv", answers) return redirect(url_for("display_question", question_id=question_id))
def delete_question(question_id): questions = connection.read_csv("sample_data/question.csv") data_handler.delete_img(question_id) # answers = data_handler.get_answers_for_question(data_handler.prepare_answers_for_display(question_id),question_id ) # for answer in answers: # if answer.get("image") != None: # os.remove(answer["image"]) # answers.remove(answer) updated_answers = data_handler.delete_all_answers_for_question(question_id) connection.write_csv("sample_data/answer.csv", updated_answers) data_handler.delete_item_from_items(questions, question_id) connection.write_csv("sample_data/question.csv", questions) return redirect(url_for("question_page"))
def add_answer_post(question_id): answers = connection.read_csv("sample_data/answer.csv") new_answer = dict(request.form) new_answer["id"] = data_handler.get_new_id(answers) new_answer["submission_time"] = data_handler.get_current_timestamp() new_answer["vote_number"] = 0 new_answer["question_id"] = question_id uploaded_file = request.files['file'] if uploaded_file.filename != '': uploaded_file.save( os.path.join(app.config['UPLOAD_PATH'], uploaded_file.filename)) new_answer["image"] = os.path.join(app.config['UPLOAD_PATH'], uploaded_file.filename) answers.append(new_answer) connection.write_csv("sample_data/answer.csv", answers) return redirect(url_for("display_question", question_id=question_id))
def add_question_post(): new_question = dict(request.form) questions = connection.read_csv("sample_data/question.csv") new_question["id"] = data_handler.get_new_id(questions) new_question["submission_time"] = data_handler.get_current_timestamp() new_question["view_number"] = 0 new_question["vote_number"] = 0 uploaded_file = request.files['file'] if uploaded_file.filename != '': uploaded_file.save( os.path.join(app.config['UPLOAD_PATH'], uploaded_file.filename)) new_question["image"] = os.path.join(app.config['UPLOAD_PATH'], uploaded_file.filename) questions.append(new_question) connection.write_csv("sample_data/question.csv", questions) return redirect(url_for("display_question", question_id=new_question["id"]))
def get_next_id(filename): list_of_datas = connection.read_csv(filename) sorted_datas = sorted(list_of_datas, key=lambda row: row['id']) return str(int(sorted_datas[-1]['id']) + 1)
def question_vote_down(question_id): questions = connection.read_csv("sample_data/question.csv") questions = data_handler.substract_vote(questions, question_id) connection.write_csv("sample_data/question.csv", questions) return redirect(url_for("display_question", question_id=question_id))
def prepare_question_for_display(question_id): all_questions = connection.read_csv("sample_data/question.csv") question = get_item_by_id(all_questions, question_id) # question["submission_time"] = transform_timestamp(question["submission_time"]) return question
if order_direction == "desc": sorted_questions.reverse() return sorted_questions '''switch timestamp to a nice date string''' def transform_timestamp(timestamp): date_time = datetime.datetime.fromtimestamp(int(timestamp)) time_formatted = date_time.strftime('%d-%b-%Y (%H:%M:%S)') return time_formatted def delete_img(item_id): # nie działą item = prepare_question_for_display(item_id) path = item.get('image') # file_path = os.path.join(app.config['UPLOAD_PATH'], file_path) jeśli jest znana tylko nazwa pliku if os.path.exists(path): os.remove(path) else: return # jak tu zrobić informację (osobny route?) czy raise exept i z server.py przekierowac gdzies? if __name__ == "__main__": s = connection.read_csv("sample_data/question.csv") print(s) d = sorting_questions(s, "title", "view_number") print(d)