diff --git a/main.py b/main.py index c22931f..b6ed339 100644 --- a/main.py +++ b/main.py @@ -3,8 +3,7 @@ Portfolio Project for CS361 Spring 2022 Written by Lucas Jensen Last updated 3/29/22 for Assignment 1 """ -import json -from flask import Flask, redirect, flash, render_template, request, jsonify +from flask import Flask, redirect, render_template, request from markupsafe import escape from recipe import Recipe, RecipeBook, open_json @@ -19,16 +18,25 @@ def home(): @app.route('/recipes', methods=['GET']) def ReturnJSON(): - return render_template("recipe.html", content=open_json()) + recipes = open_json() + if recipes == {}: + return render_template("empty.html") + return render_template("recipes.html", content=recipes) @app.route('/recipes/<_id>', methods=['GET']) def recipe_page(_id): if request.method == 'GET': - # recipe = book.find_by_id(int(_id)) - # return jsonify(recipe.get_recipe()) - recipe = open_json()[_id] - return recipe + recipe = book.find_by_id(_id) + + return render_template("recipe.html", content=recipe, _id=_id) + + +@app.route('/recipes/<_id>/delete') +def delete_recipe(_id): + book.delete(_id) + + return redirect("/recipes") @app.route('/add', methods=['GET', 'POST']) @@ -43,16 +51,10 @@ def add_recipe(): new_recipe = Recipe(name, loaves, flour, water, salt) book.add_recipes(new_recipe) - # flash("Recipe Added!") return redirect("/recipes") return render_template("new.html") if __name__ == "__main__": - # testing - # recipes = default_recipes() book = RecipeBook() - # for recipe in recipes: - # book.add_recipes(recipe) - app.run(debug=True) diff --git a/recipe.py b/recipe.py index f406aca..b0f6a83 100644 --- a/recipe.py +++ b/recipe.py @@ -11,8 +11,17 @@ class RecipeBook: Represents a recipe book containing several bread recipes """ def __init__(self): - self._count = 0 - self._recipes = {} + self._recipes = self.__build_recipes() + + @staticmethod + def __build_recipes(): + """ + Builds the recipe book from an existing json file + """ + with open("recipes/recipes.json", "rt") as f: + recipes = json.load(f) + + return recipes def add_recipes(self, recipe: object) -> None: """ @@ -21,13 +30,13 @@ class RecipeBook: :return: nothing """ keys = open_json() - for key in keys: - if int(key) > self._count: - self._count = int(key) - self._count += 1 - self._recipes[self._count] = recipe - self.save(self._count, recipe) - # self._count += 1 + count = 0 + + while str(count) in keys: + count += 1 + + self._recipes[count] = recipe + self.save(count, recipe) def get_recipes(self) -> list: """ @@ -50,15 +59,21 @@ class RecipeBook: :param _id: int :return: nothing """ - a_dict = {str(_id): recipe.get_recipe()} + self._recipes[_id] = recipe.get_recipe() - with open('recipes/recipes.json') as f: - data = json.load(f) + with open("recipes/recipes.json", "wt") as f: + json.dump(self._recipes, f, indent=4) - data.update(a_dict) + def delete(self, _id: int) -> None: + """ + Deletes a recipes as found by its ID + :param _id: int: id of the recipe + :return: nothing + """ + del self._recipes[str(_id)] - with open('recipes/recipes.json', 'w') as f: - json.dump(data, f) + with open("recipes/recipes.json", "wt") as f: + json.dump(self._recipes, f, indent=4) class Recipe: @@ -87,11 +102,12 @@ class Recipe: """ recipe_dict = { 'quantity': self._num_loaves, - 'name': self._name + 'name': self._name, + 'ingredients': {} } for ingredient in self._ingredients: - recipe_dict[ingredient] = self._ingredients[ingredient] + recipe_dict['ingredients'][ingredient] = self._ingredients[ingredient] return recipe_dict @@ -139,16 +155,6 @@ class Recipe: return scaled_recipe -def build_recipe(infile) -> None: - """ - Builds a recipe object from a JSON file - :param infile: filename - :return: nothing - """ - # TODO - pass - - def default_recipes(): recipe_1 = Recipe('Country Brown', 1, 600, 300, 13) recipe_2 = Recipe('Conventional White', 2, 1000, 700, 25) @@ -159,8 +165,8 @@ def default_recipes(): def open_json(): """TODO""" - with open('recipes/recipes.json') as json_file: - data = json.load(json_file) + with open('recipes/recipes.json') as f: + data = json.load(f) return data @@ -171,9 +177,9 @@ if __name__ == "__main__": for recipe in recipes: book.add_recipes(recipe) - for recipe in book.get_recipes(): - print(book.get_recipes()[recipe]) - - rec = Recipe('sandwich loaf', 2, 1000, 700, 25) - rec.add_ingredient('ww flour', 500) + # for recipe in book.get_recipes(): + # print(book.get_recipes()[recipe]) + # + # rec = Recipe('sandwich loaf', 2, 1000, 700, 25) + # rec.add_ingredient('ww flour', 500) # rec.save('recipes.json') diff --git a/recipes/recipes.json b/recipes/recipes.json index a22dd1a..d739ed6 100644 --- a/recipes/recipes.json +++ b/recipes/recipes.json @@ -1 +1,11 @@ -{"0": {"quantity": "1", "name": "bread", "ap_flour": "1", "water": "1", "salt": "1"}, "1": {"quantity": 2, "name": "Conventional White", "ap_flour": 1000, "water": 700, "salt": 25}, "2": {"quantity": 4, "name": "Conventional White * 2.0", "ap_flour": 2000.0, "water": 1400.0, "salt": 50.0}, "3": {"quantity": "1", "name": "sourdough", "ap_flour": "100", "water": "1001", "salt": "1001"}, "4": {"quantity": "11", "name": "milk", "ap_flour": "10101", "water": "10", "salt": "10"}, "5": {"quantity": "1", "name": "white", "ap_flour": "100", "water": "50", "salt": "2"}} \ No newline at end of file +{ + "0": { + "quantity": "2", + "name": "Sourdough", + "ingredients": { + "ap_flour": "1000", + "water": "600", + "salt": "20" + } + } +} \ No newline at end of file diff --git a/templates/empty.html b/templates/empty.html new file mode 100644 index 0000000..605a6d8 --- /dev/null +++ b/templates/empty.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} +{% block title %}Recipes{% endblock %} + +{% block html_head %} + +{% endblock %} + +{% block content %} + +
You haven't added any recipes yet!
+ +{% endblock %} \ No newline at end of file diff --git a/templates/new.html b/templates/new.html index 2c033b5..87eb783 100644 --- a/templates/new.html +++ b/templates/new.html @@ -11,18 +11,18 @@