|
| 1 | +import re |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from flask import Blueprint, request |
| 5 | +from flask_login import login_required, current_user |
| 6 | + |
| 7 | +from .response import bad_request, not_found, unauthorized |
| 8 | +from ... import acl |
| 9 | +from ...models import Appreciation, Like, User, Mention |
| 10 | +from ...schemas.appreciation import AppreciationSchema, CreateAppreciationSchema |
| 11 | +from ...services import notification |
| 12 | + |
| 13 | +blueprint = Blueprint('api.appreciations', __name__) |
| 14 | + |
| 15 | +appreciation_schema = AppreciationSchema() |
| 16 | +create_appreciation_schema = CreateAppreciationSchema() |
| 17 | + |
| 18 | + |
| 19 | +def get_appreciation_viewer_like(appreciation: Appreciation): |
| 20 | + return Like.get_by_appreciation_and_user(appreciation, current_user) |
| 21 | + |
| 22 | + |
| 23 | +def appreciation_view(appreciation: Appreciation): |
| 24 | + return { |
| 25 | + 'id': appreciation.id, |
| 26 | + 'content': appreciation.content, |
| 27 | + 'created_at': appreciation.created_at, |
| 28 | + 'created_by': appreciation.created_by, |
| 29 | + 'like_count': appreciation.like_count, |
| 30 | + 'comment_count': appreciation.comment_count, |
| 31 | + 'mentions': appreciation.mentions, |
| 32 | + |
| 33 | + 'viewer_like': get_appreciation_viewer_like(appreciation), |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | +@login_required |
| 38 | +@blueprint.route('', methods=['GET']) |
| 39 | +def list_all_appreciations(): |
| 40 | + if not acl.can_view_appreciations(): |
| 41 | + return unauthorized() |
| 42 | + |
| 43 | + formatted_appreciations = [appreciation_view(a) for a in Appreciation.get_all()] |
| 44 | + return appreciation_schema.jsonify(formatted_appreciations, many=True) |
| 45 | + |
| 46 | + |
| 47 | +@login_required |
| 48 | +@blueprint.route('', methods=['PUT']) |
| 49 | +def create_appreciation(): |
| 50 | + if not request.is_json: |
| 51 | + return bad_request() |
| 52 | + |
| 53 | + if not acl.can_create_appreciations(): |
| 54 | + return unauthorized() |
| 55 | + |
| 56 | + appreciation: Appreciation = create_appreciation_schema.load(request.json) |
| 57 | + |
| 58 | + appreciation.created_by = current_user |
| 59 | + appreciation.created_at = datetime.now() |
| 60 | + |
| 61 | + Appreciation.create(appreciation) |
| 62 | + |
| 63 | + mentions = re.findall(r'@[a-zA-Z0-9._]+', appreciation.content) |
| 64 | + |
| 65 | + for mention_text in mentions: |
| 66 | + user = User.get_by_username(mention_text[1:]) |
| 67 | + if user is None: |
| 68 | + continue |
| 69 | + mention = Mention(user=user, appreciation=appreciation) |
| 70 | + Mention.create(mention) |
| 71 | + |
| 72 | + notification.notify_appreciation(appreciation) |
| 73 | + |
| 74 | + return appreciation_schema.jsonify(appreciation) |
| 75 | + |
| 76 | + |
| 77 | +@login_required |
| 78 | +@blueprint.route('/<_id>', methods=['GET']) |
| 79 | +def get_appreciation(_id): |
| 80 | + appreciation = Appreciation.get(_id) |
| 81 | + |
| 82 | + if not appreciation: |
| 83 | + return not_found() |
| 84 | + |
| 85 | + if not acl.can_view_appreciation(appreciation): |
| 86 | + return unauthorized() |
| 87 | + |
| 88 | + return appreciation_schema.jsonify(appreciation) |
| 89 | + |
| 90 | + |
| 91 | +@login_required |
| 92 | +@blueprint.route('/<_id>', methods=['PATCH']) |
| 93 | +def update_appreciation(_id): |
| 94 | + return not_found() |
| 95 | + |
| 96 | + |
| 97 | +@login_required |
| 98 | +@blueprint.route('/<_id>', methods=['DELETE']) |
| 99 | +def delete_appreciation(_id): |
| 100 | + pass |
| 101 | + |
| 102 | + |
| 103 | +@login_required |
| 104 | +@blueprint.route('/<appreciation_id>/likes', methods=['GET']) |
| 105 | +def get_appreciation_likes(appreciation_id): # pylint: disable=unused-argument |
| 106 | + pass |
| 107 | + |
| 108 | + |
| 109 | +@login_required |
| 110 | +@blueprint.route('/<appreciation_id>/likes', methods=['PUT']) |
| 111 | +def like(appreciation_id): # pylint: disable=unused-argument |
| 112 | + pass |
| 113 | + |
| 114 | + |
| 115 | +@login_required |
| 116 | +@blueprint.route('/<appreciation_id>/likes/<like_id>', methods=['DELETE']) |
| 117 | +def delete_like(appreciation_id, like_id): # pylint: disable=unused-argument |
| 118 | + pass |
| 119 | + |
| 120 | + |
| 121 | +@login_required |
| 122 | +@blueprint.route('/<appreciation_id>/comments', methods=['GET']) |
| 123 | +def get_comments(appreciation_id): # pylint: disable=unused-argument |
| 124 | + pass |
| 125 | + |
| 126 | + |
| 127 | +@login_required |
| 128 | +@blueprint.route('/<appreciation_id>/comments', methods=['GET']) |
| 129 | +def create_comment(appreciation_id): # pylint: disable=unused-argument |
| 130 | + pass |
| 131 | + |
| 132 | + |
| 133 | +@login_required |
| 134 | +@blueprint.route('/<appreciation_id>/comments/<comment_id>', methods=['GET']) |
| 135 | +def update_comment(appreciation_id, comment_id): # pylint: disable=unused-argument |
| 136 | + pass |
| 137 | + |
| 138 | + |
| 139 | +@login_required |
| 140 | +@blueprint.route('/<appreciation_id>/comments/<comment_id>', methods=['GET']) |
| 141 | +def delete_comment(appreciation_id, comment_id): # pylint: disable=unused-argument |
| 142 | + pass |
0 commit comments