Esempio n. 1
0
def create_ingredient(data: dict):

    try:
        schema = IngredientSchema()
        validated_data = schema.load(data)
        new_ingredient = Ingredient(**validated_data)

        db.session.add(new_ingredient)
        db.session.commit()

        return schema.dump(new_ingredient), None
    except ValidationError:
        log.debug('there was an error validating ingredient')
        traceback.print_exc()
        return None, {
            'msg': 'there was an error validating ingredient',
            'status_code': 400
        }
    except SQLAlchemyError:
        log.error('there was a database error creating ingredient')
        traceback.print_exc()
        return None, {
            'msg': 'there was an error creating ingredient',
            'status_code': 500
        }
Esempio n. 2
0
def search(params: 'SearchQueryParam'):

    # ToDo: la respuesta del search cambia dependiendo de los parámetros (lista vs paginación) revisar si mover esto (?)
    if params.ids is not None:
        ingredients, error = multiget(params.ids, params.fields)
        return jsonify(ingredients), error  # Note this jsonify is necessary

    try:
        result = Ingredient.search(params)
        res = {
            'paging': {
                'offset': params.offset,
                'limit': params.limit
            },
            'results': result.items
        }
        paginated_response = IngredientPaginationSchema().dump(
            res)  # Todo Instaciar siempre pagination Schema
        return paginated_response, None
    except Exception as e:
        log.error(
            f'there was a database error while searching ingredient(s) [error:{str(e)}]'
        )
        traceback.print_exc(
        )  # ToDo agregar esto en lugares importantes, o en todos los try catch mejor (?)
        return None, {
            'msg': 'there was an error searching ingredient(s)',
            'status_code': 500
        }
Esempio n. 3
0
def update_ingredient(id: int, data: dict):

    try:
        ingredient_schema = IngredientSchema()
        validated_data = ingredient_schema.load(data, partial=True)
        ingredient = Ingredient.get_by_id(id)
        for attribute in validated_data:
            setattr(ingredient, attribute, validated_data[attribute])
        db.session.add(ingredient)
        db.session.commit()
        return ingredient_schema.dump(ingredient), None
    except ValidationError:
        # ToDo: retornar msg error en la validación en vez de simplemente invalid data
        log.error(
            f'there was an error while parsing data for ingredient update [id:{id}]'
        )
        traceback.print_exc()
        return None, {'msg': 'invalid data for ingredient', 'status_code': 400}
    except SQLAlchemyError:
        log.error(
            f'there was a database error while updating ingredient [id:{id}]')
        traceback.print_exc()
        return None, {
            'msg': 'there was an error while updating ingredient',
            'status_code': 500
        }
Esempio n. 4
0
    def index(self):
        shared_user = User.load_shared_user()
        preset_ingredients = self._get_preset_trial_ingredients()

        return template(
            "recipes/new.html.j2",
            ingredients=Ingredient.load_all_shared(),
            preset_ingredients=[i.id for i in preset_ingredients],
            diets=shared_user.active_diets,
            is_trialrecipe=True,
        )
Esempio n. 5
0
def get_ingredient_by_id(id: int, params: 'IngredientQuery'):
    try:
        ingredient = Ingredient.get_by_id(id, params.fields)
    except SQLAlchemyError as e:
        log.error(
            'there was a database error while getting ingredient [id:{}][error:{}]'
            .format(id, str(e)))
        return None, {
            'msg': 'there was and error while looking for ingredient',
            'status_code': 500
        }
    return IngredientSchema().dump(ingredient), None
Esempio n. 6
0
    def _get_preset_trial_ingredients(self):
        preset_ingredients = []

        ingredient = Ingredient.load_shared_by_name("Ananas")
        if ingredient:
            preset_ingredients.append(ingredient)
        else:
            preset_ingredients.append(
                Ingredient.load_random_by_nutrient("sugar"))

        ingredient = Ingredient.load_shared_by_name("Avokádo")
        if ingredient:
            preset_ingredients.append(ingredient)
        else:
            preset_ingredients.append(
                Ingredient.load_random_by_nutrient("fat"))

        ingredient = Ingredient.load_shared_by_name(
            "Maso - krůtí, prsa bez kosti")
        if ingredient:
            preset_ingredients.append(ingredient)
        else:
            preset_ingredients.append(
                Ingredient.load_random_by_nutrient("protein"))

        return preset_ingredients
Esempio n. 7
0
def multiget(ids: list, fields: list):
    try:
        ingredients = Ingredient.multiget(ids, fields)
    except SQLAlchemyError as e:
        log.error(
            'there was a database error while getting ingredients [ids:%s][error:%s]',
            ','.join(ids), str(e))
        return None, {
            'msg': 'there was an error getting ingredients',
            'status_code': 500
        }

    return IngredientSchema(many=True).dump(ingredients), None
Esempio n. 8
0
 def addIngredientWithAmount(self):
     ingredient = Ingredient.load(request.json["ingredient_id"])
     if not ingredient:
         abort(404)
     if not ingredient.can_current_user_add:
         abort(403)
     template_data = template(
         "recipes/_add_ingredient_with_amount.html.j2", ingredient=ingredient
     )
     result = {"ingredient": ingredient.json, "template_data": template_data}
     DataHandler.set_additional_request_data(
         item_type="add_ingredient_with_amount_AJAX", item_id=ingredient.id
     )
     return jsonify(result)
Esempio n. 9
0
    def load_ingredient_amounts_for_daily_plans(ids):
        # TODO: move this to ingredient model (90)
        from app import db
        from app.models.ingredients import Ingredient

        # i need to always have at least two ids for tuple to not have trailing coma
        if len(ids) < 2:
            ids.append(0)

        ids = tuple(ids)

        # TODO: change to SQlAlchemy Query to avoid SQL Injection risk and improve code (30)
        amounts_sql = f"""
                        SELECT
                            I.id AS ingredient_id,
                            -- CONCAT(R.id) AS recipe_ids,
                            SUM(RHI.amount * DPHR.portion_count) AS amount_sum
                        FROM
                            daily_plans AS DP
                            INNER JOIN daily_plans_have_recipes AS DPHR ON
                                DPHR.daily_plan_id = DP.id
                            INNER JOIN recipes AS R ON
                                R.id = DPHR.recipe_id
                            INNER JOIN recipes_have_ingredients AS RHI ON
                                RHI.recipe_id = R.id
                            INNER JOIN ingredients AS I ON
                                I.id = RHI.ingredient_id
                        WHERE
                            DP.id IN {ids}
                        GROUP BY
                            I.id
                    """

        result = db.engine.execute(amounts_sql)

        ingredients = []
        for row in result:
            ingredient = Ingredient.load(row[0])
            # ingredient.recipe_ids = row[1]
            ingredient.amount = row[1]
            ingredients.append(ingredient)

        return ingredients
Esempio n. 10
0
def db_fill_calc():
    user = create_user(username="******", password="******")
    user.save()

    diets = [{
        "name": "3.5",
        "calorie": 0,
        "sugar": 10,
        "fat": 81,
        "protein": 13,
        "active": 1,
        "user_id": 1,
    }]

    for diet in diets:
        Diet(
            name=diet["name"],
            calorie=diet["calorie"],
            sugar=diet["sugar"],
            fat=diet["fat"],
            protein=diet["protein"],
            active=diet["active"],
            user_id=diet["user_id"],
        ).save()

    ingredients = [
        {
            "name": "Brambory skladované",
            "calorie": 219,
            "sugar": 12.2,
            "fat": 0.1,
            "protein": 1.1,
            "author": "calc",
        },
        {
            "name": "Česnek",
            "calorie": 366,
            "sugar": 21.8,
            "fat": 0.2,
            "protein": 5.4,
            "author": "calc",
        },
        {
            "name": "Cuketa",
            "calorie": 57,
            "sugar": 1.8,
            "fat": 0.1,
            "protein": 0.8,
            "author": "calc",
        },
        {
            "name": "Filé z Aljašky",
            "calorie": 352,
            "sugar": 0,
            "fat": 2.76,
            "protein": 14.7,
            "author": "calc",
        },
        {
            "name": "Kurkuma",
            "calorie": 1435,
            "sugar": 65.9,
            "fat": 2.5,
            "protein": 10.8,
            "author": "calc",
        },
        {
            "name": "Máslo výběrové",
            "calorie": 3056,
            "sugar": 0.6,
            "fat": 82,
            "protein": 0.7,
            "author": "calc",
        },
        {
            "name": "Okurka salátová",
            "calorie": 54,
            "sugar": 2.1,
            "fat": 0.2,
            "protein": 1,
            "author": "calc",
        },
        # for do_register add_default_ingredients confirmation
        {
            "name": "Default salátová",
            "calorie": 100,
            "sugar": 2.1,
            "fat": 0.2,
            "protein": 1,
            "author": "default",
            "is_shared": True,
            "is_approved": True,
        },
        {
            "name": "Okurka defaultová",
            "calorie": 54,
            "sugar": 100,
            "fat": 0.2,
            "protein": 1,
            "author": "default",
            "is_shared": True,
            "is_approved": True,
        },
        {
            "name": "Okurka salátová",
            "calorie": 54,
            "sugar": 2.1,
            "fat": 100,
            "protein": 1,
            "author": "default",
            "is_shared": True,
            "is_approved": True,
        },
    ]

    for ingredient in ingredients:
        Ingredient(
            name=ingredient["name"],
            calorie=ingredient["calorie"],
            sugar=ingredient["sugar"],
            fat=ingredient["fat"],
            protein=ingredient["protein"],
            author=ingredient["author"],
            is_shared=getattr(ingredient, "is_shared", None),
            is_approved=getattr(ingredient, "is_approved", None),
        ).save()
Esempio n. 11
0
    def before_request(self, name, id=None, *args, **kwargs):
        self.ingredient = Ingredient.load(id)

        self.measurements = Measurement.load_all()
        self.categories = IngredientCategory.load_all()
        self.public_ingredients = Ingredient.load_all_public()
Esempio n. 12
0
def load_datasets_calc():
    datasets = []

    # 4
    diet = Diet.load_by_name("3.5")
    ingredients = [
        Ingredient.load_by_name("Brambory skladované").set_main().json,
        Ingredient.load_by_name("Filé z Aljašky").json,
        Ingredient.load_by_name("Máslo výběrové").json,
        Ingredient.load_by_name("Okurka salátová").json,
    ]
    test_dataset = {
        "ingredients": ingredients,
        "dietID": diet.id,
        "diet": diet.json,
        "none": "False",
    }
    datasets.append(test_dataset)

    # 4 + fixed
    diet = Diet.load_by_name("3.5")
    ingredients = [
        Ingredient.load_by_name("Brambory skladované").set_main().json,
        Ingredient.load_by_name("Filé z Aljašky").set_main(False).json,
        Ingredient.load_by_name("Máslo výběrové").set_main(False).json,
        Ingredient.load_by_name("Okurka salátová").set_main(False).json,
        Ingredient.load_by_name("Česnek").set_fixed(
            amount=0.5).set_main(False).json,
        Ingredient.load_by_name("Cuketa").set_fixed(
            amount=30).set_main(False).json,
        Ingredient.load_by_name("Kurkuma").set_fixed(
            amount=0.2).set_main(False).json,
    ]
    test_dataset = {
        "ingredients": ingredients,
        "dietID": diet.id,
        "diet": diet.json,
        "none": "False",
    }
    datasets.append(test_dataset)

    # too many / too few
    diet = Diet.load_by_name("3.5")
    ingredients = [
        Ingredient.load_by_name("Brambory skladované").json,
        Ingredient.load_by_name("Filé z Aljašky").json,
    ]
    test_dataset = {
        "ingredients": ingredients,
        "dietID": diet.id,
        "diet": diet.json,
        "none": "True",
    }
    datasets.append(test_dataset)

    # with no solution
    diet = Diet.load_by_name("3.5")
    ingredients = [
        Ingredient.load_by_name("Brambory skladované").set_main().json,
        Ingredient.load_by_name("Filé z Aljašky").json,
        Ingredient.load_by_name("Okurka salátová").json,
        Ingredient.load_by_name("Kurkuma").json,
    ]
    test_dataset = {
        "ingredients": ingredients,
        "dietID": diet.id,
        "diet": diet.json,
        "none": "True",
    }
    datasets.append(test_dataset)

    return datasets
Esempio n. 13
0
    def calcRecipeAJAX(self):
        json_ingredients = request.json["ingredients"]
        diet = Diet.load(request.json["dietID"])
        if "trial" in request.json and request.json["trial"] == "True":
            is_trialrecipe = True
        else:
            is_trialrecipe = False

        if diet is None:
            abort(400, "no diet")

        ingredients = []
        for json_i in json_ingredients:
            ingredient = Ingredient.load(json_i["id"])
            if not ingredient.can_current_user_add:
                return ("Tuto surovinu nemůžete použít", 403)
            ingredient.fill_from_json(json_i)
            ingredients.append(ingredient)

        try:
            result = calculations.calculate_recipe(ingredients, diet)
        except ValueError:
            return ("Recept nelze vytvořit", 204)

        ingredients = result["ingredients"]
        totals = result["totals"]

        json_ingredients = []
        for ing in ingredients:
            ing.calorie = round(ing.calorie * ing.amount, 2)
            ing.fat = round(ing.fat, 2)
            ing.sugar = round(ing.sugar, 2)
            ing.protein = round(ing.protein, 2)
            ing.amount = round(ing.amount * 100, 2)

            json_ingredients.append(ing.json)

        totals.calorie = round(totals.calorie, 2)
        totals.sugar = round(totals.sugar, 2)
        totals.fat = round(totals.fat, 2)
        totals.protein = round(totals.protein, 2)
        totals.amount = round(totals.amount * 100, 2)
        totals.ratio = round((totals.fat / (totals.protein + totals.sugar)), 2)

        template_data = template(
            "recipes/_right_form.html.j2",
            ingredients=ingredients,
            totals=totals,
            diet=diet,
            is_trialrecipe=is_trialrecipe,
        )

        result = {
            "template_data": str(template_data),
            "totals": json.dumps(totals.__dict__),
            "ingredients": json_ingredients,
            "diet": diet.json,
        }

        # reset ingredients (so they are not changed in db)
        for ing in ingredients:
            ing.expire()

        DataHandler.set_additional_request_data(
            item_type="calculate_recipe_AJAX", item_id=ingredient.id
        )
        return jsonify(result)
Esempio n. 14
0
def db_fill():
    print("🚧 filling db with testing data...")
    # from flask_security import create_user, create_role
    from app import security

    roles = [
        security.datastore.create_role(
            name="admin",
            permissions=
            "manage-application,manage-users,login-as,see-debug,see-other,edit-other",
        ),
        security.datastore.create_role(
            name="application_manager",
            permissions="manage-application,see-other,edit-other",
        ),
    ]

    for role in roles:
        role.save()

    users = [
        security.datastore.create_user(username="******",
                                       email="*****@*****.**",
                                       password="******"),
        security.datastore.create_user(
            username="******",
            email="*****@*****.**",
            roles=["application_manager"],
            password="******",
        ),
        security.datastore.create_user(username="******",
                                       email="*****@*****.**",
                                       roles=["admin"],
                                       password="******"),
    ]

    for user in users:
        user.save()

    from app.models.ingredients import Ingredient

    ingredients = [
        Ingredient(name="první surovina", created_by=users[0].id),
        Ingredient(name="druhá surovina", created_by=users[0].id),
        Ingredient(name="třetí surovina", created_by=users[0].id),
    ]

    for ingredient in ingredients:
        ingredient.save()

    from app.models.recipes import Recipe

    recipe = Recipe(name="první recept",
                    created_by=users[0].id,
                    portion_count=1,
                    is_shared=False)
    recipe.add_ingredient(ingredients[0], amount=20)
    recipe.add_ingredient(ingredients[2], amount=10)
    recipe.save()

    recipe_2 = Recipe(name="veřejný recept",
                      created_by=users[0].id,
                      portion_count=1,
                      is_shared=True)
    recipe_2.add_ingredient(ingredients[0], amount=20)
    recipe_2.add_ingredient(ingredients[2], amount=10)
    recipe_2.save()

    print("✅ database ready for testing.")
Esempio n. 15
0
def public_ingredients():
    from app.models.ingredients import Ingredient

    return Ingredient.load_all_in_public_recipes()
Esempio n. 16
0
 def index(self):
     self.conversions = Conversion.load_all()
     self.ingredients = Ingredient.load_all()
     return self.template()