示例#1
0
def edit_recipe(user_id, id, recipe_id, **kwargs):
    """This route handles update a recipe by id"""

    title = str(request.data.get('title', '')).strip()
    description = str(request.data.get('description', '')).strip()
    identity = Category.find_user_by_id(id, user_id)
    if not identity:
        return jsonify(
            {"message": "You don't have"
             " that recipe in that category"}), 400
    result2 = valid_recipe_title(title)
    if result2:
        return jsonify(result2), 400
    title = title.lower()
    result = Recipe.find_by_id(title, id)
    if result and result.description == description:
        return jsonify({"message": "Recipe already exists"}), 400
    recipe = Recipe.find_recipe_by_id(recipe_id, id)
    if not recipe:
        return jsonify({"message": "No recipes" " with that id to edit "}), 404
    title = str(request.data.get('title', '')).lower()
    description = str(request.data.get('description', ''))
    recipe.title = title
    recipe.description = description
    recipe.save()
    return recipe.json(), 200
示例#2
0
def add_recipes(user_id, id, **kwargs):
    """This route handles posting a recipe"""

    if request.method == "POST":
        title = str(request.data.get('title', '')).strip().lower()
        title = re.sub(' +', ' ', title)
        description = str(request.data.get('description', ''))
        result1 = valid_recipe_title(title)
        if result1:
            return jsonify(result1), 400
        identity = Category.find_user_by_id(id, user_id)
        if not identity:
            return jsonify({"message": "Category doesn't exist"}), 400
        result = Recipe.find_by_id(title, id)
        if result:
            return jsonify({"message": "Recipe already exists"}), 400
        if title:
            recipe = Recipe(title=title,
                            description=description,
                            category_identity=id)
            recipe.save()
            return recipe.json(), 201