Example #1
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)
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 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)
Example #4
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()