Example #1
0
def delete_tag(question_id, tag_id):
    util.check_if_user_is_logged()
    username, logged_user_id = util.get_user_details_from_session()
    selected_tag = data_manager.get_specific_record(question_id, "question")
    util.check_if_user_is_owner(logged_user_id, selected_tag["user_id"])
    data_manager.delete_tag(question_id, tag_id)
    return redirect(url_for("show_question", question_id=question_id))
Example #2
0
def delete_answer(answer_id):
    util.check_if_user_is_logged()
    username, logged_user_id = util.get_user_details_from_session()
    selected_answer = data_manager.get_specific_record(answer_id, "answer")
    util.check_if_user_is_owner(logged_user_id, selected_answer["user_id"])
    data_manager.delete_connected_comment(None, answer_id)
    data_manager.delete_record(answer_id, "answer")
    return redirect("/question/" + str(selected_answer["question_id"]))
Example #3
0
def delete_question(question_id):
    util.check_if_user_is_logged()
    username, logged_user_id = util.get_user_details_from_session()
    selected_question = data_manager.get_specific_record(question_id, "question")
    util.check_if_user_is_owner(logged_user_id, selected_question["user_id"])
    all_answers = data_manager.get_all_records("answer")
    for answer in all_answers:
        if str(answer.get("question_id")) == str(question_id):
            data_manager.delete_record(answer.get("id"), "answer")
            data_manager.delete_connected_comment(-1, answer.get("id"))
    data_manager.delete_connected_comment(question_id, -1)
    data_manager.delete_connected_tags(question_id)
    data_manager.delete_record(question_id, "question")
    return redirect("/list")
Example #4
0
def edit_answer(answer_id):
    util.check_if_user_is_logged()
    username, logged_user_id = util.get_user_details_from_session()
    selected_answer = data_manager.get_specific_record(answer_id, "answer")
    util.check_if_user_is_owner(logged_user_id, selected_answer["user_id"])
    if request.method == "POST":
        old_record = set_answer_values(selected_answer)
        if not old_record["image"]:
            old_record["image"] = ''
        data_manager.edit_record(old_record, "answer")
        return redirect("/question/" + str(old_record["question_id"]))
    return render_template("answer_form.html",
                           old_record=selected_answer,
                           is_new=False,
                           user_id=logged_user_id,
                           username=username)
Example #5
0
def add_question():
    util.check_if_user_is_logged()
    username, logged_user_id = util.get_user_details_from_session()
    default_blank_question = {}
    if request.method == "POST":
        new_question = set_question_values(default_blank_question)
        if not new_question["image"]:
            new_question["image"] = ''
        data_manager.add_record(new_question, "question")
        return redirect("/list")
    return render_template(
        "question_form.html",
        old_record=default_blank_question,
        is_new=True,
        user_id=logged_user_id,
        username=username
    )
Example #6
0
def edit_question(question_id):
    util.check_if_user_is_logged()
    username, logged_user_id = util.get_user_details_from_session()
    selected_question = data_manager.get_specific_record(question_id, "question")
    util.check_if_user_is_owner(logged_user_id, selected_question["user_id"])
    if request.method == "POST":
        updated_question = set_question_values(selected_question)
        if not updated_question["image"]:
            updated_question["image"] = ''
        data_manager.edit_record(updated_question, "question")
        return redirect("/question/" + str(question_id))
    return render_template(
        "question_form.html",
        old_record=selected_question,
        is_new=False,
        user_id=logged_user_id,
        username=username
    )
Example #7
0
def add_answer(question_id):
    util.check_if_user_is_logged()
    username, logged_user_id = util.get_user_details_from_session()
    if data_manager.get_question_owner(question_id).get(
            'user_id') == logged_user_id:
        flash("Ooops you can't answer your own question !")
        return redirect("/question/" + str(question_id))
    default_blank_answer = {"question_id": str(question_id)}
    if request.method == "POST":
        new_answer = set_answer_values(default_blank_answer)
        if not new_answer["image"]:
            new_answer["image"] = ''
        data_manager.add_record(new_answer, "answer")
        return redirect("/question/" + str(question_id))
    return render_template("answer_form.html",
                           old_record=default_blank_answer,
                           is_new=True,
                           user_id=logged_user_id,
                           username=username)
Example #8
0
def add_tag(question_id):
    util.check_if_user_is_logged()
    username, logged_user_id = util.get_user_details_from_session()
    selected_question = data_manager.get_specific_record(
        question_id, "question")
    util.check_if_user_is_owner(logged_user_id, selected_question["user_id"])
    tags_collection = data_manager.get_available_tags()
    new_tag = request.args.get("new_tag")
    if new_tag:
        if not data_manager.check_if_tag_already_available(
                new_tag, tags_collection):
            data_manager.add_tag_to_db(new_tag)
        tag_id = data_manager.get_tag_id(new_tag).get("id")
        if not data_manager.is_tag_already_assigned(question_id, tag_id):
            data_manager.assign_tag_to_question(question_id, tag_id)
        return redirect(f"/question/{question_id}")
    return render_template("add_tag.html",
                           tags_list=tags_collection,
                           user_id=logged_user_id,
                           username=username)