Exemple #1
0
def poll_manage():
    if not is_logged_in():
        return redirect("/login")
    # get the users polls
    user = get_user_by_session()
    polls = get_polls_by_user_id(user["user_id"])
    return render_template("manage_polls.html", polls=polls)
Exemple #2
0
def poll_new():
    if not is_logged_in():
        return redirect("/login")
    if request.method == "POST":
        # Create the poll via the information passed into it
        form = request.form
        question = form.get("question")
        if not question:
            return "Please enter a question"
        user = get_user_by_session()
        polls_lock.acquire()
        poll_id = len(polls_dict)
        poll = {
            "poll_id": poll_id,
            "user_id": user["user_id"],
            "question": question,
            "reports": 0,
            "answers": []
        }
        polls_dict[poll_id] = poll
        polls_lock.release()
        polls = user["polls"]
        polls.append(poll_id)
        users_lock.acquire()
        user["polls"] = polls # might be redundant?
        users_dict[user["user_id"]] = user
        users_lock.release()
        return "Successfully created poll"
    return render_template("new_poll.html") 
Exemple #3
0
def poll_delete(poll_id):
    """poll_delete will delete the poll which the link points to only if the user is the owner of the link, or the user is an admin of the site"""
    if not is_logged_in():
        return redirect("/login")
    user = get_user_by_session()
    polls_lock.acquire()
    poll = polls_dict.get(poll_id)
    if not poll:
        polls_lock.release()
        return "No poll with that id"
    # check if the are an admin, or they are the owner of the poll
    if is_admin() or poll["user_id"] == user["user_id"]:
        del polls_dict[poll_id]
        polls_lock.release()
        return "Poll successfully deleted"
Exemple #4
0
def poll_edit(poll_id):
    polls_lock.acquire()
    poll = polls_dict.get(poll_id)
    if not poll:
        return "The poll was not found"
    if request.method == "POST":
        if not is_logged_in():
            return redirect("/login")
        form = request.form
        question= form.get("question")
        if not question:
            return "The edited poll did not update the question"
        poll["question"] = question
        polls_dict[poll_id] = poll
        polls_lock.release()
        return "Successfully updated poll"
    polls_lock.release()
    return render_template("poll_edit.html", poll=poll)
Exemple #5
0
def poll_anwser_new(poll_id):
    if not is_logged_in():
        return redirect("/login")
    if request.method == "POST":
        form = request.form
        answer = form.get("answer")
        if not answer:
            return "Did not enter an answer value"
        polls_lock.acquire()
        poll = polls_dict.get(poll_id)
        if not poll:
            polls_lock.release()
            return "No poll with that id"
        answers = poll["answers"]
        user = get_user_by_session()
        user_id = user["user_id"]
        answer_id = len(answers)
        answer = {
            "answer_id": answer_id,
            "user_id": user_id,
            "answer": answer,
            "votes": 0,
            "reports": 0
        }
        answers[str(answer_id)] = answer
        poll["answers"] = answers
        polls_dict[poll_id] = poll
        polls_lock.release()
        answer = {
            "poll_id": poll_id,
            "answer_id": answer_id
        }
        users_lock.acquire()
        user["answers"].append(answer)
        users_dict[user_id] = user
        users_lock.release()
        return "Answer successfully added"