-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
342 lines (304 loc) · 9.5 KB
/
app.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from json import JSONEncoder
from flask import Flask, request, jsonify
from flask_restful import Api as RestfulApi, Resource, reqparse
from flask_jwt_extended import JWTManager, jwt_required, create_access_token, get_jwt_identity, get_jwt
from flask_migrate import Migrate
from flask_swagger_ui import get_swaggerui_blueprint
from flask_restful_swagger_2 import Api as SwaggerApi, swagger
from flask_cors import CORS
from config import Config
from models import db, Project, Pathogen, Study
import jwt
import datetime
app = Flask(__name__)
app.config.from_object(Config)
CORS(app, resources={r"/api/*": {"origins": "*", "methods": ["GET", "POST", "PUT", "DELETE"]}})
if not hasattr(app, 'json_encoder'):
app.json_encoder = JSONEncoder
db.init_app(app)
migrate = Migrate(app, db)
jwtm = JWTManager(app)
api = SwaggerApi(app, api_version='0.1', api_spec_url='/swagger')
SWAGGER_URL = '/swagger'
API_URL = '/swagger.json'
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "SANBI Projects"
}
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
class User(Resource):
@jwt_required()
def get(self):
claims = get_jwt()
return claims
class Projects(Resource):
@swagger.doc({
'tags': ['projects'],
'description': 'Get all projects',
'responses': {
'200': {
'description': 'List of projects'
}
}
})
def get(self):
projects = Project.query.all()
project_list = []
for project in projects:
project_data = {
'id': project.id,
'title': project.title,
'pid': project.pid,
'group': project.group,
'description': project.description,
'owner_id': project.owner_id,
'created_at': project.created_at.strftime('%Y-%m-%d %H:%M:%S'),
'updated_at': project.updated_at.strftime('%Y-%m-%d %H:%M:%S')
}
project_list.append(project_data)
return project_list
@jwt_required()
@swagger.doc({
'tags': ['projects'],
'description': 'Create a new project',
'parameters': [
{
'name': 'title',
'description': 'Project title',
'in': 'formData',
'type': 'string',
'required': True
},
{
'name': 'pid',
'description': 'Project ID',
'in': 'formData',
'type': 'string',
'required': True
},
{
'name': 'group',
'description': 'Project group',
'in': 'formData',
'type': 'string',
'required': True
},
{
'name': 'description',
'description': 'Project description',
'in': 'formData',
'type': 'string',
'required': True
}
],
'responses': {
'201': {
'description': 'Project created'
}
}
})
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('title', required=True)
parser.add_argument('pid', required=True)
parser.add_argument('group', required=True)
parser.add_argument('description', required=True)
args = parser.parse_args()
new_project = Project(
title=args['title'],
pid=args['pid'],
group=args['group'],
description=args['description'],
owner_id=get_jwt_identity()
)
db.session.add(new_project)
db.session.commit()
return new_project.as_dict(), 201
@jwt_required()
@swagger.doc({
'tags': ['projects'],
'description': 'Update a project',
'parameters': [
{
'name': 'id',
'description': 'Project ID',
'in': 'formData',
'type': 'integer',
'required': True
},
{
'name': 'title',
'description': 'Project title',
'in': 'formData',
'type': 'string',
'required': False
},
{
'name': 'pid',
'description': 'Project ID',
'in': 'formData',
'type': 'string',
'required': False
},
{
'name': 'group',
'description': 'Project group',
'in': 'formData',
'type': 'string',
'required': False
},
{
'name': 'description',
'description': 'Project description',
'in': 'formData',
'type': 'string',
'required': False
}
],
'responses': {
'200': {
'description': 'Project updated'
}
}
})
def put(self):
parser = reqparse.RequestParser()
parser.add_argument('id', type=int, required=True)
parser.add_argument('title')
parser.add_argument('pid')
parser.add_argument('group')
parser.add_argument('description')
args = parser.parse_args()
project = Project.query.get(args['id'])
if not project:
return {'message': 'Project not found'}, 404
if args['title']:
project.title = args['title']
if args['pid']:
project.pid = args['pid']
if args['group']:
project.group = args['group']
if args['description']:
project.description = args['description']
db.session.commit()
return project.as_dict(), 200
@jwt_required()
@swagger.doc({
'tags': ['projects'],
'description': 'Delete a project',
'parameters': [
{
'name': 'id',
'description': 'Project ID',
'in': 'formData',
'type': 'integer',
'required': True
}
],
'responses': {
'204': {
'description': 'Project deleted'
}
}
})
def delete(self):
parser = reqparse.RequestParser()
parser.add_argument('id', type=int, required=True)
args = parser.parse_args()
project = Project.query.get(args['id'])
if not project:
return {'message': 'Project not found'}, 404
db.session.delete(project)
db.session.commit()
return {}, 204
class Pathogens(Resource):
@swagger.doc({
'tags': ['pathogens'],
'description': 'Get all pathogens',
'responses': {
'200': {
'description': 'List of pathogens'
}
}
})
def get(self):
pathogens = Pathogen.query.all()
pathogen_list = []
for pathogen in pathogens:
pathogen_data = {
'id': pathogen.id,
'common_name': pathogen.common_name,
'scientific_name': pathogen.scientific_name,
'schema': pathogen.schema,
'created_at': pathogen.created_at.strftime('%Y-%m-%d %H:%M:%S'),
'updated_at': pathogen.updated_at.strftime('%Y-%m-%d %H:%M:%S')
}
pathogen_list.append(pathogen_data)
return pathogen_list
def post(self):
pass
def put(self):
pass
def delete(self):
pass
class Studies(Resource):
@swagger.doc({
'tags': ['studies'],
'description': 'Get all studies',
'responses': {
'200': {
'description': 'List of studies'
}
}
})
def get(self):
pass
def post(self):
pass
def put(self):
pass
def delete(self):
pass
class GenerateToken(Resource):
def get(self):
user_payload = {
"iat": datetime.datetime.utcnow(),
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1),
"sub": "3423616",
"iss": "ego",
"jti": "2323232323",
"context": {
"scope": [
"VIRUS-SEQ.WRITE",
"VIRUS-SEQ.READ"
],
"user": {
"email": "user@domain.com",
"status": "APPROVED",
"firstName": "Di",
"lastName": "MMM",
"createdAt": 1715867872506,
"lastLogin": 1717576566031,
"preferredLanguage": None,
"providerType": "KEYCLOAK",
"providerSubjectId": "eee",
"type": "ADMIN",
"groups": [
"1111"
]
}
},
"aud": []
}
token = jwt.encode(user_payload, app.config['JWT_SECRET_KEY'], algorithm='HS256')
return jsonify({'token': token})
api.add_resource(User, '/api/')
api.add_resource(Projects, '/api/projects/')
api.add_resource(Pathogens, '/api/pathogens/')
api.add_resource(Studies, '/api/studies/')
api.add_resource(GenerateToken, '/api/generate-token')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')