Ejemplo n.º 1
0
def loginuser(useremail, password):
    user = helpers.get_user_by_email(useremail)
    if user and check_password(user, password):
        helpers.set_login_cookie(useremail)
        return user
    else:
        return None
Ejemplo n.º 2
0
def loginuser(useremail, password):
    user = helpers.get_user_by_email(useremail)
    if user and check_password(user, password):
        helpers.set_login_cookie(useremail)
        return user
    else:
        return None
Ejemplo n.º 3
0
def show_user_page():
    """Show user profile page."""

    email = session.get("current_user")

    if email:
        user = get_user_by_email(email)
        fav_courses = db.session.query(User.user_id,Course.title,Course.url,
                                        Course.picture,Course.course_id).join(
                                        Course_Favorited).join(Course).filter(
                                        User.user_id==user.user_id).all()
        taken_courses = db.session.query(User.user_id,Course.title,Course.url,
                                        Course.picture,Course.course_id).join(
                                        Course_Taken).join(Course).filter(
                                        User.user_id==user.user_id).all()
        enrolled_courses = db.session.query(User.user_id,Course.title,Course.url,
                                        Course.picture,Course.course_id).join(
                                        Course_Taking).join(Course).filter(
                                        User.user_id==user.user_id).all()

        return render_template("user_profile.html", 
                                user=user, 
                                fav_courses=fav_courses, 
                                taken_courses=taken_courses,
                                enrolled_courses=enrolled_courses)
    if not email:
        flash("You must be logged in to see your profile page.")
        return redirect("/")
Ejemplo n.º 4
0
def move_course_to_taken_list():
    """Move favorited course or enrolled course of user from courses_favorited 
        table or courses_taking table to courses_taken table.
    """

    course_id = request.form.get("id")
    email = session.get("current_user")
    user = get_user_by_email(email)

    if request.form.get("origin") == "fav":
        fav_course = Course_Favorited.query.filter_by(user_id=user.user_id, 
                                                course_id=course_id).first()
        db.session.delete(fav_course)
        db.session.commit()
        num_courses = db.session.query(func.count("*")).select_from(Course_Favorited
                                    ).filter_by(user_id=user.user_id).one()
    elif request.form.get("origin") == "enrolled":
        enrolled_course = Course_Taking.query.filter_by(user_id=user.user_id, 
                                                course_id=course_id).first()
        db.session.delete(enrolled_course)
        db.session.commit()
        num_courses = db.session.query(func.count("*")).select_from(Course_Taking
                                    ).filter_by(user_id=user.user_id).one()

    taken_course = Course_Taken(user_id=user.user_id, course_id=course_id)
    db.session.add(taken_course)
    db.session.commit()

    return jsonify({"course_no": num_courses})
Ejemplo n.º 5
0
def process_registeration():
    """Process regisration form and create new user in database"""

    fname = request.form.get("fname")
    lname = request.form.get("lname")
    email = request.form.get("email")
    password = request.form.get("password")
    password = hashlib.sha224(password).hexdigest()

    if get_user_by_email(email):
        flash("You already have an account. Please log in here.")
        # alert = "You already have an account. Please log in."
        return redirect("/login")
        # return jsonify({"alert": alert})

    else:
        user = User(fname=fname, lname=lname, email=email, password=password)
        db.session.add(user)
        db.session.commit()

        session["current_user"] = user.email
        flash("You have successfully created an account. Welcome!")

        return render_template("user_profile.html", 
                            user=user)
Ejemplo n.º 6
0
def show_user_page():
    """Show user profile page."""

    email = session.get("current_user")

    if email:
        user = get_user_by_email(email)
        fav_courses = db.session.query(User.user_id,Course.title,Course.url,
                                        Course.picture,Course.course_id).join(
                                        Course_Favorited).join(Course).filter(
                                        User.user_id==user.user_id).all()
        taken_courses = db.session.query(User.user_id,Course.title,Course.url,
                                        Course.picture,Course.course_id).join(
                                        Course_Taken).join(Course).filter(
                                        User.user_id==user.user_id).all()
        enrolled_courses = db.session.query(User.user_id,Course.title,Course.url,
                                        Course.picture,Course.course_id).join(
                                        Course_Taking).join(Course).filter(
                                        User.user_id==user.user_id).all()

        return render_template("user_profile.html", 
                                user=user, 
                                fav_courses=fav_courses, 
                                taken_courses=taken_courses,
                                enrolled_courses=enrolled_courses)
    if not email:
        flash("You must be logged in to see your profile page.")
        return redirect("/")
Ejemplo n.º 7
0
def process_registeration():
    """Process regisration form and create new user in database"""

    fname = request.form.get("fname")
    lname = request.form.get("lname")
    email = request.form.get("email")
    password = request.form.get("password")
    password = hashlib.sha224(password).hexdigest()

    if get_user_by_email(email):
        flash("You already have an account. Please log in here.")
        # alert = "You already have an account. Please log in."
        return redirect("/login")
        # return jsonify({"alert": alert})

    else:
        user = User(fname=fname, lname=lname, email=email, password=password)
        db.session.add(user)
        db.session.commit()

        session["current_user"] = user.email
        flash("You have successfully created an account. Welcome!")

        return render_template("user_profile.html", 
                            user=user)
Ejemplo n.º 8
0
def move_course_to_taken_list():
    """Move favorited course or enrolled course of user from courses_favorited 
        table or courses_taking table to courses_taken table.
    """

    course_id = request.form.get("id")
    email = session.get("current_user")
    user = get_user_by_email(email)

    if request.form.get("origin") == "fav":
        fav_course = Course_Favorited.query.filter_by(user_id=user.user_id, 
                                                course_id=course_id).first()
        db.session.delete(fav_course)
        db.session.commit()
        num_courses = db.session.query(func.count("*")).select_from(Course_Favorited
                                    ).filter_by(user_id=user.user_id).one()
    elif request.form.get("origin") == "enrolled":
        enrolled_course = Course_Taking.query.filter_by(user_id=user.user_id, 
                                                course_id=course_id).first()
        db.session.delete(enrolled_course)
        db.session.commit()
        num_courses = db.session.query(func.count("*")).select_from(Course_Taking
                                    ).filter_by(user_id=user.user_id).one()

    taken_course = Course_Taken(user_id=user.user_id, course_id=course_id)
    db.session.add(taken_course)
    db.session.commit()

    return jsonify({"course_no": num_courses})
Ejemplo n.º 9
0
    def test_is_enrolled(self):
        """Query db to see if course is currently being taken by user."""

        jane = get_user_by_email("*****@*****.**")

        assert is_enrolled(jane, 1) is False
        assert is_enrolled(jane, 2) is False
        assert is_enrolled(jane, 3) is True
Ejemplo n.º 10
0
    def test_is_favorited(self):
        """Query db to see if course is already favorited by user."""

        jane = get_user_by_email("*****@*****.**")

        assert is_favorited(jane, 1) is True
        assert is_favorited(jane, 2) is False
        assert is_favorited(jane, 3) is False
Ejemplo n.º 11
0
    def test_is_taken(self):
        """Query db to see if course has already been taken by user."""

        jane = get_user_by_email("*****@*****.**")

        assert is_taken(jane, 2) is True
        assert is_taken(jane, 1) is False
        assert is_taken(jane, 3) is False
Ejemplo n.º 12
0
 def GET(self, form=None):
     i = web.input()
     email = i.get('email', '')
     email_exists = bool(helpers.get_user_by_email(email))
     if email_exists and check_secret_token(email, i.get('token', '@')):
         form = form or forms.passwordform()
         return render.set_password(form, i.email)
     else:
         if email_exists: msg = 'Invalid token'
         else: msg = 'No user account exists with this email'
         helpers.set_msg(msg, msg_type='error')
         raise web.seeother('/u/forgot_password', absolute=True)
Ejemplo n.º 13
0
 def GET(self, form=None):
     i = web.input()
     email = i.get('email', '')
     email_exists = bool(helpers.get_user_by_email(email))
     if email_exists and check_secret_token(email, i.get('token', '@')):
         form = form or forms.passwordform()
         return render.set_password(form, i.email)
     else:
         if email_exists: msg = 'Invalid token'
         else: msg = 'No user account exists with this email'
         helpers.set_msg(msg, msg_type='error')
         raise web.seeother('/u/forgot_password', absolute=True)
Ejemplo n.º 14
0
def update_user_details(i, uid=None):
    if not uid:
        user = helpers.get_user_by_email(i.get('email'))
        uid = user and user.id
    i['phone'] = web.numify(i.get('phone'))
    details = ['prefix', 'lname', 'fname', 'addr1', 'addr2', 'city', 'zip5', 'zip4', 'phone', 'state']
    
    d = {}
    for (k, v) in i.items():
        if v and (k in details):
            d[k] = v
    db.update('users', where='id=$uid', vars=locals(), **d)
Ejemplo n.º 15
0
def update_user_details(i):
    user = helpers.get_user_by_email(i.get("email"))
    userid = user and user.id
    i["zip5"] = i.get("zipcode")
    i["phone"] = web.numify(i.get("phone"))
    details = ["prefix", "lname", "fname", "addr1", "addr2", "city", "zip5", "zip4", "phone", "state"]

    d = {}
    for (k, v) in i.items():
        if v and (k in details):
            d[k] = v
    db.update("users", where="id=$userid", vars=locals(), **d)
Ejemplo n.º 16
0
def remove_enrolled_course():
    """Remove taken course of user from courses_taken table."""

    course_id = request.form.get("id")
    email = session.get("current_user")
    user = get_user_by_email(email)
    course = Course_Taking.query.filter_by(user_id=user.user_id, 
                                            course_id=course_id).first()
    db.session.delete(course)
    db.session.commit()

    num_courses = db.session.query(func.count("*")).select_from(Course_Taking
                                    ).filter_by(user_id=user.user_id).one()

    return jsonify({"course_no": num_courses})
Ejemplo n.º 17
0
def update_user_details(i, uid=None):
    if not uid:
        user = helpers.get_user_by_email(i.get('email'))
        uid = user and user.id
    i['phone'] = web.numify(i.get('phone'))
    details = [
        'prefix', 'lname', 'fname', 'addr1', 'addr2', 'city', 'zip5', 'zip4',
        'phone', 'state'
    ]

    d = {}
    for (k, v) in i.items():
        if v and (k in details):
            d[k] = v
    db.update('users', where='id=$uid', vars=locals(), **d)
Ejemplo n.º 18
0
def unfavorite_course():
    """Remove favorited course of user from courses_favorited table."""

    course_id = request.form.get("id")
    email = session.get("current_user")
    user = get_user_by_email(email)
    course = Course_Favorited.query.filter_by(user_id=user.user_id, 
                                                course_id=course_id).first()
    db.session.delete(course)
    db.session.commit()

    num_courses = db.session.query(func.count("*")).select_from(Course_Favorited
                                    ).filter_by(user_id=user.user_id).one()

    return jsonify({"course_no": num_courses})
Ejemplo n.º 19
0
def unfavorite_course():
    """Remove favorited course of user from courses_favorited table."""

    course_id = request.form.get("id")
    email = session.get("current_user")
    user = get_user_by_email(email)
    course = Course_Favorited.query.filter_by(user_id=user.user_id, 
                                                course_id=course_id).first()
    db.session.delete(course)
    db.session.commit()

    num_courses = db.session.query(func.count("*")).select_from(Course_Favorited
                                    ).filter_by(user_id=user.user_id).one()

    return jsonify({"course_no": num_courses})
Ejemplo n.º 20
0
def remove_enrolled_course():
    """Remove taken course of user from courses_taken table."""

    course_id = request.form.get("id")
    email = session.get("current_user")
    user = get_user_by_email(email)
    course = Course_Taking.query.filter_by(user_id=user.user_id, 
                                            course_id=course_id).first()
    db.session.delete(course)
    db.session.commit()

    num_courses = db.session.query(func.count("*")).select_from(Course_Taking
                                    ).filter_by(user_id=user.user_id).one()

    return jsonify({"course_no": num_courses})
Ejemplo n.º 21
0
def process_login():
    """Process login of user to check if in database and then add them to 
        session if they are.
    """

    email = request.form.get("email")
    password = request.form.get("password")
    password = hashlib.sha224(password).hexdigest()

    user = get_user_by_email(email)
    if user:
        if user.password == password:
            session["current_user"] = user.email
            flash("You have successfully logged in.")
            return redirect("/profile")
        elif user.password != password:
            flash("Incorrect password.")
            return redirect("/login")
    elif user == None:
        flash("This email is not in our database.")
        return redirect("/login")
Ejemplo n.º 22
0
def process_login():
    """Process login of user to check if in database and then add them to 
        session if they are.
    """

    email = request.form.get("email")
    password = request.form.get("password")
    password = hashlib.sha224(password).hexdigest()

    user = get_user_by_email(email)
    if user:
        if user.password == password:
            session["current_user"] = user.email
            flash("You have successfully logged in.")
            return redirect("/profile")
        elif user.password != password:
            flash("Incorrect password.")
            return redirect("/login")
    elif user == None:
        flash("This email is not in our database.")
        return redirect("/login")
Ejemplo n.º 23
0
def bookmark_course():
    """Add favorited course of user to courses_favorited table."""

    if is_user(): 
        email = session.get("current_user")
        user = get_user_by_email(email)
        course_id = request.form.get("id")

        if is_favorited(user, course_id):
            alert = "You have already added this course to your \
                    favorites list!"
        elif is_taken(user, course_id):
            alert = "You have already added this course to your \
                    courses taken list!"
        elif is_enrolled(user, course_id):
            alert = "You are currently enrolled in this course!"

        else:
            action = request.form.get("action")
            if action == "favorite":
                new_course = Course_Favorited(user_id=user.user_id, 
                                                course_id=course_id)
                alert = "You have successfully added this course to your favorites!"
            elif action == "enrolled":
                new_course = Course_Taking(user_id=user.user_id, 
                                                course_id=course_id)
                alert = "You have successfully added this course to your enrolled courses list!"
            elif action == "taken":
                new_course = Course_Taken(user_id=user.user_id, 
                                                course_id=course_id)
                alert = "You have successfully added this course to your taken courses list!"
            db.session.add(new_course)
            db.session.commit()
    else:
        alert = "You must be signed in to add this course."

    return jsonify({'alert': alert})
Ejemplo n.º 24
0
def bookmark_course():
    """Add favorited course of user to courses_favorited table."""

    if is_user(): 
        email = session.get("current_user")
        user = get_user_by_email(email)
        course_id = request.form.get("id")

        if is_favorited(user, course_id):
            alert = "You have already added this course to your \
                    favorites list!"
        elif is_taken(user, course_id):
            alert = "You have already added this course to your \
                    courses taken list!"
        elif is_enrolled(user, course_id):
            alert = "You are currently enrolled in this course!"

        else:
            action = request.form.get("action")
            if action == "favorite":
                new_course = Course_Favorited(user_id=user.user_id, 
                                                course_id=course_id)
                alert = "You have successfully added this course to your favorites!"
            elif action == "enrolled":
                new_course = Course_Taking(user_id=user.user_id, 
                                                course_id=course_id)
                alert = "You have successfully added this course to your enrolled courses list!"
            elif action == "taken":
                new_course = Course_Taken(user_id=user.user_id, 
                                                course_id=course_id)
                alert = "You have successfully added this course to your taken courses list!"
            db.session.add(new_course)
            db.session.commit()
    else:
        alert = "You must be signed in to add this course."

    return jsonify({'alert': alert})
Ejemplo n.º 25
0
    def test_user_by_email(self):
        """Test query to db to get user from input of email."""

        jane = get_user_by_email("*****@*****.**")

        assert jane.lname == "Doe"
Ejemplo n.º 26
0
 def save_msg(self, subj, msg):
     uemail = helpers.get_loggedin_email()
     user = helpers.get_user_by_email(uemail)
     user_id = user and user.id
     msg_id = db.insert('wyr', politician=self.pol, subject=subj, message=msg, sender=user_id, sent=False)
     return msg_id