Advertisement
OlexBy

//////

May 23rd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 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. user_sq = comment_query.join(User).filter(Comment.author_id == User.id).\
  16. filter(Comment.author_id == g.user.id).subquery()
  17. filter_user = comment_query.filter(or_(g.user.is_superuser is True, user_sq))
  18. comment_filtered = filter_user.filter(Comment.id == comment_id)
  19. result = comment_filtered.delete(synchronize_session='fetch')
  20. if result:
  21. db.session.commit()
  22. return jsonify({'status': 0})
  23. else:
  24. db.session.rollback()
  25. return jsonify({'msg': 'comment not found', 'status': 1})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement