-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_association_getter.py
207 lines (184 loc) · 7.98 KB
/
api_association_getter.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
from fastapi import APIRouter, Request, Response, Query, Path
from fastapi.responses import JSONResponse
import tools
from ow_config import MAIN_URL
from sqlalchemy.orm import sessionmaker
from sqlalchemy import insert
from sql_logic import sql_catalog as catalog
router = APIRouter()
@router.get(
MAIN_URL+"/list/tags",
tags=["Tag", "Game", "Association"],
summary="Ассоциации тегов с играми",
status_code=200,
responses={
200: {
"description": "OK",
"content": {
"application/json": {
"example": {
"database_size": 123,
"offset": 123,
"results": [
{"id": 1, "name": "?"},
{"id": 2, "name": "!"},
]
}
}
}
},
413: {
"description": "Неккоректный диапазон параметров(размеров).",
"content": {
"application/json": {
"example": {
"message": "incorrect page size",
"error_id": 1
}
}
}
}
}
)
async def list_tags(
game_id: int = Query(-1, description="ID игры *(для активации фильтра значение `>0`)*."),
page_size: int = Query(10, description="Размер 1 страницы. Диапазон - 1...50 элементов."),
page: int = Query(0, description="Номер страницы. Не должна быть отрицательной."),
name: str = Query("", description="Поиск по названию.", max_length=128),
tags_ids = Query([], description="Фильтрация по id тегов *(массив id)*.", example="[1, 2, 3]"),
):
"""
Возвращает список тегов. Они могут быть отфильтрованны по закрепленности за конкретной игрой.
"""
if page_size > 50 or page_size < 1:
return JSONResponse(status_code=413, content={"message": "incorrect page size", "error_id": 1})
tags_ids = tools.str_to_list(tags_ids)
# Создание сессии
session = sessionmaker(bind=catalog.engine)()
# Выполнение запроса
query = session.query(catalog.Tag)
if game_id > 0:
query = query.filter(catalog.Tag.associated_games.any(catalog.Game.id == game_id))
if len(name) > 0:
query = query.filter(catalog.Tag.name.ilike(f'%{name}%'))
if len(tags_ids) > 0:
query = query.filter(catalog.Tag.id.in_(tags_ids))
tags_count = query.count()
offset = page_size * page
tags = query.offset(offset).limit(page_size).all()
session.close()
return {"database_size": tags_count, "offset": offset, "results": tags}
@router.get(
MAIN_URL+"/list/tags/mods/{mods_ids_list}",
tags=["Mod", "Tag", "Association"],
summary="Ассоциации модов с тегами",
status_code=200,
responses={
200: {
"description": "OK",
"content": {
"application/json": {
"example": {
1: [
{"id": 1, "name": "tag1"},
{"id": 2, "name": "tag2"}
]
}
}
}
}
}
)
async def list_tags_for_mods(
response: Response,
request: Request,
mods_ids_list = Path(description="Список модов к которым нужно вернуть ассоциации.", example="[1, 2, 3]"),
tags = Query([], description="Список тегов ассоциации с которыми интересуют.", example="[1, 2, 3]"),
only_ids: bool = Query(False, description="Если True вернет только ID тегов, если False вернет все данные о теге.")
):
"""
Возвращает ассоциации модов с тегами.
"""
mods_ids_list = tools.str_to_list(mods_ids_list)
tags = tools.str_to_list(tags)
if (len(mods_ids_list) + len(tags)) > 80:
return JSONResponse(status_code=413,
content={"message": "the maximum complexity of filters is 80 elements in sum",
"error_id": 1})
# Создание сессии
session = sessionmaker(bind=catalog.engine)()
query = session.query(catalog.Mod.id)
query = query.filter(catalog.Mod.id.in_(mods_ids_list))
if len(query.all()) > 0:
result_access = await tools.access_mods(response=response, request=request, mods_ids=mods_ids_list)
if result_access != True:
return result_access
# Выполнение запроса
result = {}
query_global = session.query(catalog.Tag).join(catalog.mods_tags)
for mod_id in mods_ids_list:
query = query_global.filter(catalog.mods_tags.c.mod_id == mod_id)
if len(tags) > 0:
query = query.filter(catalog.Tag.id.in_(tags))
if only_ids:
if result.get(mod_id, None) == None: result[mod_id] = []
for id in query.all(): result[mod_id].append(id.id)
else:
result[mod_id] = query.all()
return result
@router.get(
MAIN_URL+"/list/genres/games/{games_ids_list}",
tags=["Game", "Genre", "Association"],
summary="Ассоциации игр с жанрами",
status_code=200,
responses={
200: {
"description": "Запрос успешно обработан.",
"content": {
"application/json": {
"example": {
123: [{"id": 1, "name": "Стратегия"}]
}
}
},
},
413: {
"description": "Превышен максимальный размер сложности фильтрации.",
"content": {
"application/json": {
"example": {"message": "the maximum complexity of filters is 80 elements in sum", "error_id": 2}
}
},
},
}
)
async def list_genres_for_games(
games_ids_list = Path(..., description="Список ID запрошенных игр.", example="[1, 2, 3]"),
genres = Query([], description="Фильтрация по ID жанров (т.е. если жанра нет в переденном списке, он не передается). Неактивен если пуст.", example="[1, 2, 3]"),
only_ids: bool = Query(False, description="Возвращать только массив ID жанров. В обычной ситуации возвращает массив словарей с подробной информацией."),
):
"""
Передает информацию о жанрах запрошенных игр.
"""
games_ids_list = tools.str_to_list(games_ids_list)
genres = tools.str_to_list(genres)
if (len(games_ids_list) + len(genres)) > 80:
return JSONResponse(status_code=413,
content={"message": "the maximum complexity of filters is 80 elements in sum",
"error_id": 2})
# Создание сессии
Session = sessionmaker(bind=catalog.engine)
session = Session()
# Выполнение запроса
result = {}
query_global = session.query(catalog.Genre).join(catalog.game_genres)
for game_id in games_ids_list:
query = query_global.filter(catalog.game_genres.c.game_id == game_id)
if len(genres) > 0:
query = query.filter(catalog.Genre.id.in_(genres))
if only_ids:
if result.get(game_id, None) == None: result[game_id] = []
for id in query.all(): result[game_id].append(id.id)
else:
result[game_id] = query.all()
return result