Commit da2d39ff authored by Willard's avatar Willard

Implement favorites and reviews using MethodView

parent 6e74f17d
from canteeneo import app, db, auth
from flask import jsonify, request, g
from flask.views import MethodView
from models import Dish, Stall, Location, User, DishReview, StallReview
from datetime import datetime
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired
......@@ -93,47 +94,98 @@ def get_auth_token():
token = generate_token(g.user)
return jsonify({'token': token.decode('ascii'), 'duration': 600})
@app.route('/api/dishes/<int:dish_id>/favorites', methods=['GET'])
@auth.login_required
def favorite_dish(dish_id):
class DishFavoriteResource(MethodView):
decorators = [auth.login_required]
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)
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)
else:
elif dish in g.user.dish_favorites and not favorited:
g.user.dish_favorites.remove(dish)
db.session.commit()
return str(dish in g.user.dish_favorites)
@app.route('/api/dishes/<int:dish_id>/reviews', methods=['POST'])
@auth.login_required
def review_dish(dish_id):
class DishReviewResource(MethodView):
decorators = [auth.login_required]
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
title = request.form['title']
body = request.form['body']
rating = int(request.form['rating'])
db.session.add(DishReview(title, body, rating, user_id, dish_id))
review = DishReview(title, body, rating, user_id, dish_id)
db.session.add(review)
db.session.commit()
return 'Review posted'
@app.route('/api/stalls/<int:stall_id>/favorites', methods=['GET'])
@auth.login_required
def favorite_stall(stall_id):
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)
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)
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)
else:
elif stall in g.user.stall_favorites and not favorited:
g.user.stall_favorites.remove(stall)
db.session.commit()
return str(stall in g.user.stall_favorites)
@app.route('/api/stalls/<int:stall_id>/reviews', methods=['POST'])
@auth.login_required
def review_stall(stall_id):
class StallReviewResource(MethodView):
decorators = [auth.login_required]
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
title = request.form['title']
body = request.form['body']
rating = int(request.form['rating'])
db.session.add(StallReview(title, body, rating, user_id, stall_id))
review = StallReview(title, body, rating, user_id, stall_id)
db.session.add(review)
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