-
-
Save Suor/726f36b074da122231c164d09ce372ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/api/service/<product_id>/vote/<any(up,down):direction>') | |
@xhr_required | |
@login_required | |
def api_service_vote(product_id, direction): | |
''' thi api need for vote up/down the services''' | |
product = Product.query.get_or_404(product_id) | |
query = Product_Vote.query.filter(Product_Vote.voter_id == g.user.id, | |
Product_Vote.product_id == product.id) | |
if db.session.query(query.exists()).scalar(): | |
query.delete() | |
db.session.commit() | |
# Return list of votes | |
votes = Product_Vote.query.filter(Product_Vote.product_id == product.id).all() | |
return json.jsonify({'votes': votes}) | |
if direction == 'up': | |
product.votes = Product.votes + 1 | |
else: | |
product.votes = Product.votes - 1 | |
vote = Product_Vote( | |
product_id=product.id, | |
voter_id=g.user.id, | |
up=direction == 'up', | |
down=direction == 'down' | |
) | |
db.session.add(vote) | |
db.session.add(product) | |
db.session.commit() | |
return json.jsonify() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment