From 6a19fe42dfb45b5640153d1cc21df86bab9903b7 Mon Sep 17 00:00:00 2001 From: salay Date: Wed, 20 Mar 2019 14:52:01 -0700 Subject: [PATCH] added review form to each recipe --- app.py | 61 +++++++++++++++++++++++++++++++------ forms.py | 2 +- models.py | 2 +- reviews.json | 8 +++++ templates/recipe.html | 9 +++++- templates/recipes.html | 7 +++-- templates/review.html | 2 -- templates/review_form.html | 4 +-- templates/reviews.html | 9 ++++-- veggiedish.db | Bin 0 -> 20480 bytes 10 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 veggiedish.db diff --git a/app.py b/app.py index 91c5a59..5a6c6b4 100644 --- a/app.py +++ b/app.py @@ -72,35 +72,59 @@ def users(user_id = None): return render_template("new_user.html", title="New User", form=form) +# @app.route('/reviews') +# @app.route('/reviews/') +# @app.route('/reviews/') +# def reviews(review_id = None): +# with open('reviews.json') as json_data: +# reviews_data = json.load(json_data) +# if review_id == None: +# return render_template('reviews.html', reviews_template = reviews_data) +# else: +# review_ID = int(review_id) +# return render_template('review.html', review = reviews_data[review_ID]) + @app.route('/reviews') @app.route('/reviews/') @app.route('/reviews/') def reviews(review_id = None): - with open('reviews.json') as json_data: - reviews_data = json.load(json_data) - if review_id == None: - return render_template('reviews.html', reviews_template = reviews_data) - else: - review_ID = int(review_id) - return render_template('review.html', review = reviews_data[review_ID]) + if review_id == None: + reviews = models.Review.select().limit(10) + return render_template("reviews.html", reviews_template = reviews) + else: + review_id = int(review_id) + review = models.Reviews.get(models.Reviews.id == review_id) + return render_template("reviews.html", reviews=reviews) @app.route('/recipes') @app.route('/recipes/') -@app.route('/recipes/') +@app.route('/recipes/', methods=['GET', 'POST']) def recipes(recipe_id = None): if recipe_id == None: recipes = models.Recipe.select().limit(10) - print(recipes) return render_template("recipes.html", recipes_template = recipes) else: recipe_id = int(recipe_id) recipe = models.Recipe.get(models.Recipe.id == recipe_id) - return render_template("recipe.html", recipe=recipe) + + form = ReviewForm() + if form.validate_on_submit(): + ratingInt = int(form.rating.data) + models.Review.create( + rating=ratingInt, + comment=form.comment.data.strip(), + recipe_id = recipe + ) + return redirect('/reviews') + else: + return render_template("review_form.html", recipe=recipe, form=form) @app.route('/create-recipe', methods=['GET', 'POST']) +#function name needs to match the link def recipe_form(): form = RecipeForm() + #same name as imported form if form.validate_on_submit(): models.Recipe.create( name=form.name.data.strip(), @@ -115,6 +139,23 @@ def recipe_form(): return render_template('recipe_form.html', form=form) +# @app.route('/create-review', methods=['GET', 'POST']) +# def createReview(): +# reviewform = ReviewForm() +# if form.validate_on_submit(): +# ratingInt = int(form.rating.data) +# models.Review.create( +# rating=ratingInt, +# comment=form.comment.data.strip() +# ) +# return redirect('/reviews') +# else: +# return render_template('review_form.html', form=reviewform) + + + + + if __name__ == '__main__': models.initialize() app.run(debug=DEBUG, port=PORT) diff --git a/forms.py b/forms.py index 3fc1501..9d248cb 100644 --- a/forms.py +++ b/forms.py @@ -32,7 +32,7 @@ class RecipeForm(Form): class ReviewForm(Form): rating = IntegerField("Recipe Rating on a scale of 1-5") comment = TextAreaField("Review of Recipe") - + submit = SubmitField('Create Review') diff --git a/models.py b/models.py index 012748e..69f6a52 100644 --- a/models.py +++ b/models.py @@ -30,7 +30,7 @@ class Review(Model): rating = IntegerField(default=0) date_reviewed = DateTimeField(default=datetime.datetime.now) comment = TextField() - user_id = ForeignKeyField(model=User, backref="users") + # user_id = ForeignKeyField(model=User, backref="users") recipe_id = ForeignKeyField(model=Recipe, backref="recipes") class Meta: diff --git a/reviews.json b/reviews.json index 8319335..9b6e47a 100644 --- a/reviews.json +++ b/reviews.json @@ -14,5 +14,13 @@ "user_id": 1, "recipe_id": 2, "id": 1 + }, + { + "rating": 3, + "date_reviewed": "2019-03-18", + "comment": "yummy", + "user_id": 0, + "recipe_id": 3, + "id": 2 } ] \ No newline at end of file diff --git a/templates/recipe.html b/templates/recipe.html index 6b70799..751e597 100644 --- a/templates/recipe.html +++ b/templates/recipe.html @@ -11,7 +11,14 @@

Recipe

{{recipe.instructions}}
- + {% block recipe_reviews %} + {% endblock%} + + + + + {% endblock %} diff --git a/templates/recipes.html b/templates/recipes.html index 96ca6a2..86e7e12 100644 --- a/templates/recipes.html +++ b/templates/recipes.html @@ -3,9 +3,6 @@ {% block content %}

Recipes


- -

{% for recipe in recipes_template %}
@@ -18,4 +15,8 @@

Description

{% endfor %} +
+ + {% endblock %} \ No newline at end of file diff --git a/templates/review.html b/templates/review.html index 6a4c04f..990854f 100644 --- a/templates/review.html +++ b/templates/review.html @@ -1,5 +1,4 @@ {% extends "layout.html" %} - {% block content %}

Review

@@ -12,5 +11,4 @@

Review

{{review.comment}}
- {% endblock %} \ No newline at end of file diff --git a/templates/review_form.html b/templates/review_form.html index 7327c25..078d5f3 100644 --- a/templates/review_form.html +++ b/templates/review_form.html @@ -1,6 +1,6 @@ -{% extends "layout.html" %} +{% extends "reviews.html" %} -{% block content %} +{% block reviewForm %}
{{ form.hidden_tag() }} diff --git a/templates/reviews.html b/templates/reviews.html index 356cec4..b86e0d2 100644 --- a/templates/reviews.html +++ b/templates/reviews.html @@ -1,6 +1,6 @@ -{% extends "layout.html" %} +{% extends "recipe.html" %} -{% block content %} +{% block recipe_reviews %}

Reviews

{% for review in reviews_template %}
@@ -14,4 +14,9 @@

Reviews

{% endfor %} + +{% block reviewForm %} +{% endblock%} + + {% endblock %} \ No newline at end of file diff --git a/veggiedish.db b/veggiedish.db new file mode 100644 index 0000000000000000000000000000000000000000..8e6011ecb0a91f3df4786715af0cafb5c0b0e635 GIT binary patch literal 20480 zcmeI(!B5jr90%}r9gG>VhJ-AH%d;kiCa`pylWr#`6kHqy%ODUB%X57!Pq&t|9mv5L z!XM&a;XmNjf5891vnMYee5DLwo5n;BhJ1g?>+9ZozjnV*dui5fKX34ekpn;I(TJ3! zyOJzRj|q_^DJ$B9XoE1 z(l9*s198gXgf@>(Mn_#5vDPb}drYLPif`YnZ&;V-*Iy`;xurSz9rs-JCSXUL9k&A3 z=7+4s-Q;unqUyT6YV8qic#%dn?ZIfQaha{@r*Bi4+}xaewsX#yWF}kXI%P8WhnNA4 zxYrpq7ntYOCI=f^uUOLddp+hwqf$S=_{h~