예제 #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 get_recipes(user_id, id, **kwargs):
    """This route handles getting recipes"""

    page = request.args.get('page', 1, type=int)
    limit = request.args.get('limit', 6, type=int)
    search_query = str(request.args.get('q', '')).lower()
    identity = Category.find_user_by_id(id, user_id)
    if not identity:
        return jsonify(
            {"message": "You don't have"
             " the recipes in that category"}), 400
    recipes = Recipe.query.filter(Recipe.category_identity == id)
    if search_query:
        recipes = recipes.filter(
            or_(
                Recipe.title.like('%' + search_query.strip().lower() + '%'),
                Recipe.description.like('%' + search_query.strip().lower() +
                                        '%')))
    recipes = recipes.paginate(page=page, per_page=limit, error_out=False)
    results = []
    for recipe in recipes.items:
        results.append({
            'recipe': recipe.json(),
        })
    pagination_details = {
        'Next_page': recipes.next_num,
        'current_page': recipes.page,
        'Previous_page': recipes.prev_num,
        'total_Items': recipes.total,
        'total_pages': recipes.pages,
    }
    if results:
        return jsonify({'recipes': results, **pagination_details}), 200
    return jsonify({"message": "No recipes found"}), 404
예제 #3
0
def edit_category(user_id, id, **kwargs):
    """This route handles updating categories by id"""

    name = str(request.data.get('name')).strip()
    result2 = valid_category(name)
    if result2:
        return jsonify(result2), 400
    name = name.title()
    result = Category.find_by_name(name, user_id)
    if result:
        return jsonify({"message": "name already exists"}), 400
    category = Category.find_user_by_id(id, user_id)
    if not category:
        return jsonify({"message": "No category found to edit"}), 404

    name = str(request.data.get('name', ''))
    category.name = name
    category.save()
    response2 = category.category_json()
    response = {
        'message': 'Category has been updated',
        'newcategory': response2,
        'Recipes': url_for('recipe.get_recipes',
                           id=category.id,
                           _external=True)
    }
    return make_response(jsonify(response)), 200
예제 #4
0
def delete_category(user_id, id, **kwargs):
    """This route handles deleting categories by id"""

    category = Category.find_user_by_id(id, user_id)
    if not category:
        return jsonify({"message": "No category to delete"}), 404
    if request.method == "DELETE":
        category.delete()
        return {"message": "category {} deleted".format(category.name)}, 200
예제 #5
0
def get_recipe_by_id(user_id, id, recipe_id, **kwargs):
    """This route handles getting a recipe by id"""
    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
    recipe = Recipe.find_recipe_by_id(recipe_id, id)
    if not recipe:
        return jsonify({"message": "No recipes with" " that id to get"}), 400
    return recipe.json(), 200
예제 #6
0
def delete_recipe(user_id, id, recipe_id, **kwargs):
    """This route handles deleting a recipe by id"""

    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
    recipe = Recipe.find_recipe_by_id(recipe_id, id)
    if not recipe:
        return jsonify({"message": "No recipes with"
                        " that id to delete "}), 404
    recipe.delete()
    return {
        "message": "recipe {} deleted"
        " successfully".format(recipe.title)
    }, 200
예제 #7
0
def get_category_by_id(user_id, id, **kwargs):
    """This route handles getting categories by id"""

    category = Category.find_user_by_id(id, user_id)
    if not category:
        return jsonify({"message": "No category found by id"}), 404
    else:
        response3 = category.category_json()
        response = {
            "message":
            "category {} found".format(category.id),
            'category':
            response3,
            'Recipes':
            url_for('recipe.get_recipes', id=category.id, _external=True)
        }
        return make_response(jsonify(response)), 200
예제 #8
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