This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
forked from frol/flask-restplus-server-example
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathresources.py
220 lines (193 loc) · 6.81 KB
/
resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# encoding: utf-8
# pylint: disable=bad-continuation
"""
RESTful API Team resources
--------------------------
"""
import logging
from flask_login import current_user
from flask_restplus_patched import Resource
from flask_restplus_patched._http import HTTPStatus
from app.extensions import db
from app.extensions.api import Namespace, abort
from app.extensions.api.parameters import PaginationParameters
from app.modules.users import permissions
from app.modules.users.models import User
from . import parameters, schemas
from .models import Team, TeamMember
log = logging.getLogger(__name__) # pylint: disable=invalid-name
api = Namespace('teams', description="Teams") # pylint: disable=invalid-name
@api.route('/')
@api.login_required(oauth_scopes=['teams:read'])
class Teams(Resource):
"""
Manipulations with teams.
"""
@api.parameters(PaginationParameters())
@api.response(schemas.BaseTeamSchema(many=True))
def get(self, args):
"""
List of teams.
Returns a list of teams starting from ``offset`` limited by ``limit``
parameter.
"""
#return Team.query.offset(args['offset']).limit(args['limit'])
return Team.objects.skip(args['offset']).limit(args['limit'])
@api.login_required(oauth_scopes=['teams:write'])
@api.parameters(parameters.CreateTeamParameters())
@api.response(schemas.DetailedTeamSchema())
@api.response(code=HTTPStatus.CONFLICT)
def post(self, args):
"""
Create a new team.
"""
with api.commit_or_abort(
default_error_message="Failed to create a new team"
):
team = Team(**args)
team.save()
#db.session.add(team)
team_member = TeamMember(team=team, user=current_user, is_leader=True)
team_member.save()
#db.session.add(team_member)
return team
@api.route('/<int:team_id>')
@api.login_required(oauth_scopes=['teams:read'])
@api.response(
code=HTTPStatus.NOT_FOUND,
description="Team not found.",
)
@api.resolve_object_by_model(Team, 'team')
class TeamByID(Resource):
"""
Manipulations with a specific team.
"""
@api.permission_required(
permissions.OwnerRolePermission,
kwargs_on_request=lambda kwargs: {'obj': kwargs['team']}
)
@api.response(schemas.DetailedTeamSchema())
def get(self, team):
"""
Get team details by ID.
"""
return team
@api.login_required(oauth_scopes=['teams:write'])
@api.permission_required(
permissions.OwnerRolePermission,
kwargs_on_request=lambda kwargs: {'obj': kwargs['team']}
)
@api.permission_required(permissions.WriteAccessPermission())
@api.parameters(parameters.PatchTeamDetailsParameters())
@api.response(schemas.DetailedTeamSchema())
@api.response(code=HTTPStatus.CONFLICT)
def patch(self, args, team):
"""
Patch team details by ID.
"""
with api.commit_or_abort(
default_error_message="Failed to update team details."
):
parameters.PatchTeamDetailsParameters.perform_patch(args, obj=team)
team.save()
#db.session.merge(team)
return team
@api.login_required(oauth_scopes=['teams:write'])
@api.permission_required(
permissions.OwnerRolePermission,
kwargs_on_request=lambda kwargs: {'obj': kwargs['team']}
)
@api.permission_required(permissions.WriteAccessPermission())
@api.response(code=HTTPStatus.CONFLICT)
@api.response(code=HTTPStatus.NO_CONTENT)
def delete(self, team):
"""
Delete a team by ID.
"""
with api.commit_or_abort(
default_error_message="Failed to delete the team."
):
team.delete()
#db.session.delete(team)
return None
@api.route('/<int:team_id>/members/')
@api.login_required(oauth_scopes=['teams:read'])
@api.response(
code=HTTPStatus.NOT_FOUND,
description="Team not found.",
)
@api.resolve_object_by_model(Team, 'team')
class TeamMembers(Resource):
"""
Manipulations with members of a specific team.
"""
@api.permission_required(
permissions.OwnerRolePermission,
kwargs_on_request=lambda kwargs: {'obj': kwargs['team']}
)
@api.permission_required(permissions.OwnerRolePermission(partial=True))
@api.parameters(PaginationParameters())
@api.response(schemas.BaseTeamMemberSchema(many=True))
def get(self, args, team):
"""
Get team members by team ID.
"""
return team.members.skip(args['offset']).limit(args['limit'])
@api.login_required(oauth_scopes=['teams:write'])
@api.permission_required(
permissions.OwnerRolePermission,
kwargs_on_request=lambda kwargs: {'obj': kwargs['team']}
)
@api.permission_required(permissions.WriteAccessPermission())
@api.parameters(parameters.AddTeamMemberParameters())
@api.response(schemas.BaseTeamMemberSchema())
@api.response(code=HTTPStatus.CONFLICT)
def post(self, args, team):
"""
Add a new member to a team.
"""
with api.commit_or_abort(
default_error_message="Failed to update team details."
):
user_id = args.pop('user_id')
#user = User.query.get(user_id)
user = User.objects(user_id=user_id).first()
if user is None:
abort(
code=HTTPStatus.NOT_FOUND,
message="User with id %d does not exist" % user_id
)
team_member = TeamMember(team=team, user=user, **args)
team_member.save()
#db.session.add(team_member)
return team_member
@api.route('/<int:team_id>/members/<int:user_id>')
@api.login_required(oauth_scopes=['teams:read'])
@api.response(
code=HTTPStatus.NOT_FOUND,
description="Team or member not found.",
)
@api.resolve_object_by_model(Team, 'team')
class TeamMemberByID(Resource):
"""
Manipulations with a specific team member.
"""
@api.login_required(oauth_scopes=['teams:write'])
@api.permission_required(
permissions.OwnerRolePermission,
kwargs_on_request=lambda kwargs: {'obj': kwargs['team']}
)
@api.permission_required(permissions.WriteAccessPermission())
@api.response(code=HTTPStatus.CONFLICT)
def delete(self, team, user_id):
"""
Remove a member from a team.
"""
with api.commit_or_abort(
default_error_message="Failed to update team details."
):
team_member = TeamMember.objects(team=team, user_id=user_id).first_or_404()
#team_member = TeamMember.query.filter_by(team=team, user_id=user_id).first_or_404()
team_member.save()
#db.session.delete(team_member)
return None