1
|
|
|
""" |
2
|
|
|
byceps.blueprints.api.tourney.match.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2019 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from flask import abort, jsonify, request, url_for |
10
|
|
|
|
11
|
|
|
from .....services.tourney import match_comment_service, match_service |
12
|
|
|
from .....services.user import service as user_service |
13
|
|
|
from .....util.framework.blueprint import create_blueprint |
14
|
|
|
from .....util.framework.templating import templated |
15
|
|
|
from .....util.views import respond_created, respond_no_content |
16
|
|
|
|
17
|
|
|
from ...decorators import api_token_required |
18
|
|
|
|
19
|
|
|
from .. import signals |
20
|
|
|
|
21
|
|
|
from .schemas import CreateMatchCommentRequest |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
blueprint = create_blueprint('api_tourney_match', __name__) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
# -------------------------------------------------------------------- # |
28
|
|
|
# match comments |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
@blueprint.route('/<uuid:match_id>/comments') |
32
|
|
|
@api_token_required |
33
|
|
|
@templated |
34
|
|
|
def comments_view(match_id): |
35
|
|
|
"""Render the comments on a match.""" |
36
|
|
|
match = _get_match_or_404(match_id) |
37
|
|
|
|
38
|
|
|
party_id = request.args.get('party_id') |
39
|
|
|
|
40
|
|
|
comments = match_comment_service.get_comments( |
41
|
|
|
match.id, party_id=party_id, include_hidden=False |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
return { |
45
|
|
|
'comments': comments, |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
@blueprint.route('/<uuid:match_id>/comments.json') |
50
|
|
|
@api_token_required |
51
|
|
|
def comments_view_as_json(match_id): |
52
|
|
|
"""Render the comments on a match as JSON.""" |
53
|
|
|
match = _get_match_or_404(match_id) |
54
|
|
|
|
55
|
|
|
party_id = request.args.get('party_id') |
56
|
|
|
|
57
|
|
|
comments = match_comment_service.get_comments( |
58
|
|
|
match.id, party_id=party_id, include_hidden=True |
59
|
|
|
) |
60
|
|
|
|
61
|
|
|
comment_dtos = list(map(_comment_to_json, comments)) |
62
|
|
|
|
63
|
|
|
return jsonify({ |
64
|
|
|
'comments': comment_dtos, |
65
|
|
|
}) |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def _comment_to_json(comment): |
69
|
|
|
creator = comment.creator |
70
|
|
|
|
71
|
|
|
return { |
72
|
|
|
'comment_id': str(comment.id), |
73
|
|
|
'match_id': str(comment.match_id), |
74
|
|
|
'created_at': comment.created_at.isoformat(), |
75
|
|
|
'creator': { |
76
|
|
|
'user_id': creator.id, |
77
|
|
|
'screen_name': creator.screen_name, |
78
|
|
|
'suspended': creator.suspended, |
79
|
|
|
'deleted': creator.deleted, |
80
|
|
|
'avatar_url': creator.avatar_url, |
81
|
|
|
'is_orga': creator.is_orga, |
82
|
|
|
}, |
83
|
|
|
'body': comment.body_rendered, |
84
|
|
|
'hidden': comment.hidden, |
85
|
|
|
'hidden_at': comment.hidden_at.isoformat() \ |
86
|
|
|
if (comment.hidden_at is not None) else None, |
87
|
|
|
'hidden_by_id': comment.hidden_by_id, |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
blueprint.add_url_rule( |
92
|
|
|
'/<uuid:match_id>/comments/<uuid:comment_id>', |
93
|
|
|
endpoint='comment_view', |
94
|
|
|
build_only=True, |
95
|
|
|
) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
@blueprint.route('/<uuid:match_id>/comments', methods=['POST']) |
99
|
|
|
@api_token_required |
100
|
|
|
@respond_created |
101
|
|
|
def comment_create(match_id): |
102
|
|
|
"""Create a comment on a match.""" |
103
|
|
|
match = _get_match_or_404(match_id) |
104
|
|
|
|
105
|
|
|
schema = CreateMatchCommentRequest() |
106
|
|
|
try: |
107
|
|
|
req = schema.load(request.get_json()) |
108
|
|
|
except ValidationError as e: |
|
|
|
|
109
|
|
|
abort(400, str(e.normalized_messages())) |
110
|
|
|
|
111
|
|
|
creator = user_service.find_active_user(req['creator_id']) |
112
|
|
|
if not creator: |
113
|
|
|
abort(400, 'Creator ID does not reference an active user.') |
114
|
|
|
|
115
|
|
|
body = req['body'].strip() |
116
|
|
|
|
117
|
|
|
comment = match_comment_service.create_comment(match.id, creator.id, body) |
118
|
|
|
|
119
|
|
|
signals.match_comment_created.send(None, comment_id=comment.id) |
120
|
|
|
|
121
|
|
|
return url_for('.comment_view', match_id=match.id, comment_id=comment.id) |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
@blueprint.route( |
125
|
|
|
'/<uuid:match_id>/comments/<uuid:comment_id>/flags/hidden', methods=['POST'] |
126
|
|
|
) |
127
|
|
|
@api_token_required |
128
|
|
|
@respond_no_content |
129
|
|
|
def comment_hide(match_id, comment_id): |
130
|
|
|
"""Hide the match comment.""" |
131
|
|
|
initiator_id = request.form.get('initiator_id') |
132
|
|
|
if not initiator_id: |
133
|
|
|
abort(400, 'Initiator ID missing') |
134
|
|
|
|
135
|
|
|
match_comment_service.hide_comment(comment_id, initiator_id) |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
@blueprint.route( |
139
|
|
|
'/<uuid:match_id>/comments/<uuid:comment_id>/flags/hidden', |
140
|
|
|
methods=['DELETE'], |
141
|
|
|
) |
142
|
|
|
@api_token_required |
143
|
|
|
@respond_no_content |
144
|
|
|
def comment_unhide(match_id, comment_id): |
145
|
|
|
"""Un-hide the match comment.""" |
146
|
|
|
initiator_id = request.form.get('initiator_id') |
147
|
|
|
if not initiator_id: |
148
|
|
|
abort(400, 'Initiator ID missing') |
149
|
|
|
|
150
|
|
|
match_comment_service.unhide_comment(comment_id, initiator_id) |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
def _get_match_or_404(match_id): |
154
|
|
|
match = match_service.find_match(match_id) |
155
|
|
|
|
156
|
|
|
if match is None: |
157
|
|
|
abort(404) |
158
|
|
|
|
159
|
|
|
return match |
160
|
|
|
|