Example #1
0
def create_ingredient():
    body = json.loads(request.data)
    name = body.get('name')
    amount = body.get('amount')

    if amount is None or name is None:
        return failure_response("Incorrect input!")
    new_ingredient = Ingredient(name=name, amount=amount)
    db.session.add(new_ingredient)
    db.session.commit()
    return success_response(new_ingredient.simple_serialize(), 201)
def send_recipe(user_recipe, username):
    user = User.query.filter_by(username=username).first()
    methods = user_recipe.getlist('step')
    methodNumber = 1
    ingredNumber = 1

    recipe = Recipe(name=user_recipe['name'],
                    description=user_recipe['description'],
                    cookingtime=user_recipe['cookingtime'],
                    servings=user_recipe['servings'],
                    course=user_recipe['course'],
                    countryOfOrigin=user_recipe['origin'],
                    user_id=user.userId)
    db.session.add(recipe)

    recipe.methods = []

    for method in methods:
        method1 = Method(stepNumber=methodNumber, description=method)
        recipe.methods.append(method1)
        methodNumber += 1

    recipe.ingredients = []
    ingredients = user_recipe['ingredients-list'].split(',')

    for ingredient in ingredients:
        ingredient1 = Ingredient(name=ingredient)
        recipe.ingredients.append(ingredient1)
        ingredNumber += 1

    db.session.commit()
Example #3
0
def ingredient_details(ean):
    try:
        ean_obj = EAN.query.filter_by(ean=ean).one()
        ingredient = ean_obj.ingredient
    except NoResultFound:
        ingredient = Ingredient.fetch(ean)
        db.session.commit()
    return jsonify(ingredient=ingredient.to_json())
Example #4
0
def load():
    # Get total hits
    base_url = url = 'https://test-web-api.migros.ch/eth-hack/products?key=k0DFQajkP8AnGMF9&limit=%d&offset=%d'
    r = requests.get(base_url % (0, 0))
    data = r.json()
    if not 'total_hits' in data:
        abort(500)
    total = int(data['total_hits'])
    inserted = 0
    while inserted < total:
        r = requests.get(base_url % (100, inserted))
        data = r.json()
        if not 'products' in data:
            abort(500)
        products = data['products'].values()
        for product in products:
            eans = product['eans']
            if len(eans) > 0:
                ingredient = Ingredient()
                db.session.add(ingredient)
                ingredient.title = product['name']
                for ean_code in eans:
                    ingredient.add_ean(ean_code)
                ingredient.from_product(product)
            inserted += 1
    db.session.commit()
    return jsonify(success=True, imported=inserted)
Example #5
0
def inventory_delete(id_or_ean):
    inventory = Inventory.get_current()
    ingredient = Ingredient.get_by_id_or_ean({
        'id': id_or_ean,
        'ean': id_or_ean
    })
    if not ingredient:
        return
    inventory.remove_ingredient(ingredient)
    db.session.commit()
    pubnub.publish('inventory', {'update': True})
Example #6
0
def create_course():
    body = json.loads(request.data)
    name = body.get('name')
    amount = body.get('amount')

    if amount is None or name is None:
        return failure_response("Incorrect input!")
    new_ingredient = Ingredient(name=name, amount=amount)
    db.session.add(new_ingredient)
    db.session.commit()

    recipes = [
        r.simple_serialize() for r in Recipe.query.filter_by(name=name).all()
    ]
    if recipes is None:
        return failure_response("Recipe not found!")

    return success_response(recipes, 201)
def update_recipe(user_recipe, username, recipeId):
    user = search_for_existing(username)
    recipe = Recipe.query.filter_by(recipeId=recipeId).first()
    methods = Method.query.filter_by(recipe_id=recipe.recipeId).all()
    ingredients = Ingredient.query.filter_by(recipe_id=recipe.recipeId).all()
    methodNumber = 1
    ingredNumber = 1
    methodsGot = user_recipe.getlist('step')

    for method in methods:
        db.session.delete(method)
    for ingredient in ingredients:
        db.session.delete(ingredient)

    #db.session.delete(ingredients)

    recipe.name = user_recipe['name'],
    recipe.description = user_recipe['description'],
    recipe.cookingtime = user_recipe['cookingtime'],
    recipe.servings = user_recipe['servings'],
    recipe.course = user_recipe['course'],
    recipe.countryOfOrigin = user_recipe['origin'],
    recipe.user_id = user.userId
    db.session.commit()

    methodsGot = user_recipe.getlist('step')
    recipe.methods = []

    for methodGot in methodsGot:
        method1 = Method(stepNumber=methodNumber, description=methodGot)
        recipe.methods.append(method1)
        methodNumber += 1

    recipe.ingredients = []
    ingredientsGot = user_recipe['ingredients-list'].split(',')

    for ingredientGot in ingredientsGot:
        ingredient1 = Ingredient(name=ingredientGot)
        recipe.ingredients.append(ingredient1)
        ingredNumber += 1

    db.session.commit()
Example #8
0
def shopping_list_details():
    if request.method == 'DELETE':
        return delete_shoppinglist()
    elif request.method == 'POST':
        shopping_list = ShoppingList.query.first()
        data = request.get_json(force=True)
        if not 'ingredients' in data:
            abort(400)
        for item in data['ingredients']:
            ingredient = Ingredient.get_by_id_or_ean(item)
            shopping_list.add_ingredient(ingredient, item.get('amount'),
                                         item.get('unit'))
        db.session.commit()
        pubnub.publish('shopping_list', {'update': True})
        return jsonify(ok=True)
    else:
        shopping_list = db.session.query(ShoppingList).options(
            joinedload(ShoppingList.shopping_list_ingredients,
                       ShoppingListIngredients.ingredient,
                       Ingredient.eans)).first()
    return jsonify(ingredients=list(i.to_json()
                                    for i in shopping_list.ingredients if i))
Example #9
0
def inventory_details():
    inventory = Inventory.get_current()
    if request.method == 'POST':
        data = request.get_json(force=True)
        if not 'inventory' in data:
            abort(400)
        for item in data['inventory']:
            ingredient = Ingredient.get_by_id_or_ean(item)
            if not ingredient:
                continue
            inventory.add_ingredient(ingredient, None, None)
        db.session.commit()
        pubnub.publish('inventory', {'update': True})
        return jsonify(inventory=inventory.to_json())
    elif request.method == 'GET':
        return jsonify(inventory=inventory.to_json())
    elif request.method == 'DELETE':
        data = request.get_json(force=True)
        if not 'inventory' in data:
            abort(400)
        eans = []
        ids_remove = []
        for item in data['inventory']:
            if 'id' in item:
                ids_remove.append(item['id'])
            else:
                eans.append(item['ean'])
        ids = db.session.query(EAN.ingredient_id).filter(
            EAN.ean.in_(eans)).all()
        ids = [id[0] for id in ids] + ids_remove
        db.session.query(InventoryIngredients).filter(
            InventoryIngredients.ingredient_id.in_(ids_remove)).delete(
                synchronize_session='fetch')
        db.session.commit()
        pubnub.publish('inventory', {'update': True})
        return jsonify(ok=True)