Advertisement
OlexBy

controller_dl

May 22nd, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. from flask import Blueprint, session, jsonify, g
  2. from sqlalchemy import or_
  3.  
  4. from .models import Comment
  5. from ..auth.models import User
  6. from ..db import db
  7.  
  8.  
  9. comments_bp = Blueprint('comments', __name__)
  10.  
  11.  
  12. @comments_bp.route('/comments/<int:comment_id>/', methods=['DELETE'])
  13. def delete_comment(comment_id):
  14. comment_query = db.session.query(Comment)
  15. comment_filtered = comment_query.filter(Comment.id == comment_id)
  16. has_permission = comment_query.join(User).filter(Comment.author_id == User.id).\
  17. filter(or_(Comment.author_id == g.user.id, g.user.is_superuser is True))
  18. result = comment_filtered.delete()
  19. if result:
  20. db.session.commit()
  21. return jsonify({'status': 0})
  22. else:
  23. db.session.rollback()
  24. return jsonify({'msg': 'comment not found', 'status': 1})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement