Commit 7d6260a0 authored by Willard's avatar Willard

Fix embarrasing Stall and StallFavorite errors

parent cdb9a728
......@@ -144,6 +144,13 @@ def get_stall_by_name():
name = request.args.get('name')
return jsonify(stall_obj(Stall.query.filter_by(name=name).first()))
@app.route('/api/stalls/<int:stall_id>/favorites/count', methods=['GET'])
def get_stall_favorites_count(stall_id):
stall = Stall.query.get(stall_id)
if(stall is None):
return 0
return len(stall.favorites)
@app.route('/api/stalls/<int:stall_id>/dishes', methods=['GET'])
def get_dishes_by_stall(stall_id):
dishes = []
......@@ -164,11 +171,11 @@ def stall_favorite(stall_id):
if request.method == 'POST':
favorited = request.form['favorited'] == u'1'
if stall not in g.user.stall_favorites and favorited:
g.user.stall_favorites.append(dish)
elif dish in g.user.dish_favorites and not favorited:
g.stall.stall_favorites.remove(dish)
g.user.stall_favorites.append(stall)
elif stall in g.user.stall_favorites and not favorited:
g.stall.stall_favorites.remove(stall)
db.session.commit()
return str(dish in g.user.stall_favorites)
return str(stall in g.user.stall_favorites)
@app.route('/api/stalls/<int:stall_id>/reviews', methods=['POST'])
@auth.login_required
......
......@@ -18,7 +18,7 @@ class Stall(db.Model):
owner_id = db.Column(db.Integer, db.ForeignKey('owner.id'))
location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
dishes = db.relationship('Dish', backref='stall', cascade='all,delete', lazy='dynamic')
favorites = db.relationship('Stall', secondary=stall_favorites, backref=db.backref('stall', lazy='dynamic'))
favorites = db.relationship('User', secondary=stall_favorites, backref=db.backref('stall', lazy='dynamic'))
reviews = db.relationship('StallReview', backref='stall', lazy='dynamic')
def __init__(self, name, description, owner_id, location_id):
......
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