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 list_all(): if request.args: questions = data_manager.select_sql( 'question', order_column=[*request.args.keys()][0], order_asc_desc=[*request.args.values()][0]) return render_template('list_all.html', questions=questions) questions = data_manager.select_sql('question', order_column='submission_time', order_asc_desc='DESC') return render_template('list_all.html', questions=questions)
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 index(): latest_5_questions = data_manager.select_sql( 'question', order_column='submission_time', order_asc_desc='DESC', limit='5') return render_template('index.html', questions=latest_5_questions)
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_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 vote(id_, vote_direction, table): table_data = data_manager.select_sql(table, clause='WHERE', condition=['id', '=', id_]) if vote_direction == 'up': table_data[0]['vote_number'] += 1 elif vote_direction == 'down': table_data[0]['vote_number'] -= 1 data_manager.update_sql(table=table, column='vote_number', update_value=table_data[0]['vote_number'], update_condition=f'id={id_}') if table == 'answer': id_ = table_data[0]['question_id'] question = data_manager.select_sql('question', clause='WHERE', condition=['id', '=', id_]) question[0]['view_number'] -= 1 data_manager.update_sql(table='question', column='view_number', update_value=question[0]['view_number'], update_condition=f'id={id_}') return redirect(f'/question/{id_}')
def delete_comment(id_): comment_row = data_manager.select_sql(table='comment', clause='WHERE', condition=['id', '=', id_]) if comment_row[0]['answer_id'] is None: redirect_table = 'question' redirect_id = comment_row[0]['question_id'] else: redirect_table = 'answer' redirect_id = comment_row[0]['answer_id'] data_manager.delete_record(table='comment', clause='WHERE', condition=['id', '=', id_]) return redirect(f'/{redirect_table}/{redirect_id}')
def search(): questions = data_manager.select_sql( 'question', clause='WHERE', condition=['title', 'LIKE', '%' + [*request.args.values()][0] + '%'], clause_operator='OR', condition2=[ 'message', 'LIKE', '%' + [*request.args.values()][0] + '%' ]) answer_id = data_manager.select_sql( 'answer', clause='WHERE', condition=['message', 'LIKE', '%' + [*request.args.values()][0] + '%']) answer_id = [*{i['question_id'] for i in answer_id}] for item in answer_id: if item not in [*{i['id'] for i in questions}]: questions += data_manager.select_sql('question', clause='WHERE', condition=['id', '=', item]) if questions: return render_template('fancy_search.html', questions=questions, search_frase=[*request.args.values()][0]) return redirect('/')
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)