def add_recipe():
    form    = AddRecipeForm()
    # Access the underlying object User that is proxied for making the ReferenceField author work
    author_id  = current_user._get_current_object()
    author     = current_user.username 
   
    # Check if a request is both a POST request and a valid request
    if form.validate_on_submit():
        # Count recipe ID's in separate collection so that numbers stay unique
        recipe_id           = RecipeID.objects.count() + 1
        title               = form.title.data
        description         = form.description.data
        category_name       = form.category_name.data
        ingredients         = form.ingredients.data
        directions          = form.directions.data    
        preparation_time    = form.preparation_time.data 
        cooking_time        = form.cooking_time.data        
        calories            = form.calories.data              
        protein             = form.protein.data               
        carbohydrates       = form.carbohydrates.data           
        cholesterol         = form.cholesterol.data               

        # Check if recipe image is selected by user
        if 'recipe_image' in request.files:
            recipe_image = request.files[ 'recipe_image'] 
            # Check if image name is secure by usering Werkzeug's secure_filename function
            secure_image_name = secure_filename(recipe_image.filename)

            # Creating prefix for making image name unique
            prefix = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
            recipe_image_name = "_".join([prefix, secure_image_name])  

        # Count total cooking time
        total_cooking_time  = preparation_time + cooking_time   

        # Create new instance of recipe
        new_recipe_id   = RecipeID(recipe_id = recipe_id)
        new_recipe      = Recipe(recipe_id = recipe_id, title = title, description = description, category_name = category_name, ingredients = ingredients,\
                            directions = directions, preparation_time = preparation_time, cooking_time = cooking_time, total_cooking_time = total_cooking_time, \
                            calories = calories, protein = protein, carbohydrates = carbohydrates, cholesterol = cholesterol, author_id = author_id, author = author,\
                            recipe_image = recipe_image, recipe_image_name = recipe_image_name)
        # Insert record to the DB
        new_recipe_id.save()
        new_recipe.save()
        flash('Your awesome recipe has been added!', 'success')
        # Go to all account page after submitting a recipe
        return redirect(url_for('account'))

    # Getting latest 5 recipes for footer
    footer_recipes              = Recipe.objects[:5].order_by('-recipe_id')

    # Render html, giving its title, passing in the form and footer recipes
    return render_template('add_recipe.html', title = 'Add recipe', form = form, footer_recipes = footer_recipes)
def vegetarian_recipes():
    # Set per page for pagination
    per_page = 6

    # Set default page parameter to 1 for pagination
    page = request.args.get('page', 1, type = int)

    # Get recipe and order descending so that newest recipes come first
    recipes = Recipe.objects(category_name = "Vegetarian").order_by('-recipe_id').paginate(page = page, per_page = per_page)

    # Count number of recipes
    total_recipes = Recipe.objects(category_name = "Vegetarian").count()

    # Getting latest 5 recipes for footer
    footer_recipes              = Recipe.objects[:5].order_by('-recipe_id')
        
    # Render html, giving its title, recipes by category, count of category recipes and footer recipes
    return render_template('vegetarian_recipes.html', title = 'All vegetarian recipes', recipes = recipes, total_recipes = total_recipes, footer_recipes = footer_recipes)
def user_recipes(author):
    # Set per page for pagination
    per_page = 6

    # Set default page parameter to 1 for pagination
    page = request.args.get('page', 1, type = int)

    # Get recipe and order descending so that newest recipes come first
    user_recipes = Recipe.objects(author = author).order_by('-recipe_id').paginate(page = page, per_page = per_page)

    # Count user's recipes
    total_user_recipes = Recipe.objects(author = author).count()

    # Getting latest 5 recipes for footer
    footer_recipes              = Recipe.objects[:5].order_by('-recipe_id')
    
    # Render html, giving its title and passing in the user's recipe object and recipes for footer
    return render_template('user_recipes.html', title = 'User recipes', user_recipes = user_recipes, total_user_recipes = total_user_recipes, author = author, footer_recipes = footer_recipes)
Exemplo n.º 4
0
def recipes():
    form = RecipeForm()
    if form.validate_on_submit():
        recipeData = Recipe(name=form.recipe_name.data, )

        db.session.add(recipeData)
        db.session.commit()
        return redirect(url_for('home'))

    return render_template('recipes.html', title='Recipe', form=form)
    def setUp(self):

        db.create_all()

        ingedient1 = Ingredients(name="Milk")
        method1 = Method(steps="hello", time="40mins")

        db.session.add(sample1) 
        db.session.add(method1)
        db.session.commit()
        recipe1= Recipe(name="recipe", ingredient_id=sample1.id, quantity="100g", method_id=method1.id)

        db.session.add(recipe1)
        db.session.commit()
def account():
    author              = current_user.username
    user_first_name     = current_user.first_name 

    # Set per page for pagination
    per_page = 6

    # Set default page parameter to 1 for pagination
    page = request.args.get('page', 1, type = int)
   
    # Make recipe list by author
    recipe_list = Recipe.objects(author = author).order_by('-recipe_id').paginate(page = page, per_page = per_page)
    
    # Getting latest 5 recipes for footer
    footer_recipes = Recipe.objects[:5].order_by('-recipe_id') 

    # Render html, giving its title, recipe list by author, user's first name and footer recipes
    return render_template('account.html', title = 'User account', recipe_list = recipe_list, user_first_name = user_first_name, footer_recipes = footer_recipes)
Exemplo n.º 7
0
def recipe():
    form = RecipeForm()
    if form.validate_on_submit():
        recipeData = Recipe(
            recipe_name=form.recipe_name.data,
            meal_type=form.meal_type.data,
            dietary_requirements=form.dietary_requirements.data,
            difficulty=form.difficulty.data,
            number_of_servings=form.number_of_servings.data,
            ingredients=form.ingredients.data,
            method=form.method.data,
            author=current_user,
            cuisine_id=int(form.cuisine.data))
        db.session.add(recipeData)
        db.session.commit()
        return redirect(url_for('home'))

    else:
        print(form.errors)
    return render_template('recipe.html', title='Recipe', form=form)
Exemplo n.º 8
0
with open('train.json') as json_file:
    data = json.load(json_file)
for element in data:
    for ing in element['ingredients']:
        ingredients_set.add(ing)

with open('data_food.json') as json_file:
    recipes = json.load(json_file)
    for recipe in recipes:
        title = recipe['title']
        directions = recipe['directions']
        img = recipe['img']
        url = recipe['url']
        ingredients = recipe['ingredients']
        ingredients_display = recipe['ingredientsDisplay']
        new_recipe = Recipe(title, directions, img, url)
        db.session.add(new_recipe)
        db.session.flush()
        for ingredient, ingredient_display in zip(ingredients,
                                                  ingredients_display):
            ing_title_matches = difflib.get_close_matches(
                ingredient, ingredients_set)
            if ing_title_matches:
                ing_title = ing_title_matches[0]
            ingredient_id = db.session.query(
                Ingredient.id).filter_by(title=ing_title).scalar()
            if ingredient_id is None:
                new_ingredient = Ingredient(ing_title)
                db.session.add(new_ingredient)
                db.session.flush()
                ingredient_id = new_ingredient.id
def get_image(image_name):
    image = Recipe.objects(recipe_image_name = image_name).first()
    return send_file(image.recipe_image, mimetype='image')
def home():
    form = searchForm()

    # Set per page for pagination
    per_page = 6

    # Set default page parameter to 1 for pagination
    page = request.args.get('page', 1, type = int)

    # Get recipes and order descending so that newest recipes come first
    recipes = Recipe.objects.order_by('-recipe_id').paginate(page = page, per_page = per_page)
    

    if form.validate_on_submit():
        search_text         = form.search_text.data
        category_name       = form.category_name.data
        max_total_time      = form.max_total_time.data

        # If a category choice is not made
        if category_name == "":
            
            # Search query on title and description (case insensitive) and max cooking time
            if max_total_time != None:
                # Pagination of filtered recipes 
                filtered_recipes    = Recipe.objects.order_by('-recipe_id')((Q(title__icontains = search_text) | Q(description__icontains = search_text)) & Q(total_cooking_time__lte = max_total_time)).paginate(page = page, per_page = per_page)
                # Count number of filtered recipes
                total_recipes       = Recipe.objects.order_by('-recipe_id')((Q(title__icontains = search_text) | Q(description__icontains = search_text)) & Q(total_cooking_time__lte = max_total_time)).count()
            # Search query on title and description (case insensitive)
            else:
                # Pagination of filtered recipes 
                filtered_recipes    = Recipe.objects.order_by('-recipe_id')(Q(title__icontains = search_text) | Q(description__icontains = search_text)).paginate(page = page, per_page = per_page)
                # Count number of filtered recipes
                total_recipes       = Recipe.objects.order_by('-recipe_id')(Q(title__icontains = search_text) | Q(description__icontains = search_text)).count()
        # When a category choice is made
        else:
           
            # Search query on title and description (case insensitive), category and max cooking time
            if max_total_time != None:
                # Pagination of filtered recipes 
                filtered_recipes    = Recipe.objects.order_by('-recipe_id')((Q(title__icontains = search_text) | Q(description__icontains = search_text)) & Q(category_name = category_name) & Q(total_cooking_time__lte = max_total_time)).paginate(page = page, per_page = per_page)
                # Count number of filtered recipes
                total_recipes       = Recipe.objects.order_by('-recipe_id')((Q(title__icontains = search_text) | Q(description__icontains = search_text)) & Q(category_name = category_name) & Q(total_cooking_time__lte = max_total_time)).count()
            # Search query on title and description (case insensitive) and category 
            else:
                # Pagination of filtered recipes 
                filtered_recipes    = Recipe.objects.order_by('-recipe_id')((Q(title__icontains = search_text) | Q(description__icontains = search_text)) & Q(category_name = category_name)).paginate(page = page, per_page = per_page)
                # Count number of filtered recipes
                total_recipes       = Recipe.objects.order_by('-recipe_id')((Q(title__icontains = search_text) | Q(description__icontains = search_text)) & Q(category_name = category_name)).count()

        return render_template('recipes.html', title = 'Filter results recipes', form = form, recipes = filtered_recipes, total_recipes = total_recipes, category_name = category_name)

    total_meat_recipes          = Recipe.objects(category_name = "Meat").count()
    total_seafood_recipes       = Recipe.objects(category_name = "Seafood").count()
    total_vegetarian_recipes    = Recipe.objects(category_name = "Vegetarian").count()
    total_recipes               = Recipe.objects.count()

    # Getting latest 5 recipes for footer
    footer_recipes              = Recipe.objects[:5].order_by('-recipe_id')

    # Render html, recipes count (by category), search form and footer recipes
    return render_template('index.html', total_meat_recipes = total_meat_recipes, total_seafood_recipes = total_seafood_recipes, total_vegetarian_recipes = total_vegetarian_recipes, form = form, total_recipes = total_recipes, footer_recipes = footer_recipes)