Example #1
0
def update_shopping(id):
  session = request.environ["beaker.session"]
  shopping = Shopping_List(id)
  if shopping.shopping["user_id"] != session["user"]:
    if not "flash" in session:
      session["flash"] = {} 
    session["flash"]["error"]="You do not have permission to access that resource"
    redirect("/")
  else:
    ingredients = []
    for i in range(10):
      name = request.forms.get("ingredient[%d][name]" % i)
      unit = request.forms.get("ingredient[%d][unit]" % i)
      quantity = request.forms.get("ingredient[%d][quantity]" % i)
      price = request.forms.get("ingredient[%d][price]" % i)
      if name != "" and quantity != "" and int(quantity) > 0:
        ingredients.append({"name":name,"unit":unit,"quantity":quantity,"price":price})
    shopping.shopping["ingredients"] = ingredients
    shopping.save()
    if not "flash" in session:
      session["flash"] = {} 
    session["flash"]["success"]="Shopping List updated successfully"
    redirect("/shopping/"+id)