1
|
|
|
""" |
2
|
|
|
byceps.blueprints.api.user_badge.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2019 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from flask import abort, request |
10
|
|
|
|
11
|
|
|
from ....services.user import service as user_service |
12
|
|
|
from ....services.user_badge import ( |
13
|
|
|
command_service as badge_command_service, |
14
|
|
|
service as badge_service, |
15
|
|
|
) |
16
|
|
|
from ....util.framework.blueprint import create_blueprint |
17
|
|
|
from ....util.views import respond_no_content |
18
|
|
|
|
19
|
|
|
from ..decorators import api_token_required |
20
|
|
|
|
21
|
|
|
from ...user_badge import signals |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
blueprint = create_blueprint('api_user_badge', __name__) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
@blueprint.route('/awardings', methods=['POST']) |
28
|
|
|
@api_token_required |
29
|
|
|
@respond_no_content |
30
|
|
|
def award_badge_to_user(): |
31
|
|
|
"""Award the badge to a user.""" |
32
|
|
|
badge = _get_badge_or_400() |
33
|
|
|
user = _get_user_or_400() |
34
|
|
|
initiator = _get_initiator_or_400() |
35
|
|
|
|
36
|
|
|
_, event = badge_command_service.award_badge_to_user( |
37
|
|
|
badge.id, user.id, initiator_id=initiator.id |
38
|
|
|
) |
39
|
|
|
|
40
|
|
|
signals.user_badge_awarded.send(None, event=event) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def _get_badge_or_400(): |
44
|
|
|
badge_id = request.form['badge_id'] |
45
|
|
|
if badge_id is None: |
46
|
|
|
abort(400, 'Badge ID missing') |
47
|
|
|
|
48
|
|
|
badge = badge_service.find_badge(badge_id) |
49
|
|
|
if not badge: |
50
|
|
|
abort(400, 'Badge ID unknown') |
51
|
|
|
|
52
|
|
|
return badge |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def _get_user_or_400(): |
|
|
|
|
56
|
|
|
user_id = request.form['user_id'] |
57
|
|
|
if not user_id: |
58
|
|
|
abort(400, 'User ID missing') |
59
|
|
|
|
60
|
|
|
user = user_service.find_user(user_id) |
61
|
|
|
if not user: |
62
|
|
|
abort(400, 'User ID unknown') |
63
|
|
|
|
64
|
|
|
return user |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def _get_initiator_or_400(): |
68
|
|
|
initiator_id = request.form['initiator_id'] |
69
|
|
|
if not initiator_id: |
70
|
|
|
abort(400, 'Initiator ID missing') |
71
|
|
|
|
72
|
|
|
initiator = user_service.find_user(initiator_id) |
73
|
|
|
if not initiator: |
74
|
|
|
abort(400, 'Initiator ID unknown') |
75
|
|
|
|
76
|
|
|
return initiator |
77
|
|
|
|