Exemple #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))
Exemple #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"]))
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")
Exemple #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)
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
    )
Exemple #6
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)