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"])
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"))
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"))
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
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)
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"))
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"))