def post(self): data = json.loads(request.data) user = g.user recipe = Recipe() recipe.name = data["name"] for ingredient in data["ingredients"]: irec = RecipeIngredient() irec.name = ingredient recipe.ingredients.append(irec) recipe.instructions = data["instructions"] recipe.author = data["author"] user.recipes.append(recipe) #db.session.add(recipe) try: db.session.commit() except Exception,e: print "error adding new record" db.session.rollback()
def put(self, r_id): data = json.loads(request.data) recipe = db.session.query(Recipe).filter_by(id=r_id).first() recipe.name = data["name"] #recipe.ingredients = data["ingredients"] for rec in recipe.ingredients: db.session.delete(rec) for ingredient in data["ingredients"]: irec = RecipeIngredient() irec.name = ingredient recipe.ingredients.append(irec) recipe.instructions = data["instructions"] recipe.author = data["author"] #maybe don't do this db.session.add(recipe) try: db.session.commit() except Exception,e: print "error adding new record" db.session.rollback()