def get(self): if request.method != "GET": return utils.RecipeBuilder.create_error_response(405, "Wrong method", "GET method required") users = db.session.query(models.User).all() body = utils.RecipeBuilder(users=[]) for user in users: useritem = utils.RecipeBuilder( username=user.username ) useritem.add_control("self", "/api/users/{}/".format(user.username)) useritem.add_control("profile", utils.USER_PROFILE) body["users"].append(useritem) body.add_control_all_users() body.add_control_add_user() return Response(json.dumps(body), 200, mimetype=utils.MASON)
def get(self, recipe_id, step_id): if request.method != "GET": return utils.RecipeBuilder.create_error_response( 405, "wrong method", "GET method required") db_recipe = models.RecipeInstructionStep.query.filter_by( recipe_id=recipe_id, step=step_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not Found", "No user was found with the username {}".format(recipe_id)) body = utils.RecipeBuilder(recipe_id=db_recipe.recipe_id, step=db_recipe.step, text=db_recipe.text) body.add_namespace("recipe_steps", utils.LINK_RELATIONS_URL) body.add_control( "self", api.api.url_for(Step, recipe_id=recipe_id, step_id=step_id)) body.add_control("profile", utils.STEPS_PROFILE) body.add_control("collection", api.api.url_for(StepCollection, recipe_id=recipe_id)) body.add_control_delete_step(recipe_id, step_id) body.add_control_edit_step(recipe_id, step_id) body.add_control("storage:recipe_steps-all", api.api.url_for(StepCollection, recipe_id=recipe_id)) return Response(json.dumps(body), 200, mimetype=utils.MASON)
def put(self, recipe_id, step_id): if request.method != "PUT": return utils.RecipeBuilder.create_error_response( 405, "wrong method", "PUT method required") db_recipe = models.RecipeInstructionStep.query.filter_by( recipe_id=recipe_id, step=step_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not found", "No recipe was found with the name {}".format(recipe_id)) try: step = int(request.json["step"]) text = str(request.json["text"]) except KeyError: return utils.RecipeBuilder.create_error_response( 400, "Missing fields", "Incomplete request - missing fields") except ValueError: return utils.RecipeBuilder.create_error_response( 400, "Invalid input", "Weight and price must be numbers") except TypeError: return utils.RecipeBuilder.create_error_response( 415, "Invalid content", "request content type must be JSON") body = utils.RecipeBuilder(recipe_id=db_recipe.recipe_id, recipe=db_recipe.recipe, step=db_recipe.step, text=db_recipe.text) db_recipe.step = step db_recipe.text = text db.session.commit() url = api.api.url_for(Step, recipe_id=db_recipe.recipe_id, step_id=db_recipe.step) return Response(headers={"Location": url}, status=204)
def put(self, username): if request.method != "PUT": return utils.RecipeBuilder.create_error_response(405, "Wrong method", "PUT method required") db_user = models.User.query.filter_by(username=username).first() if db_user is None: return utils.RecipeBuilder.create_error_response(404, "Not Found", "No user was found with the username {}".format(username)) try: newusername = str(request.json["username"]) except KeyError: return utils.RecipeBuilder.create_error_response(400, "Missing fields", "Incomplete request - missing fields") except ValueError: return utils.RecipeBuilder.create_error_response(400, "Invalid input", "Username must be a string") except TypeError: return utils.RecipeBuilder.create_error_response(415, "Invalid content", "Request content must be JSON") if (models.User.query.filter_by(username=newusername).first() is not None) and username!=newusername: return utils.RecipeBuilder.create_error_response(409, "Duplicate content", "User already exists") body = utils.RecipeBuilder( username=db_user.username ) db_user.username = newusername db.session.commit() url = api.api.url_for(Users, username=newusername) return Response(headers={ "Location": url }, status=204 )
def index(): body = utils.RecipeBuilder() body.add_control_all_recipes() body.add_control_add_recipe() body.add_control_all_users() body.add_control_add_user() return Response(json.dumps(body), 200, mimetype=utils.MASON)
def get(self, username, list_id): if request.method != "GET": return utils.RecipeBuilder.create_error_response( 405, "wrong method", "GET method required") if not models.User.query.filter_by(username=username).all(): return utils.RecipeBuilder.create_error_response( 404, "Not found", "No user was found with the username {}".format(username)) db_recipe = models.ShoppingList.query.filter_by(owner_name=username, id=list_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not found", "User {} has no shoppinglist with id {}".format( username, list_id)) body = utils.RecipeBuilder(notes=db_recipe.notes, owner=db_recipe.owner_name, ingredients=[]) ingredients = models.ShoppingListIngredient.query.filter_by( shopping_list_id=list_id).all() for ingredient in ingredients: item = utils.RecipeBuilder( name=ingredient.ingredient.name, description=ingredient.ingredient.description, amount=ingredient.amount, unit=ingredient.unit) body["ingredients"].append(item) body.add_namespace("shoppinglists", utils.LINK_RELATIONS_URL) body.add_control( "self", api.api.url_for(Shoppinglist, username=username, list_id=list_id)) body.add_control("profile", utils.SHOPLIST_PROFILE) body.add_control( "collection", api.api.url_for(ShoppingListCollection, username=username)) body.add_control_add_shoplist_ingredient(username, list_id) body.add_control_all_shoplist_ingredients(username, list_id) body.add_control_delete_shoppinglist(username, list_id) body.add_control_edit_shoppinglist(username, list_id) body.add_control( "storage:shoppinglists-all", api.api.url_for(ShoppingListCollection, username=username)) return Response(json.dumps(body), 200, mimetype=utils.MASON)
def get(self, recipe_id): if request.method != "GET": return utils.RecipeBuilder.create_error_response( 405, "Invalid method", "GET method required") steps = models.RecipeInstructionStep.query.filter_by( recipe_id=recipe_id).all() body = utils.RecipeBuilder(steps=[]) for step in steps: item = utils.RecipeBuilder(step=step.step, text=step.text) item.add_control( "self", "/api/recipes/{}/steps/{}/".format(recipe_id, step.step)) item.add_control("profile", utils.STEPS_PROFILE) body["steps"].append(item) body.add_control_all_steps(recipe_id) body.add_control_add_step(recipe_id) return Response(json.dumps(body), 200, mimetype=utils.MASON)
def get(self): if request.method != "GET": return utils.RecipeBuilder.create_error_response( 405, "Invalid method", "GET method required") recipes = db.session.query(models.Recipe).all() body = utils.RecipeBuilder(recipes=[]) for recipe in recipes: item = utils.RecipeBuilder(name=recipe.name, description=recipe.description, recipeYield=recipe.recipeYield, cookTime=recipe.cookTime, recipeCategory=recipe.recipeCategory, author=recipe.author, datePublished=str(recipe.datePublished)) item.add_control("self", "/api/recipes/{}/".format(recipe.id)) item.add_control("profile", utils.REC_PROFILE) body["recipes"].append(item) body.add_control_all_recipes() body.add_control_add_recipe() return Response(json.dumps(body), 200, mimetype=utils.MASON)
def get(self, username): if request.method != "GET": return utils.RecipeBuilder.create_error_response( 405, "Invalid method", "GET method required") body = utils.RecipeBuilder(shoppinglists=[]) shoppinglists = models.ShoppingList.query.filter_by( owner_name=username).all() if not models.User.query.filter_by(username=username).all(): return utils.RecipeBuilder.create_error_response( 404, "Not found", "No user was found with the username {}".format(username)) for shoppinglist in shoppinglists: item = utils.RecipeBuilder(notes=shoppinglist.notes) item.add_control( "self", "/api/users/{}/shoppinglist/{}/".format( username, shoppinglist.id)) item.add_control("profile", utils.SHOPLIST_PROFILE) body["shoppinglists"].append(item) body.add_control_all_shoppinglists(username) body.add_control_add_shoppinglist(username) return Response(json.dumps(body), 200, mimetype=utils.MASON)
def delete(self, username): if request.method != "DELETE": return utils.RecipeBuilder.create_error_response(405, "Wrong method", "DELETE method required") db_user = models.User.query.filter_by(username=username).first() print(db_user) if db_user is None: return utils.RecipeBuilder.create_error_response(404, "Not Found", "No user was found with the username {}".format(username)) body = utils.RecipeBuilder( username=db_user.username ) db.session.delete(db_user) db.session.commit() return Response(json.dumps(body), 204, mimetype=utils.MASON)
def put(self, recipe_id): if request.method != "PUT": return utils.RecipeBuilder.create_error_response( 405, "wrong method", "PUT method required") db_recipe = models.Recipe.query.filter_by(id=recipe_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not Found", "No recipe was found with id {}".format(recipe_id)) try: name = str(request.json["name"]) description = str(request.json["description"]) recipeYield = str(request.json["recipeyield"]) cookTime = str(request.json["cooktime"]) recipeCategory = str(request.json["category"]) author = str(request.json["author"]) datePublished = datetime.datetime.now() except KeyError: return utils.RecipeBuilder.create_error_response( 400, "Missing fields", "Incomplete request - missing fields") except ValueError: return utils.RecipeBuilder.create_error_response( 400, "Invalid input", "Weight and price must be numbers") except TypeError: return utils.RecipeBuilder.create_error_response( 415, "Invalid content", "request content type must be JSON") body = utils.RecipeBuilder(name=db_recipe.name, description=db_recipe.description, recipeYield=db_recipe.recipeYield, cookTime=db_recipe.cookTime, recipeCategory=db_recipe.recipeCategory, author=db_recipe.author, datePublished=str(db_recipe.datePublished)) db_recipe.name = name db_recipe.description = description db_recipe.recipeYield = name db_recipe.cookTime = name db_recipe.recipeCategory = name db_recipe.author = author db_recipe.datePublished = datePublished db.session.commit() url = api.api.url_for(RecipeItem, recipe_id=recipe_id) return Response(headers={"Location": url}, status=204)
def delete(self, recipe_id, step_id): if request.method != "DELETE": return utils.RecipeBuilder.create_error_response( 405, "Invalid method", "DELETE method required") db_recipe = models.RecipeInstructionStep.query.filter_by( recipe_id=recipe_id, step=step_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not Found", "No user was found with the username {}".format(recipe_id)) body = utils.RecipeBuilder(recipe_id=db_recipe.recipe_id, step=db_recipe.step, text=db_recipe.text) db.session.delete(db_recipe) db.session.commit() return Response(json.dumps(body), 204, mimetype=utils.MASON)
def get(self, username): if request.method != "GET": return utils.RecipeBuilder.create_error_response(405, "Wrong method", "GET method required") db_user = models.User.query.filter_by(username=username).first() if db_user is None: return utils.RecipeBuilder.create_error_response(404, "Not Found", "No user was found with the username {}".format(username)) body = utils.RecipeBuilder( username=db_user.username ) body.add_namespace("users", utils.LINK_RELATIONS_URL) body.add_control("self", api.api.url_for(Users, username=username)) body.add_control("profile", utils.USER_PROFILE) body.add_control("collection", "/api/users/") body.add_control_delete_user(username) body.add_control_edit_user(username) body.add_control("storage:users-all", api.api.url_for(Users, username=username)) body.add_control_add_shoppinglist(username) body.add_control_all_shoppinglists(username) return Response(json.dumps(body), 200, mimetype=utils.MASON)
def delete(self, username, list_id): if request.method != "DELETE": return utils.RecipeBuilder.create_error_response( 405, "Invalid method", "DELETE method required") if not models.User.query.filter_by(username=username).all(): return utils.RecipeBuilder.create_error_response( 404, "Not found", "No user was found with the username {}".format(username)) db_recipe = models.ShoppingList.query.filter_by(owner_name=username, id=list_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not found", "User {} has no shoppinglist with id {}".format( username, list_id)) body = utils.RecipeBuilder(notes=db_recipe.notes, owner=db_recipe.owner_name) db.session.delete(db_recipe) db.session.commit() return Response(json.dumps(body), 204, mimetype=utils.MASON)
def delete(self, recipe_id): if request.method != "DELETE": return utils.RecipeBuilder.create_error_response( 405, "Invalid method", "DELETE method required") db_recipe = models.Recipe.query.filter_by(id=recipe_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not Found", "No recipe was found with id {}".format(recipe_id)) body = utils.RecipeBuilder( name=db_recipe.name, description=db_recipe.description, recipeYield=db_recipe.recipeYield, cookTime=db_recipe.cookTime, recipeCategory=db_recipe.recipeCategory, author=db_recipe.author, datePublished=str(db_recipe.datePublished), ) db.session.delete(db_recipe) db.session.commit() return Response(json.dumps(body), 204, mimetype=utils.MASON)
def put(self, username, list_id): if request.method != "PUT": return utils.RecipeBuilder.create_error_response( 405, "wrong method", "PUT method required") if not models.User.query.filter_by(username=username).all(): return utils.RecipeBuilder.create_error_response( 404, "Not found", "No user was found with the username {}".format(username)) db_recipe = models.ShoppingList.query.filter_by(owner_name=username, id=list_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not found", "User {} has no shoppinglist with id {}".format( username, list_id)) try: notes = str(request.json["notes"]) except KeyError: return utils.RecipeBuilder.create_error_response( 400, "Missing fields", "Incomplete request - missing fields") except ValueError: return utils.RecipeBuilder.create_error_response( 400, "Invalid input", "Weight and price must be numbers") except TypeError: return utils.RecipeBuilder.create_error_response( 415, "Invalid content", "request content type must be JSON") body = utils.RecipeBuilder(notes=db_recipe.notes, owner=db_recipe.owner_name) db_recipe.notes = notes db.session.commit() url = api.api.url_for(Shoppinglist, username=username, list_id=id) return Response(headers={"Location": url}, status=204)
def get(self, recipe_id): if request.method != "GET": return utils.RecipeBuilder.create_error_response( 405, "wrong method", "GET method required") db_recipe = models.Recipe.query.filter_by(id=recipe_id).first() if db_recipe is None: return utils.RecipeBuilder.create_error_response( 404, "Not Found", "No recipe was found with id {}".format(recipe_id)) if db_recipe.nutrition_information is not None: body = utils.RecipeBuilder( name=db_recipe.name, description=db_recipe.description, recipeYield=db_recipe.recipeYield, cookTime=db_recipe.cookTime, recipeCategory=db_recipe.recipeCategory, author=db_recipe.author, datePublished=str(db_recipe.datePublished), calories=db_recipe.nutrition_information.calories, carbohydrateContent=db_recipe.nutrition_information. carbohydrateContent, cholesterolContent=db_recipe.nutrition_information. cholesterolContent, fatContent=db_recipe.nutrition_information.fatContent, fiberContent=db_recipe.nutrition_information.fiberContent, proteinContent=db_recipe.nutrition_information.proteinContent, saturatedFatContent=db_recipe.nutrition_information. saturatedFatContent, sodiumContent=db_recipe.nutrition_information.sodiumContent, sugarContent=db_recipe.nutrition_information.sugarContent, transFatContent=db_recipe.nutrition_information. transFatContent, unsaturatedFatContent=db_recipe.nutrition_information. unsaturatedFatContent, steps=[], ingredients=[]) else: body = utils.RecipeBuilder(name=db_recipe.name, description=db_recipe.description, recipeYield=db_recipe.recipeYield, cookTime=db_recipe.cookTime, recipeCategory=db_recipe.recipeCategory, author=db_recipe.author, datePublished=str( db_recipe.datePublished), steps=[], ingredients=[]) steps = models.RecipeInstructionStep.query.filter_by( recipe_id=recipe_id).all() for step in steps: item = utils.RecipeBuilder(step=step.step, text=step.text) body["steps"].append(item) ingredients = models.RecipeIngredient.query.filter_by( recipe_id=recipe_id).all() for ingredient in ingredients: item = utils.RecipeBuilder( name=ingredient.ingredient.name, description=ingredient.ingredient.description, amount=ingredient.amount, unit=ingredient.unit) body["ingredients"].append(item) body.add_namespace("recipes", utils.LINK_RELATIONS_URL) body.add_control("self", api.api.url_for(RecipeItem, recipe_id=recipe_id)) body.add_control("profile", utils.REC_PROFILE) body.add_control("collection", "/api/products/") body.add_control_delete_recipe(recipe_id) body.add_control_edit_recipe(recipe_id) body.add_control("storage:recipes-all", api.api.url_for(RecipeItem, recipe_id=recipe_id)) body.add_control_add_recipe_ingredient(recipe_id) body.add_control_all_recipe_ingredients(recipe_id) body.add_control_add_step(recipe_id) body.add_control_all_steps(recipe_id) return Response(json.dumps(body), 200, mimetype=utils.MASON)