def delete_answer(question_id=None, answer_id=None): answ_datas = data_manager.get_answers_by_question_id(question_id) question_id = answ_datas[0]['question_id'] data_manager.delete_answer(answer_id) return redirect( url_for('get_question_and_answer_and_comments_by_id', question_id=question_id))
def route_question_detail(id): try: question = data_manager.get_question_by_id(id) answers = data_manager.get_answers_by_question_id(id) return render_template('qd.html', question=question, id=id, answers=answers) except ValueError: return redirect('/')
def route_add_answer(question_id): if request.method == 'GET': question = data_manager.get_question_by_id(question_id) answers = data_manager.get_answers_by_question_id(question_id) return render_template('answer.html', question=question, answers=answers) elif request.method == 'POST': if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit a empty part without filename if file.filename == '': flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) values = [ util.get_timestamp(), '0', question_id, request.form['message'], filename ] data_manager.add_answer(values) return redirect(f'/question/{question_id}')
def route_show_question(question_id): data_manager.update_view_counter(question_id, 1) current_question = data_manager.get_record_by_id('question', question_id)[0] current_answers = data_manager.get_answers_by_question_id(question_id) number_of_answers = len(current_answers) current_question_comments = data_manager.get_comments_by_question_id( question_id) current_answer_comments = data_manager.get_answer_comments() if 'username' in session: username = session['username'] return render_template('show_question.html', question_id=question_id, question=current_question, answers=current_answers, question_comments=current_question_comments, answer_comments=current_answer_comments, number_of_answers=number_of_answers, username=username) return render_template('show_question.html', question_id=question_id, question=current_question, answers=current_answers, question_comments=current_question_comments, answer_comments=current_answer_comments, number_of_answers=number_of_answers)
def manage_questions(question_id): if request.args.getlist('addinganswer'): addinganswer = True else: addinganswer = False current_question = data_manager.get_question_by_id(question_id) answers_to_question = data_manager.get_answers_by_question_id(question_id) reputation = data_manager.get_reputation(current_question['user_name']) current_question['reputation'] = reputation['reputation'] comments = data_manager.find_comments(question_id) if 'user' in session: question_vote = data_manager.check_if_user_voted_on_question( session['user'], question_id) else: question_vote = [] return render_template("question-child.html", question_id=int(question_id), comments=comments, question=current_question, answers=answers_to_question, addinganswer=addinganswer, question_headers=QUESTION_HEADERS, answer_headers=ANSWER_HEADERS, question_vote=question_vote)
def route_question(question_id): data_manager.update_question_view_number(question_id) question = data_manager.get_question_by_id(question_id) answers = data_manager.get_answers_by_question_id(question_id) comments = data_manager.get_comments() return render_template('question.html', question=question, answers=answers, comments=comments)
def get_question_details(question_id): data_manager.add_question_view(question_id) question = data_manager.get_question_by_id(question_id) question_author = data_manager.get_user_data_by_id(question["user_id"]) answers_for_question = data_manager.get_answers_by_question_id(question_id) comments_for_question = data_manager.get_comments_by_question_id(question_id) comments_for_answer = data_manager.get_answer_comments_to_question(question_id) return render_template("question_page.html", dict_of_question=question, question_author_data=question_author, comments_for_question=comments_for_question, answers_to_list=answers_for_question, comments_for_answer=comments_for_answer)
def detail_question(question_id): if request.args.get('count_view'): data_manager.increase_view_number(question_id) return redirect(url_for("detail_question", question_id=question_id)) question = data_manager.get_question_by_id(question_id) answers = data_manager.get_answers_by_question_id(question_id) tags = data_manager.get_tags_by_question_id(question_id) return render_template('detailed_question.html', question=question, tags=tags, answers=answers)
def route_delete_tag(question_id, tag): utility.remove_tag_from_question(tag, question_id) answers = data_manager.get_answers_by_question_id(question_id) question = data_manager.get_question_by_id(question_id) tags = utility.get_all_tags() question_tags = utility.get_tags_by_question_id(question_id) return render_template('display_question.html', answers=answers, question=question, question_id=question_id, tags=tags, question_tags=question_tags)
def route_question_delete(question_id): if 'id' not in session: return redirect(url_for('route_main')) answers = data_manager.get_answers_by_question_id(int(question_id)) question = data_manager.get_row_from_table('question', int(question_id)) for answer in answers: if os.path.exists(answer['image'][1:]): os.remove(answer['image'][1:]) if os.path.exists(question[0]['image'][1:]): os.remove(question[0]['image'][1:]) data_manager.delete_by_id('question', 'id', int(question_id)) data_manager.tag_delete() return redirect("/")
def get_question_and_answer_and_comments_by_id(question_id=None): quest_datas = data_manager.get_question_by_id(question_id) answ_datas = data_manager.get_answers_by_question_id(question_id) print(answ_datas) q_comm_datas = data_manager.get_comment_by_question_id(question_id) a_comm_datas = data_manager.get_comment_by_question_id(question_id) return render_template( 'questions.html', question_id=question_id, quest_datas=quest_datas, answ_datas=answ_datas, q_comm_datas=q_comm_datas, a_comm_datas=a_comm_datas, )
def new_answer(question_id): if request.method == 'POST': user_id = data_manager.get_user_id_by_username(session['username']) file_to_upload = request.files['image_file'] answer_to_add = dict(request.form) answer_to_add['image'] = save_file(file_to_upload) data_manager.add_answer(answer_to_add, user_id) question_id = request.form['question_id'] return redirect(url_for('route_question', question_id=question_id)) question = data_manager.get_question_by_id(question_id) answers = data_manager.get_answers_by_question_id(question_id) return render_template('new-answer.html', question=question, answers=answers)
def route_add_new_tag(question_id, tag): if request.method == 'GET' and tag is None: answers = data_manager.get_answers_by_question_id(question_id) question = data_manager.get_question_by_id(question_id) tags = utility.get_all_tags() question_tags = utility.get_tags_by_question_id(question_id) return render_template('new-tag.html', answers=answers, question=question, question_id=question_id, tags=tags, question_tags=question_tags) else: new_question_tag = tag if tag else request.form["new-tag"] utility.add_tag_to_question(question_id, new_question_tag) return redirect( url_for('display.route_display', question_id=question_id))
def route_question_display(question_id): tag = data_manager.get_tag_by_question_id(question_id) if tag == []: current_tag = None else: tag = tag[0]['tag_id'] current_tag = data_manager.get_record_by_id("tag", tag) template_name = "record-details.html" question = data_manager.get_question_with_user_info(question_id) if question is None: return render_template(template_name, question_id=question_id) answers = data_manager.get_answers_by_question_id(question_id) comments = data_manager.get_comment_by_parent_id('question_id', question_id) return render_template(template_name, question=question, answers=answers, comments=comments, tag=current_tag)
def route_question(question_id): question = data_manager.get_question_with_username(int(question_id)) tags = data_manager.get_tags(question_id) answers = data_manager.get_answers_by_question_id(int(question_id)) question_comment = data_manager.get_comment_with_username('question_id', int(question_id)) answers_comments = data_manager.get_answer_comments_with_username(int(question_id)) if 'id' in session: user_id = session["id"] upvoted_questions = data_manager.get_votes_by_user('question_id', user_id, 'True') downvoted_questions = data_manager.get_votes_by_user('question_id', user_id, 'False') upvoted_answers = data_manager.get_votes_by_user('answer_id', user_id, 'True') downvoted_answers = data_manager.get_votes_by_user('answer_id', user_id, 'False') else: upvoted_questions = None downvoted_questions = None upvoted_answers = None downvoted_answers = None return render_template("question.html", question_id=int(question_id), question=question[0], answers=answers, question_comment=question_comment, answers_comments=answers_comments, tags=tags, upvoted_questions=upvoted_questions, downvoted_questions=downvoted_questions, upvoted_answers=upvoted_answers, downvoted_answers=downvoted_answers)
def route_question(question_id): question_data = data_manager.question(question_id) answers = data_manager.get_answers_by_question_id(question_id) comment_question = data_manager.get_comment_by_question(question_id) comment_answer = data_manager.get_comment_by_answer() question_tags = data_manager.get_tags(question_id) accept_delete = False if request.args: accept_delete = True return render_template('question.html', question_id=question_id, question=question_data['title'], question_message=question_data['message'], image=question_data['image'], answer_images=answers, comment=comment_question, answer_comment=comment_answer, tags=question_tags, accept_delete=accept_delete)
def display_question(question_id): if request.referrer != request.url: data_manager.views_updated(question_id) question = data_manager.get_question_by_id(question_id) answers = data_manager.get_answers_by_question_id(question_id) question_comments = data_manager.get_comments_by_question_id(question_id) answer_comments = data_manager.get_answer_comments_by_question_id( question_id) answers_headers = ["Votes' number", "Answer", "Submission time"] comment_headers = ["Submission time", "Message", "Edition counter"] question_tag = data_manager.get_tag_by_question_id(question_id) # users = data_manager.get_all_users_basic_info() question_image = data_manager.get_question_image_by_id(question_id) answers_images = util.get_answers_images(answers) author_id = data_manager.check_question_author_id(question_id)['user_id'] response = make_response( render_template( "question.html", username=SESSION_USERNAME, user_id=SESSION_ID, question=question, answers=answers, answers_headers=answers_headers, question_comments=question_comments, comment_headers=comment_headers, answer_comments=answer_comments, question_tag=question_tag, question_image=question_image, answers_images=answers_images, author_id=author_id, # author=author # users=users )) return response
def route_display(question_id): if request.method == 'GET': answers = data_manager.get_answers_by_question_id(question_id) question = data_manager.get_question_by_id(question_id) question_tags = utility.get_tags_by_question_id(question_id) question_comments = utility.show_comment_question(question_id) answer_comments = [] for item in answers: sublist = utility.show_comment_answer(item['id']) for element in sublist: answer_comments.append(element) return render_template('display_question.html', question=question, answers=answers, question_id=question_id, question_tags=question_tags, question_comments=question_comments, answer_comments=answer_comments) else: file = request.files['file'] if file and utility.allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) answer = { 'submission_time': datetime.today(), 'vote_number': 0, 'question_id': question_id, 'message': request.form['answer'], 'image': UPLOAD_FOLDER + '/' + filename if file.filename else '', 'user_id': None } if 'user_id' in session: answer['user_id'] = session['user_id'] data_manager.add_new_answer(answer) return redirect('/display/' + question_id)
def route_question(question_id): question = data_manager.get_question_by_id(question_id) answers = data_manager.get_answers_by_question_id(question_id) return render_template('question.html', question=question, answers=answers)
def delete_answer_comment_by_question_id(cursor, question_id): answers = data_manager.get_answers_by_question_id(question_id) for answer in answers: delete_comment_by_answer(answer['id'])
def get_question_answers(question_id): return data_manager.get_answers_by_question_id(question_id)