-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
327 lines (291 loc) · 9.87 KB
/
main.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
from fastapi.middleware.cors import CORSMiddleware
from src.models.models import *
from src.examples import *
from fastapi_offline import FastAPIOffline
import uvicorn
from tortoise import Tortoise
from fastapi import Body
from tortoise.contrib.fastapi import register_tortoise
from pydantic import BaseModel, validator, ValidationError
# Initial config
app = FastAPIOffline(title='Gen App')
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
register_tortoise(
app,
db_url='sqlite://db.sqlite3',
modules={'models': ['src.models.models']},
generate_schemas=True,
add_exception_handlers=True,
)
Tortoise.init_models(["src.models.models"], "models")
Tortoise.generate_schemas()
class Item(BaseModel):
target_class: str
data: dict
@validator('target_class', allow_reuse=True)
def target_class_validator(cls, v):
if v not in list(adapter_dict['models'].keys()):
raise ValidationError
return v
@app.post("/create_single", name="Создать один объект")
async def create_single(item: Item = Body(
...,
examples={
"create_gene": {
"description": "Create gene",
"value": ADD_GENE_EXAMPLE
},
"create_person": {
"description": "Create person",
"value": ADD_PERSON_EXAMPLE
}
}
)):
target_class = item.target_class
data = item.data
status = "Ok"
error_data = {}
if target_class == 'people':
genes = data.get('genes')
data.pop('genes')
try:
obj_creator = adapter_dict['objects_in'][target_class].parse_obj(data)
response = await adapter_dict['models'][target_class].create(**obj_creator.dict())
if target_class == 'people':
data = {
"people_id": {
"id": response.id
},
"genes_ids": genes
}
body = Item(target_class="people", data=data)
await tie_single(body)
except Exception as e:
error_data[f'error in {data}'] = str(e)
status = "not ok"
return {"status": status, "data": response, "error_data": error_data}
@app.post('/create_many', name="Создать несколько объектов")
async def create_many(item: Item = Body(
...,
examples={
"create_many_genes": {
"description": "Create genes",
"value": ADD_MANY_GENES_EXAMPLE
},
"create_many_persons": {
"description": "Create persons",
"value": ADD_MANY_PERSONS_EXAMPLE
}
}
)):
target_class = item.target_class
data = item.data
num_of_items = len(data['list_to_create'])
status = 'stopped at the beginning'
created_objects = []
error_data = {}
num_created_items = 0
for obj in data['list_to_create']:
try:
obj_creator = adapter_dict['objects_in'][target_class].parse_obj(obj)
created_obj = await adapter_dict['models'][target_class].create(**obj_creator.dict())
created_objects.append(created_obj)
num_created_items += 1
except Exception as e:
error_data[f"in: {obj}"] = f"error: {e}"
status = 'not ok' if len(error_data) else 'ok'
return {
'status': status,
'data': f'IDs that were created: {created_objects}',
"created": f'{num_created_items}/{num_of_items}',
"error_data": error_data,
}
@app.post('/get_all', name="Получить все объекты")
async def get_all(item: Item = Body(
...,
examples={
"get_all_genes": {
"description": "Get genes",
"value": GET_ALL_GENES
},
"get_all_persons": {
"description": "Get persons",
"value": GET_ALL_PEOPLE
}
}
)):
target_class = item.target_class
response = await adapter_dict['objects_out'][target_class].from_queryset(adapter_dict['models'][target_class].all())
num_of_items = len(response)
return {"status": "Ok", "data": response, "num_of_items": num_of_items}
@app.post('/get_single', name="Получить один объект")
async def get_single(item: Item = Body(
...,
examples={
"get_single_gene": {
"description": "Get single gene",
"value": GET_SINGLE_GENE_EXAMPLE
},
"get_single_person": {
"description": "Get single person",
"value": GET_SINGLE_PERSON_EXAMPLE
}
}
)):
target_class = item.target_class
data = item.data
try:
response = (
await adapter_dict['objects_out'][target_class].from_queryset_single(
adapter_dict['models'][target_class].get(**data)))
except Exception as e:
return {"status": "not ok", "data": str(e)}
return {"status": "Ok", "data": response}
@app.post('/delete', name="Удалить один или несколько объектов")
async def delete(item: Item = Body(
...,
examples={
"delete_genes": {
"description": "Delete genes",
"value": DEL_GENE_EXAMPLE
},
"delete_persons": {
"description": "Delete persons",
"value": DEL_PERSON_EXAMPLE
}
}
)):
target_class = item.target_class
data = item.data
deleted_objects = []
num_deleted_items = 0
num_of_items = len(data['list_to_delete'])
error_data = {}
status = 'error at the beginning'
for obj in data['list_to_delete']:
try:
obj_to_delete = await adapter_dict['models'][target_class].get(**obj)
await obj_to_delete.delete()
deleted_objects.append(obj['id'])
num_deleted_items += 1
except Exception as e:
error_data[f"in ID: {obj['id']}"] = f"error: {e}"
status = 'not ok' if len(error_data) else 'ok'
return {
'status': status,
'data': f'IDs that were deleted: {deleted_objects}',
"deleted": f'{num_deleted_items}/{num_of_items}',
"error_data": error_data,
}
@app.post('/get_all_depend', name="Получить все объекты со связями")
async def get_all_depend(item: Item = Body(
...,
examples={
"get_all_persons": {
"description": "Get persons",
"value": GET_ALL_PEOPLE
}
}
)):
target_class = "people"
response = await adapter_dict['models'][target_class].all().prefetch_related("genes")
num_of_items = len(response)
for i, x in enumerate(response):
list_for_person = x.genes.related_objects
response[i] = response[i].__dict__
response[i].pop("_genes")
response[i].pop("_partial")
response[i]["genes"] = list_for_person
return {"status": "Ok", "data": response, "num_of_items": num_of_items}
@app.post('/tie', name="Объединить один объект другими")
async def tie_single(item: Item = Body(
...,
examples={
"tie_gene_with_persons": {
"description": "Delete genes",
"value": TIE_GENE_EXAMPLE
},
"tie_person_with_genes": {
"description": "Delete persons",
"value": TIE_PERSON_EXAMPLE
}
}
)):
"""
target_class - То, к чему присоединяем
"""
target_class = item.target_class
data = item.data
errors_list = []
status = 'error at the beginning'
try:
objects_to_tie = []
target_obj = await adapter_dict['models'][target_class].get(**data[f'{target_class}_id'])
combiners = 'people' if target_class == 'genes' else 'genes'
for obj in data[f"{combiners}_ids"]:
try:
objects_to_tie.append(
await adapter_dict['models'][combiners].get(**obj)
)
except Exception as e:
errors_list.append(f'{str(e)} in {obj}')
if target_class == 'people':
await target_obj.genes.add(*objects_to_tie)
else:
await target_obj.people.add(*objects_to_tie)
status = 'not ok' if errors_list else 'ok'
num_of_success = len(objects_to_tie)
num_of_combiners = len(data[f'{combiners}_ids'])
return {"status": status,
"data": f"successfully combined {num_of_success}/{num_of_combiners}",
"errors": errors_list
}
except Exception as e:
return {"status": status, "data": f"{str(e)} while adding preprocessed objects"}
@app.post('/get_params_of_object', name="Получить связи объекта")
async def get_params_of_obj(item: Item = Body(
...,
examples={
"get_gene_with_persons": {
"description": "gene's persons",
"value": GET_GENE_WITH_PERSONS_EXAMPLE
},
"get_person_with_genes": {
"description": "persons' genes",
"value": GET_PERSON_WITH_GENES_EXAMPLE
}
}
)):
target_class = item.target_class
data = item.data
error_data = []
response = x = full_info = 'error at the beginning'
combiners = 'genes' if target_class == 'people' else 'people'
try:
target_obj = await adapter_dict['models'][target_class].get(**data[f'{target_class}_id'])
response = []
await target_obj.fetch_related(combiners)
if target_class == 'people':
for x in target_obj.genes:
response.append(x)
else:
for x in target_obj.people:
response.append(x)
full_info = {f'{combiners} ids for {target_class} with ID {target_obj.id}: {[x.id for x in response]}'}
except Exception as e:
error_data.append(f'{e} in {x}')
return {
"status": "ok",
"data": response,
"full_info": full_info,
"error_data": error_data
}
if __name__ == '__main__':
uvicorn.run(app)