MVP done, along with delete button
This commit is contained in:
28
main.py
28
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)
|
||||
|
||||
74
recipe.py
74
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')
|
||||
|
||||
@@ -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"}}
|
||||
{
|
||||
"0": {
|
||||
"quantity": "2",
|
||||
"name": "Sourdough",
|
||||
"ingredients": {
|
||||
"ap_flour": "1000",
|
||||
"water": "600",
|
||||
"salt": "20"
|
||||
}
|
||||
}
|
||||
}
|
||||
14
templates/empty.html
Normal file
14
templates/empty.html
Normal file
@@ -0,0 +1,14 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}Recipes{% endblock %}
|
||||
|
||||
{% block html_head %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h1>Bread Recipes</h1>
|
||||
|
||||
<p>You haven't added any recipes yet!</p>
|
||||
|
||||
{% endblock %}
|
||||
@@ -11,18 +11,18 @@
|
||||
|
||||
<form action="{{ url_for("add_recipe")}}" method="post">
|
||||
|
||||
<label for="name">Recipe Name:</label>
|
||||
<label for="name">Recipe Name:</label><br>
|
||||
<input required type="text" id="name" name="name" placeholder="Sourdough">
|
||||
<br>
|
||||
<p>Enter all values grams.</p>
|
||||
|
||||
{% for ing in ['flour', 'water', 'salt'] %}
|
||||
<label for="{{ing}}_weight">{{ing.title()}}:</label>
|
||||
<input required type="number" id="{{ing}}_weight" name="{{ing}}_weight" placeholder="100">
|
||||
<label for="{{ing}}_weight">{{ing.title()}}:</label><br>
|
||||
<input required type="number" id="{{ing}}_weight" name="{{ing}}_weight" placeholder="100"><br>
|
||||
<br>
|
||||
{% endfor %}
|
||||
|
||||
<label for="yield">Loaves:</label>
|
||||
<label for="yield">Loaves:</label><br>
|
||||
<input required type="number" id="yield" name="yield" placeholder="2">
|
||||
|
||||
<button type="submit">Add!</button>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}Recipes{% endblock %}
|
||||
{% block title %}{{content["name"]}}{% endblock %}
|
||||
|
||||
{% block html_head %}
|
||||
|
||||
@@ -7,13 +7,25 @@
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h1>Bread Recipes</h1>
|
||||
<ul>
|
||||
{% for recipe in content %}
|
||||
<li>
|
||||
<a href=recipes/{{recipe}}>{{content[recipe]['name']}}</a>
|
||||
</li>
|
||||
<h1>{{content["name"]}} Recipe</h1>
|
||||
|
||||
|
||||
<p>Number of loaves: {{content['quantity']}}</p>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Ingredient</th>
|
||||
<th>Grams</th>
|
||||
</tr>
|
||||
{% for item in content['ingredients'] %}
|
||||
<tr>
|
||||
<td>{{ item.title() }}</td>
|
||||
<td>{{ content['ingredients'][item] }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</table>
|
||||
|
||||
<input type="button" onclick="location.href='/recipes/{{_id}}/delete'" value=Delete />
|
||||
|
||||
|
||||
{% endblock %}
|
||||
19
templates/recipes.html
Normal file
19
templates/recipes.html
Normal file
@@ -0,0 +1,19 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}Recipes{% endblock %}
|
||||
|
||||
{% block html_head %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h1>Bread Recipes</h1>
|
||||
<ul>
|
||||
{% for recipe in content %}
|
||||
<li>
|
||||
<a href=recipes/{{recipe}}>{{content[recipe]['name']}}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user