Commit da2d39ff authored by Willard's avatar Willard

Implement favorites and reviews using MethodView

parent 6e74f17d
from canteeneo import app, db, auth from canteeneo import app, db, auth
from flask import jsonify, request, g from flask import jsonify, request, g
from flask.views import MethodView
from models import Dish, Stall, Location, User, DishReview, StallReview from models import Dish, Stall, Location, User, DishReview, StallReview
from datetime import datetime from datetime import datetime
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired
...@@ -93,47 +94,98 @@ def get_auth_token(): ...@@ -93,47 +94,98 @@ def get_auth_token():
token = generate_token(g.user) token = generate_token(g.user)
return jsonify({'token': token.decode('ascii'), 'duration': 600}) return jsonify({'token': token.decode('ascii'), 'duration': 600})
@app.route('/api/dishes/<int:dish_id>/favorites', methods=['GET']) class DishFavoriteResource(MethodView):
@auth.login_required decorators = [auth.login_required]
def favorite_dish(dish_id):
def get(self, dish_id):
return str(Dish.query.get(dish_id) in g.user.dish_favorites)
def post(self, dish_id):
dish = Dish.query.get(dish_id) dish = Dish.query.get(dish_id)
if dish not in g.user.dish_favorites: favorited = request.form['favorited'] == u'1'
if dish not in g.user.dish_favorites and favorited:
g.user.dish_favorites.append(dish) g.user.dish_favorites.append(dish)
else: elif dish in g.user.dish_favorites and not favorited:
g.user.dish_favorites.remove(dish) g.user.dish_favorites.remove(dish)
db.session.commit() db.session.commit()
return str(dish in g.user.dish_favorites) return str(dish in g.user.dish_favorites)
@app.route('/api/dishes/<int:dish_id>/reviews', methods=['POST']) class DishReviewResource(MethodView):
@auth.login_required decorators = [auth.login_required]
def review_dish(dish_id):
def get(self, dish_id):
review = DishReview.query.filter_by(user_id=g.user.id, dish_id=dish_id).first()
print(g.user.id, dish_id)
return self.review_json(review)
def post(self, dish_id):
user_id = g.user.id user_id = g.user.id
title = request.form['title'] title = request.form['title']
body = request.form['body'] body = request.form['body']
rating = int(request.form['rating']) rating = int(request.form['rating'])
review = DishReview(title, body, rating, user_id, dish_id)
db.session.add(DishReview(title, body, rating, user_id, dish_id)) db.session.add(review)
db.session.commit() db.session.commit()
return 'Review posted' return self.review_json(review)
@app.route('/api/stalls/<int:stall_id>/favorites', methods=['GET']) def review_json(self, review):
@auth.login_required if review is None:
def favorite_stall(stall_id): return ''
review = {
'title': review.title,
'body': review.body,
'rating': review.rating,
'user_id': review.user_id,
'dish_id': review.dish_id
}
return jsonify(review)
class StallFavoriteResource(MethodView):
decorators = [auth.login_required]
def get(self, stall_id):
return str(Stall.query.get(stall_id) in g.user.stall_favorites)
def post(self, stall_id):
stall = Stall.query.get(stall_id) stall = Stall.query.get(stall_id)
if stall not in g.user.stall_favorites: favorited = request.form['favorited'] == u'1'
if stall not in g.user.stall_favorites and favorited:
g.user.stall_favorites.append(stall) g.user.stall_favorites.append(stall)
else: elif stall in g.user.stall_favorites and not favorited:
g.user.stall_favorites.remove(stall) g.user.stall_favorites.remove(stall)
db.session.commit() db.session.commit()
return str(stall in g.user.stall_favorites) return str(stall in g.user.stall_favorites)
@app.route('/api/stalls/<int:stall_id>/reviews', methods=['POST']) class StallReviewResource(MethodView):
@auth.login_required decorators = [auth.login_required]
def review_stall(stall_id):
def get(self, stall_id):
review = StallReview.query.filter_by(user_id=g.user.id, stall_id=stall_id).first()
return self.review_json(review)
def post(self, stall_id):
user_id = g.user.id user_id = g.user.id
title = request.form['title'] title = request.form['title']
body = request.form['body'] body = request.form['body']
rating = int(request.form['rating']) rating = int(request.form['rating'])
review = StallReview(title, body, rating, user_id, stall_id)
db.session.add(StallReview(title, body, rating, user_id, stall_id)) db.session.add(review)
db.session.commit() db.session.commit()
return self.review_json(review)
def review_json(self, review):
if review is None:
return ''
review = {
'title': review.title,
'body': review.body,
'rating': review.rating,
'user_id': review.user_id,
'dish_id': review.dish_id
}
return jsonify(review)
app.add_url_rule('/api/dishes/<int:dish_id>/favorites', view_func=DishFavoriteResource.as_view('dish_favorite'))
app.add_url_rule('/api/dishes/<int:dish_id>/reviews', view_func=DishReviewResource.as_view('dish_review'))
app.add_url_rule('/api/stalls/<int:stall_id>/favorites', view_func=StallFavoriteResource.as_view('stall_favorite'))
app.add_url_rule('/api/stalls/<int:stall_id>/reviews', view_func=StallReviewResource.as_view('stall_review'))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment