You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.6 KiB

преди 4 години
  1. # -*- coding: utf-8 -*-
  2. """
  3. :author: Grey Li ()
  4. :url: http://greyli.com
  5. :copyright: © 2018 Grey Li <withlihui@gmail.com>
  6. :license: MIT, see LICENSE for more details.
  7. """
  8. from flask import render_template, jsonify, Blueprint
  9. from flask_login import current_user
  10. from albumy.models import User, Photo, Notification
  11. from albumy.notifications import push_collect_notification, push_follow_notification
  12. ajax_bp = Blueprint('ajax', __name__)
  13. @ajax_bp.route('/notifications-count')
  14. def notifications_count():
  15. if not current_user.is_authenticated:
  16. return jsonify(message='Login required.'), 403
  17. count = Notification.query.with_parent(current_user).filter_by(is_read=False).count()
  18. return jsonify(count=count)
  19. @ajax_bp.route('/profile/<int:user_id>')
  20. def get_profile(user_id):
  21. user = User.query.get_or_404(user_id)
  22. return render_template('main/profile_popup.html', user=user)
  23. @ajax_bp.route('/followers-count/<int:user_id>')
  24. def followers_count(user_id):
  25. user = User.query.get_or_404(user_id)
  26. count = user.followers.count() - 1 # minus user self
  27. return jsonify(count=count)
  28. @ajax_bp.route('/<int:photo_id>/followers-count')
  29. def collectors_count(photo_id):
  30. photo = Photo.query.get_or_404(photo_id)
  31. count = len(photo.collectors)
  32. return jsonify(count=count)
  33. @ajax_bp.route('/collect/<int:photo_id>', methods=['POST'])
  34. def collect(photo_id):
  35. if not current_user.is_authenticated:
  36. return jsonify(message='Login required.'), 403
  37. if not current_user.confirmed:
  38. return jsonify(message='Confirm account required.'), 400
  39. if not current_user.can('COLLECT'):
  40. return jsonify(message='No permission.'), 403
  41. photo = Photo.query.get_or_404(photo_id)
  42. if current_user.is_collecting(photo):
  43. return jsonify(message='Already collected.'), 400
  44. current_user.collect(photo)
  45. if current_user != photo.author and photo.author.receive_collect_notification:
  46. push_collect_notification(collector=current_user, photo_id=photo_id, receiver=photo.author)
  47. return jsonify(message='Photo collected.')
  48. @ajax_bp.route('/uncollect/<int:photo_id>', methods=['POST'])
  49. def uncollect(photo_id):
  50. if not current_user.is_authenticated:
  51. return jsonify(message='Login required.'), 403
  52. photo = Photo.query.get_or_404(photo_id)
  53. if not current_user.is_collecting(photo):
  54. return jsonify(message='Not collect yet.'), 400
  55. current_user.uncollect(photo)
  56. return jsonify(message='Collect canceled.')
  57. @ajax_bp.route('/follow/<username>', methods=['POST'])
  58. def follow(username):
  59. if not current_user.is_authenticated:
  60. return jsonify(message='Login required.'), 403
  61. if not current_user.confirmed:
  62. return jsonify(message='Confirm account required.'), 400
  63. if not current_user.can('FOLLOW'):
  64. return jsonify(message='No permission.'), 403
  65. user = User.query.filter_by(username=username).first_or_404()
  66. if current_user.is_following(user):
  67. return jsonify(message='Already followed.'), 400
  68. current_user.follow(user)
  69. if user.receive_collect_notification:
  70. push_follow_notification(follower=current_user, receiver=user)
  71. return jsonify(message='User followed.')
  72. @ajax_bp.route('/unfollow/<username>', methods=['POST'])
  73. def unfollow(username):
  74. if not current_user.is_authenticated:
  75. return jsonify(message='Login required.'), 403
  76. user = User.query.filter_by(username=username).first_or_404()
  77. if not current_user.is_following(user):
  78. return jsonify(message='Not follow yet.'), 400
  79. current_user.unfollow(user)
  80. return jsonify(message='Follow canceled.')