Skip to content

Commit

Permalink
added review form to each recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
salay committed Mar 20, 2019
1 parent 5cb1eda commit 6a19fe4
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 22 deletions.
61 changes: 51 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<review_id>')
# 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/<review_id>')
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/<recipe_id>')
@app.route('/recipes/<recipe_id>', 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(),
Expand All @@ -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)
2 changes: 1 addition & 1 deletion forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')



Expand Down
2 changes: 1 addition & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions reviews.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
9 changes: 8 additions & 1 deletion templates/recipe.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ <h1>Recipe</h1>
<div>{{recipe.instructions}}</div>
</div>
</article>

{% block recipe_reviews %}
{% endblock%}



<!-- {% for review in reviews_template %}
{% endfor %} -->

{% endblock %}


7 changes: 4 additions & 3 deletions templates/recipes.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
{% block content %}
<h1>Recipes</h1>
<br>
<button id = "create-recipe-btn" class="button is-medium"
onclick="window.location.href= '{{ url_for('recipe_form') }}';">Create new recipe</button>
<br>
<br>
{% for recipe in recipes_template %}
<article class="message is-primary">
Expand All @@ -18,4 +15,8 @@ <h4>Description</h4>
</div>
</article>
{% endfor %}
<br>

<button id = "create-recipe-btn" class="button is-medium"
onclick="window.location.href= '{{ url_for('recipe_form') }}';">Create new recipe</button>
{% endblock %}
2 changes: 0 additions & 2 deletions templates/review.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% extends "layout.html" %}

{% block content %}
<h1>Review</h1>
<article class="message tile is-4 is-vertical is-parent is-primary">
Expand All @@ -12,5 +11,4 @@ <h1>Review</h1>
{{review.comment}}
</div>
</article>

{% endblock %}
4 changes: 2 additions & 2 deletions templates/review_form.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "layout.html" %}
{% extends "reviews.html" %}

{% block content %}
{% block reviewForm %}
<form method="POST" action="" novalidate>
{{ form.hidden_tag() }}

Expand Down
9 changes: 7 additions & 2 deletions templates/reviews.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "layout.html" %}
{% extends "recipe.html" %}

{% block content %}
{% block recipe_reviews %}
<h1>Reviews</h1>
{% for review in reviews_template %}
<article class="message is-secondary">
Expand All @@ -14,4 +14,9 @@ <h1>Reviews</h1>
</div>
</article>
{% endfor %}

{% block reviewForm %}
{% endblock%}


{% endblock %}
Binary file added veggiedish.db
Binary file not shown.

0 comments on commit 6a19fe4

Please sign in to comment.