Exemple #1
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 #2
0
def get_user_by_email(email):
    # loop over the users
    users_lock.acquire()
    for key, user in users_dict.items():
        user_email = user.get("email")
        if user_email and user_email == email:
            users_lock.release()
            return user
    users_lock.release()
    return None
Exemple #3
0
def is_admin(user_id):
    """is_admin will return whether the user is an admin and can do the operations that they are trying to access"""
    users_lock.acquire()
    user = users_dict.get(user_id)
    if user:
        admin = user['admin']
        if admin:
            users_lock.release()
            return True    
    users_lock.release()
    return False
Exemple #4
0
def delete_user(user_id):
    """delete_user will delete the user with the specified id"""
    users_lock.acquire()
    exists = users_dict.get(user_id)
    if exists:
        del users_dict[user_id]
        users_lock.release()
        return True
    else:
        users_lock.release()
        return False
Exemple #5
0
def get_polls_by_user_id(user_id):
    """get_polls_by_user_id will return a list of all the polls which a single user has created"""
    users_lock.acquire()
    user = users_dict[user_id]
    users_lock.release()
    poll_ids = user["polls"]
    polls = []
    polls_lock.acquire()
    for poll_id in poll_ids:
        poll = polls_dict[poll_id]
        polls.append(poll)
    polls_lock.release()
    return polls
Exemple #6
0
def user_make_admin(user_id):
    if not is_logged_in():
        return redirect("/login")
    if not is_admin(get_user_by_session()):
        return "404 page not found"
    user_login.acquire()
    user = users_dict.get(user_id)
    if not user:
        users_lock.release()
        return "User not found"
    user["admin"] = True
    users_lock.release()
    return "User successfully updated to admin"
Exemple #7
0
def create_user(email, password):
    """create_user will add a new user to the users_dict"""
    user = {
        'email': email,
        'password_hash': hash_password(password),
        'admin': False,
        'email_verified': False,
        'strikes': 0,
        'polls': [],
        'answers': []
    }
    users_lock.acquire()
    user['user_id'] = len(users_dict)
    users_dict[len(users_dict)] = user
    users_lock.release()
Exemple #8
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"