def add_question(): table_head = data_manager.get_table_head('question') if request.method == 'POST': new_record = { table_head[1]: str(strftime("%Y-%m-%d %H:%M:%S", gmtime())), table_head[2]: '0', table_head[3]: '0', table_head[4]: request.form[table_head[4]], table_head[5]: request.form[table_head[5]], table_head[6]: f'{request.form[table_head[6]] if request.form[table_head[6]] else None}' } data_manager.insert_record('question', new_record) new_record_id = data_manager.select_sql( 'question', column='id', clause='WHERE', condition=[table_head[1], '=', new_record[table_head[1]]]) return redirect(f'/question/{new_record_id[0]["id"]}') return render_template('new_question.html', table_head=table_head)
def edit_comment(comment_id): table_head = data_manager.get_table_head('comment') comment = data_manager.select_sql(table='comment', clause='WHERE', condition=['id', '=', comment_id]) if comment[0]['answer_id'] is None: redirect_table = 'question' redirect_id = comment[0]['question_id'] else: redirect_table = 'answer' redirect_id = comment[0]['answer_id'] print(comment) if request.method == 'POST': for column_name, element in request.form.items(): data_manager.update_sql('comment', column_name, element, update_condition=f'id={comment_id}') data_manager.update_sql('comment', 'edited_count', comment[0]['edited_count'] + 1, update_condition=f'id={comment_id}') return redirect(f'/{redirect_table}/{redirect_id}') return render_template('update_comment.html', table_head=table_head, comment=comment, redirect_id=redirect_id, redirect_table=redirect_table)
def show_question(question_id): question = data_manager.select_sql('question', clause='WHERE', condition=['id', '=', question_id]) answers = data_manager.select_sql( 'answer', clause='WHERE', condition=['question_id', '=', question_id]) comments = data_manager.select_sql( 'comment', clause='WHERE', condition=['question_id', '=', question_id]) comment_head = data_manager.get_table_head('comment') if not answers: answers = [{'Answers': 'This question doesn\'t have any answer yet.'}] if not comments: comment_head = ['Comments'] comments = [{'Comments': 'This answer doesn\'t have any comment yet.'}] question[0]['view_number'] += 1 data_manager.update_sql(table='question', column='view_number', update_value=question[0]['view_number'], update_condition=f'id={question_id}') return render_template('display_question.html', question=question, answers=answers, comments=comments, question_id=question_id, comment_head=comment_head)
def add_comment(table_name, id_): comment_head = data_manager.get_table_head('comment') if request.method == 'POST': if table_name == 'question': new_record = { comment_head[1]: id_, comment_head[3]: request.form[comment_head[3]], comment_head[4]: str(strftime("%Y-%m-%d %H:%M:%S", gmtime())), comment_head[5]: '0' } data_manager.insert_record('comment', new_record) elif table_name == 'answer': new_record = { comment_head[1]: '0', comment_head[2]: id_, comment_head[3]: request.form[comment_head[3]], comment_head[4]: str(strftime("%Y-%m-%d %H:%M:%S", gmtime())), comment_head[5]: '0' } data_manager.insert_record('comment', new_record) return redirect(f'/{table_name}/{id_}') return render_template('new_comment.html', table_head=comment_head, table_name=table_name, id=id_)
def edit_answer(answer_id): table_head = data_manager.get_table_head('answer') answer = data_manager.select_sql('answer', clause='WHERE', condition=['id', '=', answer_id]) if request.method == 'POST': for column_name, element in request.form.items(): data_manager.update_sql('answer', column_name, element, update_condition=f'id={answer_id}') return redirect(f'/answer/{answer_id}') return render_template('update_answer.html', table_head=table_head, answer=answer, answer_id=answer_id)
def edit_question(question_id): table_head = data_manager.get_table_head('question') question = data_manager.select_sql('question', clause='WHERE', condition=['id', '=', question_id]) if request.method == 'POST': for column_name, element in request.form.items(): data_manager.update_sql('question', column_name, element, update_condition=f'id={question_id}') return redirect(f'/question/{question_id}') return render_template('update_question.html', table_head=table_head, question=question, question_id=question_id)
def show_answer(answer_id): answer = data_manager.select_sql('answer', clause='WHERE', condition=['id', '=', answer_id]) comments = data_manager.select_sql('comment', clause='WHERE', condition=['answer_id', '=', answer_id]) comment_head = data_manager.get_table_head('comment') if not comments: comments = [{ 'Comments': 'This answer doesn\'t have any comments yet.' }] return render_template('display_answer.html', answer=answer, comments=comments, comment_head=comment_head, answer_id=answer_id, question_id=answer[0]['question_id'])
def add_answer(question_id): table_head = data_manager.get_table_head('answer') if request.method == 'POST': new_record = { table_head[1]: str(strftime("%Y-%m-%d %H:%M:%S", gmtime())), table_head[2]: '0', table_head[3]: str(question_id), table_head[4]: request.form[table_head[4]], table_head[5]: f'{request.form[table_head[5]] if request.form[table_head[5]] else None}' } data_manager.insert_record('answer', new_record) return redirect(f'/question/{question_id}') return render_template('new_answer.html', table_head=table_head, question_id=question_id)