Пример #1
0
def createUser(login_session):
    # First check if the user already has an account
    user_id = getUserID(login_session)
    # If the user already has an account, add (or re-add) the user's google or
    # facebook id, and then quit early so as not to create another user
    if user_id:
        user = User.query.filter_by(id=user_id).one()
        if login_session.get('facebook_id'):
            user.facebook_id = login_session['facebook_id'].encode('utf-8')
        if login_session.get('gplus_id'):
            user.gplus_id = login_session['gplus_id'].encode('utf-8')
        session.add(user)
        session.commit()
        return user_id
    # At this point the user doesn't have an account, so we create a new
    # account for the user
    new_user = User(
        name=login_session['username'].encode('utf-8'),
        email=login_session['email'].encode('utf-8'),
        picture=login_session['picture'].encode('utf-8'))

    if login_session.get('facebook_id'):
        new_user.facebook_id = login_session['facebook_id'].encode('utf-8')
    if login_session.get('gplus_id'):
        new_user.gplus_id = login_session['gplus_id'].encode('utf-8')

    session.add(new_user)
    session.commit()
    return getUserID(login_session)
Пример #2
0
def deleteLizard(lizard_id):
    """deleteLizard: Displays form to delete a lizard and posts that
                       information to the database; requires login

    Arguments are derived from the url"""
    lizard_to_delete = Lizard.query.filter_by(id=lizard_id).one()
    if request.method == "GET":
        return render_template(
            "deleteLizard.html", lizard=lizard_to_delete,
            login_session=login_session)

    change_log = ChangeLog(
        user_id=lizard_to_delete.user_id,
        lizard_name=lizard_to_delete.name,
        update_instant=datetime.datetime.utcnow(),
        action="delete",
        table="lizard")

    session.add(change_log)

    # hobbies = Hobby.query.filter_by(lizard_id=lizard_to_delete.id)
    # for hobby in hobbies:
    #     HobbyImage.query.filter_by(hobby_id=hobby.id).delete()
    #     session.delete(hobby)
    session.delete(lizard_to_delete)
    flash("Lizard %s Successfully Deleted" % lizard_to_delete.name)
    session.commit()
    return redirect(url_for("showLizard"))
Пример #3
0
def deleteHobby(lizard_id, hobby_id):
    """deleteHobby: Displays form to delete an hobby and posts that information
                   to the database; requires login and that the logged-in user
                   is also the user that created the lizard

    Arguments are derived from the url"""
    lizard = Lizard.query.filter_by(id=lizard_id).one()
    hobby_to_delete = Hobby.query.\
        filter_by(id=hobby_id, lizard_id=lizard_id).one()
    if request.method == "GET":
        return render_template("deleteHobby.html", lizard=lizard,
        hobby=hobby_to_delete, login_session=login_session)

    change_log = ChangeLog(
        user_id=hobby_to_delete.user_id,
        lizard_name=lizard.name,
        lizard_id=lizard_id,
        hobby_name=hobby_to_delete.name,
        update_instant=datetime.datetime.utcnow(),
        action="delete",
        table="hobby")

    session.add(change_log)
    session.delete(hobby_to_delete)
    flash("Hobby %s Successfully Deleted" % (hobby_to_delete.name))
    session.commit()
    return redirect(url_for("showHobby", lizard_id=lizard_id))
Пример #4
0
def deleteHobby(lizard_id, hobby_id):
    """deleteHobby: Displays form to delete an hobby and posts that information
                   to the database; requires login and that the logged-in user
                   is also the user that created the lizard

    Arguments are derived from the url"""
    lizard = Lizard.query.filter_by(id=lizard_id).one()
    hobby_to_delete = Hobby.query.\
        filter_by(id=hobby_id, lizard_id=lizard_id).one()
    if request.method == "GET":
        with store_context(store):
            return render_template("deleteHobby.html", lizard=lizard,
            hobby=hobby_to_delete, login_session=login_session)

    change_log = ChangeLog(
        user_id=hobby_to_delete.user_id,
        lizard_name=lizard.name,
        lizard_id=lizard_id,
        hobby_name=hobby_to_delete.name,
        update_instant=datetime.datetime.utcnow(),
        action="delete",
        table="hobby")

    session.add(change_log)
    session.delete(hobby_to_delete)
    flash("Hobby %s Successfully Deleted" % (hobby_to_delete.name))
    with store_context(store):
        session.commit()
    return redirect(url_for("showHobby", lizard_id=lizard_id))
Пример #5
0
def createUser(login_session):
    # First check if the user already has an account
    user_id = getUserID(login_session)
    # If the user already has an account, add (or re-add) the user's google or
    # facebook id, and then quit early so as not to create another user
    if user_id:
        user = User.query.filter_by(id=user_id).one()
        if login_session.get('facebook_id'):
            user.facebook_id = login_session['facebook_id']
        if login_session.get('gplus_id'):
            user.gplus_id = login_session['gplus_id']
        session.add(user)
        session.commit()
        return user_id
    # At this point the user doesn't have an account, so we create a new
    # account for the user
    new_user = User(
        name=login_session['username'],
        email=login_session['email'],
        picture=login_session['picture'])

    if login_session.get('facebook_id'):
        new_user.facebook_id = login_session['facebook_id']
    if login_session.get('gplus_id'):
        new_user.gplus_id = login_session['gplus_id']

    session.add(new_user)
    session.commit()
    return getUserID(login_session)
Пример #6
0
def newHobby(lizard_id):
    """newHobby: Displays new hobby creation form and posts new hobby to the
                database; requires login and that the logged-in user is also
                the user that created the lizard

    Arguments are derived from the url"""
    lizard = Lizard.query.filter_by(id=lizard_id).one()
    if request.method == "GET":
        with store_context(store):
            return render_template("newHobby.html",
                                   lizard=lizard,
                                   login_session=login_session)

    url = request.form.get("url")
    (url, error) = isURLImage(url)
    if error:
        return render_template("newHobby.html",
                               login_session=login_session,
                               lizard=lizard,
                               error=error)
    try:
        url_open = urlopen(url)
    except:
        error = "Unable to make a request to this URL: %s" % (url)
        return render_template("newHobby.html",
                               login_session=login_session,
                               lizard=lizard,
                               error=error)

    new_hobby = Hobby(
        name=request.form.get("name"),
        description=request.form.get("description"),
        lizard_id=lizard_id,
        user_id=lizard.user_id,
        picture_url=url)

    with store_context(store):
        new_hobby.picture.from_file(url_open)
        session.add(new_hobby)
        flash("New Hobby %s Successfully Created" % (new_hobby.name))
        session.commit()
        url_open.close()

    newest_hobby = Hobby.query.\
        filter_by(user_id=lizard.user_id).\
        order_by(db.desc("creation_instant")).limit(1)[0]

    change_log = ChangeLog(
        user_id=lizard.user_id,
        lizard_name=lizard.name,
        lizard_id=lizard_id,
        hobby_name=newest_hobby.name,
        hobby_id=newest_hobby.id,
        update_instant=newest_hobby.creation_instant,
        action="new",
        table="hobby")

    session.add(change_log)
    session.commit()
    return redirect(url_for("showHobby", lizard_id=lizard_id))
Пример #7
0
def deleteLizard(lizard_id):
    """deleteLizard: Displays form to delete a lizard and posts that
                       information to the database; requires login

    Arguments are derived from the url"""
    lizard_to_delete = Lizard.query.filter_by(id=lizard_id).one()
    if request.method == "GET":
        with store_context(store):
            return render_template(
                "deleteLizard.html", lizard=lizard_to_delete,
                login_session=login_session)

    change_log = ChangeLog(
        user_id=lizard_to_delete.user_id,
        lizard_name=lizard_to_delete.name,
        update_instant=datetime.datetime.utcnow(),
        action="delete",
        table="lizard")

    session.add(change_log)

    Hobby.query.filter_by(lizard_id=lizard_to_delete.id).delete()
    session.delete(lizard_to_delete)
    flash("Lizard %s Successfully Deleted" % lizard_to_delete.name)
    with store_context(store):
        session.commit()
    return redirect(url_for("showLizard"))
Пример #8
0
def newHobby(lizard_id):
    """newHobby: Displays new hobby creation form and posts new hobby to the
                database; requires login and that the logged-in user is also
                the user that created the lizard

    Arguments are derived from the url"""
    lizard = Lizard.query.filter_by(id=lizard_id).one()
    if request.method == "GET":
        return render_template("newHobby.html",
                               lizard=lizard,
                               login_session=login_session)

    url = request.form.get("url")
    (url, error) = isURLImage(url)
    if error:
        return render_template("newHobby.html",
                               login_session=login_session,
                               lizard=lizard,
                               error=error)
    # try:
    #     url_open = urlopen(url)
    # except:
    #     error = "Unable to make a request to this URL: %s" % (url)
    #     return render_template("newHobby.html",
    #                           login_session=login_session,
    #                           lizard=lizard,
    #                           error=error)

    new_hobby = Hobby(
        name=request.form.get("name"),
        description=request.form.get("description"),
        lizard_id=lizard_id,
        user_id=lizard.user_id,
        picture_url=url)

    session.add(new_hobby)
    flash("New Hobby %s Successfully Created" % (new_hobby.name))
    session.commit()

    newest_hobby = Hobby.query.\
        filter_by(user_id=lizard.user_id).\
        order_by(db.desc("creation_instant")).limit(1)[0]

    change_log = ChangeLog(
        user_id=lizard.user_id,
        lizard_name=lizard.name,
        lizard_id=lizard_id,
        hobby_name=newest_hobby.name,
        hobby_id=newest_hobby.id,
        update_instant=newest_hobby.creation_instant,
        action="new",
        table="hobby")

    session.add(change_log)
    session.commit()
    return redirect(url_for("showHobby", lizard_id=lizard_id))
Пример #9
0
def newLizard():
    """newLizard: Displays new lizard creation form and posts new lizard
                    to the database; requires login"""
    if request.method == "GET":
        return render_template("newLizard.html", login_session=login_session)
    # First check to see if image URL is valid from HEAD reqauest.
    # If its not return error
    url = request.form.get("url")
    (url, error) = isURLImage(url)
    if error:
        return render_template("newLizard.html",
                               login_session=login_session,
                               error=error)
    # urlopen uses a GET request and does not accept HTTPS urls
    try:
        url_open = urlopen(url)
    except:
        error = "Unable to make a request to this URL: %s" % (url)
        return render_template("newLizard.html",
                               login_session=login_session,
                               error=error)
    # Create Lizard object
    new_lizard = Lizard(
        name=request.form.get("name"),
        user_id=login_session.get("user_id"),
        picture_url=url)

    # Must add picture to lizard object within store_context
    with store_context(store):
        new_lizard.picture.from_file(url_open)  # adding picture here
        session.add(new_lizard)
        session.commit()
        url_open.close()  # make sure to close url connection after commit

    # After commit, retrieve lizard info to add to the ChangeLog
    newest_lizard = Lizard.query.\
        filter_by(user_id=login_session.get("user_id")).\
        order_by(db.desc("creation_instant")).limit(1)[0]

    change_log = ChangeLog(
        user_id=newest_lizard.user_id,
        lizard_name=newest_lizard.name,
        lizard_id=newest_lizard.id,
        update_instant=newest_lizard.creation_instant,
        action="new",
        table="lizard")
    session.add(change_log)
    flash("New Lizard %s Successfully Created" %\
        (newest_lizard.name))
    session.commit()
    return redirect(url_for("showLizard"))
Пример #10
0
def newLizard():
    """newLizard: Displays new lizard creation form and posts new lizard
                    to the database; requires login"""
    if request.method == "GET":
        return render_template("newLizard.html", login_session=login_session)
    # First check to see if image URL is valid from HEAD reqauest.
    # If its not return error
    url = request.form.get("url")
    (url, error) = isURLImage(url)
    if error:
        return render_template("newLizard.html",
                               login_session=login_session,
                               error=error)
    # urlopen uses a GET request and does not accept HTTPS urls
    # try:
    #     url_open = urlopen(url)
    # except:
    #     error = "Unable to make a request to this URL: %s" % (url)
    #     return render_template("newLizard.html",
    #                           login_session=login_session,
    #                           error=error)
    # Create Lizard object
    new_lizard = Lizard(
        name=request.form.get("name"),
        user_id=login_session.get("user_id"),
        picture_url=url)

    session.add(new_lizard)
    session.commit()

    # After commit, retrieve lizard info to add to the ChangeLog
    newest_lizard = Lizard.query.\
        filter_by(user_id=login_session.get("user_id")).\
        order_by(db.desc("creation_instant")).limit(1)[0]

    change_log = ChangeLog(
        user_id=newest_lizard.user_id,
        lizard_name=newest_lizard.name,
        lizard_id=newest_lizard.id,
        update_instant=newest_lizard.creation_instant,
        action="new",
        table="lizard")
    session.add(change_log)
    flash("New Lizard %s Successfully Created" %\
        (newest_lizard.name))
    session.commit()
    return redirect(url_for("showLizard"))
Пример #11
0
def editLizard(lizard_id):
    """editLizard: Displays form to edit a lizard"s name and/or image url
                     and posts that information to the database; requires login

    Arguments are derived from the url"""
    edited_lizard = Lizard.query.filter_by(id=lizard_id).one()
    if request.method == "GET":
        with store_context(store):
            return render_template("editLizard.html",
                                   lizard=edited_lizard,
                                   login_session=login_session)

    url = request.form.get("url")
    (url, error) = isURLImage(url)
    if error:
        return render_template("editLizard.html",
                               login_session=login_session,
                               lizard=edited_lizard,
                               error=error)
    try:
        url_open = urlopen(url)
    except:
        error = "Unable to make a request to this URL: %s" % (url)
        return render_template("editLizard.html",
                               login_session=login_session,
                               lizard=edited_lizard,
                               error=error)

    change_log = ChangeLog(
        user_id=edited_lizard.user_id,
        lizard_name=request.form.get("name"),
        lizard_id=lizard_id,
        update_instant=datetime.datetime.utcnow(),
        action="update",
        table="lizard")

    edited_lizard.name = request.form.get("name")
    edited_lizard.picture_url = url
    # Add all info to session while in store_context
    with store_context(store):
        edited_lizard.picture.from_file(url_open)
        session.add(change_log)
        session.add(edited_lizard)
        flash("Lizard %s Successfully Edited"  % edited_lizard.name)
        session.commit()
        url_open.close()
    return redirect(url_for("showLizard"))
Пример #12
0
def editLizard(lizard_id):
    """editLizard: Displays form to edit a lizard"s name and/or image url
                     and posts that information to the database; requires login

    Arguments are derived from the url"""
    edited_lizard = Lizard.query.filter_by(id=lizard_id).one()
    if request.method == "GET":
        return render_template("editLizard.html",
                               lizard=edited_lizard,
                               login_session=login_session)

    url = request.form.get("url")
    (url, error) = isURLImage(url)
    if error:
        return render_template("editLizard.html",
                               login_session=login_session,
                               lizard=edited_lizard,
                               error=error)
    # try:
    #     url_open = urlopen(url)
    # except:
    #     error = "Unable to make a request to this URL: %s" % (url)
    #     return render_template("editLizard.html",
    #                           login_session=login_session,
    #                           lizard=edited_lizard,
    #                           error=error)

    change_log = ChangeLog(
        user_id=edited_lizard.user_id,
        lizard_name=request.form.get("name"),
        lizard_id=lizard_id,
        update_instant=datetime.datetime.utcnow(),
        action="update",
        table="lizard")

    edited_lizard.name = request.form.get("name")
    edited_lizard.picture_url = url

    session.add(change_log)
    session.add(edited_lizard)
    flash("Lizard %s Successfully Edited"  % edited_lizard.name)
    session.commit()
    return redirect(url_for("showLizard"))
Пример #13
0
    path=os.path.join(os.path.dirname(__file__), "pkg/images"))

app.wsgi_app = store.wsgi_middleware(app.wsgi_app)

with open("testData.JSON") as data_file:
    lizards = json.load(data_file)
    for next_lizard in lizards["lizards"]:
        lizard = Lizard(name=next_lizard["name"],
                        user_id=user_id,
                        picture_url=next_lizard["picture_url"])
        (url, error) = isURLImage(next_lizard["picture_url"])
        if not error:
            url_open = urlopen(url)
            with store_context(store):
                lizard.picture.from_file(url_open)
                session.add(lizard)
                session.commit()
                url_open.close()

            new_lizard = Lizard.query.filter_by(
                user_id=user_id, name=next_lizard["name"]).order_by(
                    db.desc("creation_instant")).limit(1).all()
            new_lizard_id = new_lizard[0].id

            for next_hobby in next_lizard["hobbies"]:
                new_hobby = Hobby(name=next_hobby["name"],
                                  description=next_hobby["description"],
                                  picture_url=next_hobby["picture_url"],
                                  lizard_id=new_lizard_id,
                                  user_id=user_id)
                (url, error) = isURLImage(next_hobby["picture_url"])
Пример #14
0
app.wsgi_app = store.wsgi_middleware(app.wsgi_app)

with open("testData.JSON") as data_file:
    lizards = json.load(data_file)
    for next_lizard in lizards["lizards"]:
        lizard = Lizard(
            name=next_lizard["name"],
            user_id=user_id,
            picture_url=next_lizard["picture_url"])
        (url, error) = isURLImage(next_lizard["picture_url"])
        if not error:
            url_open = urlopen(url)
            with store_context(store):
                lizard.picture.from_file(url_open)
                session.add(lizard)
                session.commit()
                url_open.close()

            new_lizard = Lizard.query.filter_by(
                user_id=user_id, name=next_lizard["name"]).order_by(db.desc("creation_instant")).limit(1).all()
            new_lizard_id = new_lizard[0].id

            for next_hobby in next_lizard["hobbies"]:
                new_hobby = Hobby(
                    name=next_hobby["name"],
                    description=next_hobby["description"],
                    picture_url=next_hobby["picture_url"],
                    lizard_id=new_lizard_id,
                    user_id=user_id)
                (url, error) = isURLImage(next_hobby["picture_url"])
Пример #15
0
def editHobby(lizard_id, hobby_id):
    """editHobby: Displays form to edit a particular hobby's name, description,
                 and/or image url and posts that information to the database;
                 requires login and that the logged-in user is also the user
                 that created the lizard

    Arguments are derived from the url"""
    lizard = Lizard.query.filter_by(id=lizard_id).one()
    edited_hobby = Hobby.query.\
        filter_by(id=hobby_id, lizard_id=lizard_id).one()
    if request.method == "GET":
        return render_template("editHobby.html",
                               lizard=lizard,
                               hobby_id=hobby_id,
                               hobby=edited_hobby,
                               login_session=login_session)

    url = request.form.get("url")
    (url, error) = isURLImage(url)
    if error:
        return render_template("editHobby.html",
                               login_session=login_session,
                               lizard=lizard,
                               lizard_id=lizard_id,
                               hobby_id=hobby_id,
                               hobby=edited_hobby,
                               error=error)
    # try:
    #     url_open = urlopen(url)
    # except:
    #     error = "Unable to make a request to this URL: %s" % (url)
    #     return render_template("editHobby.html",
    #                           login_session=login_session,
    #                           lizard=lizard,
    #                           lizard_id=lizard_id,
    #                           hobby_id=hobby_id,
    #                           hobby=edited_hobby,
    #                           error=error)

    # The ChangeLog for editing hobbies will have an entry if any change is
    # made to an hobby
    # But the only metadata collected is the new hobby's name, even if the
    # name didn"t change
    change_log = ChangeLog(
        user_id=edited_hobby.user_id,
        lizard_name=lizard.name,
        lizard_id=lizard_id,
        hobby_name=request.form.get("name"),
        hobby_id=hobby_id,
        update_instant=datetime.datetime.utcnow(),
        action="update",
        table="hobby")

    edited_hobby.name = request.form.get("name")
    edited_hobby.description = request.form.get("description")
    edited_hobby.picture_url = url

    session.add(change_log)
    flash("Hobby %s Successfully Edited" % (edited_hobby.name))
    session.commit()
    return redirect(url_for("showHobby", lizard_id=lizard_id))
Пример #16
0
def editHobby(lizard_id, hobby_id):
    """editHobby: Displays form to edit a particular hobby's name, description,
                 and/or image url and posts that information to the database;
                 requires login and that the logged-in user is also the user
                 that created the lizard

    Arguments are derived from the url"""
    lizard = Lizard.query.filter_by(id=lizard_id).one()
    edited_hobby = Hobby.query.\
        filter_by(id=hobby_id, lizard_id=lizard_id).one()
    if request.method == "GET":
        with store_context(store):
            return render_template("editHobby.html",
                                   lizard=lizard,
                                   hobby_id=hobby_id,
                                   hobby=edited_hobby,
                                   login_session=login_session)

    url = request.form.get("url")
    (url, error) = isURLImage(url)
    if error:
        return render_template("editHobby.html",
                               login_session=login_session,
                               lizard_id=lizard_id,
                               hobby_id=hobby_id,
                               hobby=edited_hobby,
                               error=error)
    try:
        url_open = urlopen(url)
    except:
        error = "Unable to make a request to this URL: %s" % (url)
        return render_template("editHobby.html",
                               login_session=login_session,
                               lizard_id=lizard_id,
                               hobby_id=hobby_id,
                               hobby=edited_hobby,
                               error=error)

    # The ChangeLog for editing hobbies will have an entry if any change is
    # made to an hobby
    # But the only metadata collected is the new hobby's name, even if the
    # name didn"t change
    change_log = ChangeLog(
        user_id=edited_hobby.user_id,
        lizard_name=lizard.name,
        lizard_id=lizard_id,
        hobby_name=request.form.get("name"),
        hobby_id=hobby_id,
        update_instant=datetime.datetime.utcnow(),
        action="update",
        table="hobby")

    edited_hobby.name = request.form.get("name")
    edited_hobby.description = request.form.get("description")
    edited_hobby.picture_url = url

    with store_context(store):
        edited_hobby.picture.from_file(url_open)
        session.add(change_log)
        flash("Hobby %s Successfully Edited" % (edited_hobby.name))
        session.commit()
        url_open.close()
    return redirect(url_for("showHobby", lizard_id=lizard_id))