예제 #1
0
def farmDelete(farm_id):
    """Delete an existing farm."""
    farm = db_session.query(Farm).filter_by(id=farm_id).one()

    user_id = login_session.get("user_id")
    username = login_session.get("username")

    # Check that the login_session user_id matches the farm user_id:
    if user_id == farm.user_id:
        if request.method == "POST":
            if farm.picture:
                imageDeleteProfile(filename=farm.picture)

            db_session.delete(farm)
            db_session.commit()
            flash("Farm Successfully Deleted: %s" % (farm.name))
            return redirect(url_for("farm.farmsManage"))

        else:
            return render_template("farmDelete.html",
                                   farm=farm,
                                   username=username)

    else:
        return redirect(url_for("catalog.catalogShow", farm_id=farm_id))
예제 #2
0
def deleteCatalogItem(farm_id, item_id):
    """Delete an existing catalog item."""
    farm = db_session.query(Farm).filter_by(id=farm_id).one()
    item = db_session.query(CatalogItem).filter_by(id=item_id).one()

    user_id = login_session.get("user_id")
    username = login_session.get("username")

    if user_id == item.user_id:
        if request.method == "POST":
            db_session.delete(item)
            db_session.commit()

            if item.picture:
                imageDeleteItem(filename=item.picture)

            flash("Item Successfully Deleted: %s" % (item.name))
            return redirect(url_for("catalog.catalogManage", farm_id=farm_id))

        else:
            return render_template("catalogItemDelete.html",
                                   farm=farm,
                                   item=item,
                                   username=username)

    return redirect(url_for("error.errorShow"))
예제 #3
0
def farmNew():
    """Create a new farm and add it to the database."""
    user_id = login_session.get("user_id")
    username = login_session.get("username")

    # Check that the user_id argument matches the login_session user_id.
    if request.method == "POST":
        if not request.form["name"]:
            return render_template("farmNew.html",
                                   name_error=True,
                                   username=username)

        newFarm = Farm(name=request.form["name"],
                       location=request.form["location"],
                       description=request.form["description"],
                       picture=None,
                       user_id=login_session["user_id"])
        db_session.add(newFarm)
        db_session.commit()

        picture = request.files["picture"]
        if picture:
            db_session.refresh(newFarm)
            picture_name = imageUploadProfile(farm_id=newFarm.id, file=picture)
            newFarm.picture = picture_name
            db_session.add(newFarm)
            db_session.commit()

        flash("New Farm %s Successfully Created" % newFarm.name)
        return redirect(url_for("farm.farmsManage"))

    else:
        return render_template("farmNew.html", username=username)

    return render_template("farmNew.html", error=request.files["picture"])
예제 #4
0
def eventEdit(farm_id, event_id):
    """Edit an event."""
    farm = db_session.query(Farm).filter_by(id=farm_id).one()
    event = db_session.query(Event).filter_by(id=event_id).one()

    user_id = login_session.get("user_id")
    username = login_session.get("username")

    if user_id == farm.user_id:
        if request.method == "POST":
            if not request.form["name"]:
                return render_template("eventEdit.html",
                                       farm=farm,
                                       event=event,
                                       name_error=True,
                                       username=username)

            event.name = request.form["name"]
            event.description = request.form["description"]

            db_session.add(event)
            db_session.commit()
            flash("Event Successfully Edited: %s" % event.name)
            return redirect(
                url_for("event_manage.eventManage", farm_id=farm.id))

        else:
            return render_template("eventEdit.html",
                                   farm=farm,
                                   event=event,
                                   username=username)

    else:
        return redirect(url_for("error.errorShow"))
예제 #5
0
def eventNew(farm_id):
    """Create a new event."""
    farm = db_session.query(Farm).filter_by(id=farm_id).one()

    user_id = login_session.get("user_id")
    username = login_session.get("username")

    if user_id == farm.user_id:
        if request.method == "POST":
            if not request.form["name"]:
                return render_template("eventNew.html",
                                       farm=farm,
                                       name_error=True,
                                       username=username)

            event = Event(name=request.form["name"],
                          description=request.form["description"],
                          farm_id=farm.id,
                          user_id=user_id)

            db_session.add(event)
            flash("New Event Successfully Created: %s" % event.name)
            db_session.commit()
            return redirect(
                url_for("event_manage.eventManage", farm_id=farm.id))

        else:
            return render_template("eventNew.html",
                                   farm=farm,
                                   username=username)

    else:
        return redirect(url_for("error.errorShow"))
예제 #6
0
def eventDelete(farm_id, event_id):
    """Delete an event."""
    farm = db_session.query(Farm).filter_by(id=farm_id).one()
    event = db_session.query(Event).filter_by(id=event_id).one()

    user_id = login_session.get("user_id")
    username = login_session.get("username")

    if user_id == farm.user_id:
        if request.method == "POST":
            db_session.delete(event)
            db_session.commit()

            flash("Event Successfully Deleted: %s" % (event.name))
            return redirect(
                url_for("event_manage.eventManage", farm_id=farm.id))

        else:
            return render_template("eventDelete.html",
                                   farm=farm,
                                   event=event,
                                   username=username)

    else:
        return redirect(url_for("error.errorShow"))
예제 #7
0
def createUser(login_session):
    """Create a new user and add them to the database."""
    newUser = User(name=login_session["g_username"],
                   email=login_session["email"])
    db_session.add(newUser)
    db_session.commit()
    user = db_session.query(User).filter_by(email=login_session["email"]).one()
    return user.id
예제 #8
0
def profileEdit(farm_id):
    """Edit the profile for a given farm."""
    farm = db_session.query(Farm).filter_by(id=farm_id).one()

    user_id = login_session.get("user_id")
    username = login_session.get("username")

    if user_id != farm.user_id:
        return redirect(url_for("error.errorShow"))

    if user_id == farm.user_id:
        if request.method == "POST":
            if not request.form["name"]:
                return render_template("profileEdit.html",
                                       farm=farm,
                                       name_error=True,
                                       username=username)

            farm.name = request.form["name"]
            farm.location = request.form["location"]
            farm.description = request.form["description"]

            f = request.form
            existing_pic = farm.picture
            remove_pic = "removepicture" in f.keys() and \
                            f["removepicture"] == "no-pic"
            new_pic = request.files["picture"]

            if existing_pic:
                if remove_pic:
                    imageDeleteProfile(filename=farm.picture)
                    farm.picture = None

                elif new_pic:
                    imageDeleteProfile(filename=farm.picture)
                    farm.picture = imageUploadProfile(farm_id=farm.id,
                                                      file=new_pic)

            elif new_pic:
                farm.picture = imageUploadProfile(farm_id=farm.id,
                                                  file=new_pic)

            db_session.add(farm)
            db_session.commit()
            flash("Profile Successfully Edited")
            return redirect(url_for("profile.profileManage", farm_id=farm_id))

        else:
            return render_template("profileEdit.html",
                                   farm=farm,
                                   username=username)
예제 #9
0
def newItem(farm_id):
    """Create a new catalog item and add it to the database."""
    farm = db_session.query(Farm).filter_by(id=farm_id).one()

    user_id = login_session.get("user_id")
    username = login_session.get("username")

    if user_id == farm.user_id:
        if request.method == "POST":
            name = request.form.get("name")
            description = request.form.get("description")
            price = request.form.get("price")
            category = request.form.get("category")

            name_error = None
            category_error = None

            if not name:
                name_error = True
            if not category:
                category_error = True

            if name_error or category_error:
                return render_template("catalogItemNew.html",
                                       name_error=name_error,
                                       category_error=category_error,
                                       farm=farm,
                                       itemCategories=itemCategories,
                                       username=username)

            newItem = CatalogItem(name=name,
                                  description=description,
                                  price=price,
                                  category=category,
                                  farm_id=farm_id,
                                  user_id=farm.user_id,
                                  picture=None)
            db_session.add(newItem)
            db_session.commit()

            picture = request.files["picture"]
            if picture:
                db_session.refresh(newItem)
                picture_name = imageUploadItem(farm_id=farm_id,
                                               item_id=newItem.id,
                                               file=picture)
                newItem.picture = picture_name
                db_session.add(newItem)
                db_session.commit()

            flash("New Item Successfully Created: %s" % (newItem.name))
            return redirect(url_for("catalog.catalogManage", farm_id=farm_id))

        else:
            return render_template("catalogItemNew.html",
                                   farm=farm,
                                   itemCategories=itemCategories,
                                   username=username)

    else:
        return redirect(url_for("error.errorShow"))
예제 #10
0
def editCatalogItem(farm_id, item_id):
    """Make changes to an existing catalog item."""
    farm = db_session.query(Farm).filter_by(id=farm_id).one()
    item = db_session.query(CatalogItem).filter_by(id=item_id).one()

    user_id = login_session.get("user_id")
    username = login_session.get("username")

    if user_id == item.user_id:
        if request.method == "POST":
            name = request.form.get("name")
            description = request.form.get("description")
            price = request.form.get("price")
            category = request.form.get("category")

            name_error = None
            category_error = None

            if not name:
                name_error = True
            if not category:
                category_error = True

            if name_error or category_error:
                return render_template("catalogItemEdit.html",
                                       name_error=name_error,
                                       category_error=category_error,
                                       farm=farm,
                                       item=item,
                                       itemCategories=itemCategories,
                                       username=username)

            item.name = request.form["name"]
            item.description = request.form["description"]
            item.price = request.form["price"]
            item.category = request.form["category"]

            f = request.form
            existing_pic = item.picture
            remove_pic = "removepicture" in f.keys() and \
                            f["removepicture"] == "no-pic"
            new_pic = request.files["picture"]

            if existing_pic:
                if remove_pic:
                    imageDeleteItem(filename=item.picture)
                    item.picture = None

                elif new_pic:
                    imageDeleteItem(filename=item.picture)
                    item.picture = imageUploadItem(farm_id=farm.id,
                                                   item_id=item.id,
                                                   file=new_pic)

            elif new_pic:
                item.picture = imageUploadItem(farm_id=farm.id,
                                               item_id=item.id,
                                               file=new_pic)

            db_session.add(item)
            db_session.commit()
            flash("Item Successfully Edited: %s" % (item.name))
            return redirect(url_for("catalog.catalogManage", farm_id=farm_id))

        else:
            return render_template("catalogItemEdit.html",
                                   farm=farm,
                                   item=item,
                                   itemCategories=itemCategories,
                                   username=username)

    return redirect(url_for("error.errorShow"))