Skip to content

Commit

Permalink
Added route, template for user page
Browse files Browse the repository at this point in the history
This page display the user's favorited products and reviews.

Created a new helper function that retrieves all favorited reviews
from a user's favorited product
  • Loading branch information
michberr committed Mar 1, 2017
1 parent efadce5 commit d7f4825
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
19 changes: 13 additions & 6 deletions product_genius.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def get_favorite_review_ids(user_id):
return set(rev.review_id for rev in favorite_reviews)


def get_favorite_reviews_by_product(user_id, asin):
"""Return a list of review objects that a user favorited for a given product"""

favorites = Review.query.\
join(favorite_reviews).\
join(User).\
filter(User.user_id==user_id,
Review.asin==asin).all()

return favorites


################ Functions for favoriting ###############


Expand Down Expand Up @@ -121,11 +133,7 @@ def remove_favorite_reviews(user_id, asin):
"""

# Query for user's favorite reviews for that product
product_fav_reviews = Review.query.\
join(favorite_reviews).\
join(User).\
filter(User.user_id==user_id,
Review.asin==asin).all()
product_fav_reviews = get_favorite_reviews_by_product(user_id, asin)

user = User.query.get(user_id)
for review in product_fav_reviews:
Expand Down Expand Up @@ -304,4 +312,3 @@ def find_reviews(asin, query):
reviews = cursor.fetchall()

return reviews

20 changes: 16 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from product_genius import find_products, find_reviews, get_scores, get_chart_data
from product_genius import register_user, update_favorite_review, update_favorite_product
from product_genius import format_reviews_to_dicts, add_favorite_product_from_review, get_favorite_review_ids
from product_genius import get_reviews_by_asin, is_favorite_product, remove_favorite_reviews
from product_genius import get_reviews_by_asin, is_favorite_product, remove_favorite_reviews, get_favorite_reviews_by_product
import sqlalchemy

app = Flask(__name__)
Expand Down Expand Up @@ -111,15 +111,27 @@ def display_product_profile(asin):

##################### Favorites ################################

@app.route('/user/int<user_id>')
def display_user_profile():
@app.route('/user/<user_id>')
def display_user_profile(user_id):
"""Display user's favorite products and reviews.
User should be able to compare products/reviews side by side.
Might add functionality to add to the amazon cart via their API
"""

pass
user_id = int(user_id)
user = User.query.get(user_id)

favorite_products = user.favorite_products

print favorite_products

for pr in favorite_products:
pr.favorited_reviews = get_favorite_reviews_by_product(user_id, pr.asin)

return render_template("user_page.html",
user=user,
favorite_products=favorite_products)


@app.route('/favorite-product', methods=['POST'])
Expand Down
25 changes: 12 additions & 13 deletions templates/user_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ <h3>{{ user.email }}</h3>
<h2>Favorite Products: </h2>

{% for pr in favorite_products %}

<h4>{{ pr.title }}</h4>

<div id="product-img">
<a href="/product/{{ pr.asin }}">
<img src="{{ pr.img }}">
</a>
</div>

{% for rev in pr.favorite_reviews %}

{% endfor %}

<h3>{{ pr.title }}</h3>
<div>
<a href="/product/{{ pr.asin }}"><img src="{{ pr.image }}"></a>
</div><br><br>

<div>
<h4>Favorite Reviews:</h4>
{% for review in pr.favorited_reviews %}
<h5>{{ review.summary }}</h5>
{{ review.review }}
{% endfor %}
</div><br><br>
{% endfor %}

{% endblock %}

0 comments on commit d7f4825

Please sign in to comment.