fix:修复了查询全部数据时Internal Server Error的bug

This commit is contained in:
RTAkland
2022-12-30 16:41:18 +08:00
parent 403a2670b1
commit cddd01c6ad
3 files changed
+43 -59

No files matched your search

+35 -15
View File
@@ -67,11 +67,11 @@ class MySQL(BaseSQL):
def __init__(self):
super().__init__()
_CONFIG = Config.database.split('@')
user = _CONFIG[0].split(':')[0]
pwd = _CONFIG[0].split(':')[1]
host = _CONFIG[1].split(':')[0]
port = int(_CONFIG[1].split(':')[1].split('/')[0])
db = _CONFIG[1].split('/')[1]
user = _CONFIG[0].split(":")[0]
pwd = _CONFIG[0].split(":")[1]
host = _CONFIG[1].split(":")[0]
port = int(_CONFIG[1].split(":")[1].split("/")[0])
db = _CONFIG[1].split("/")[1]
self.conn = operator.connect(user=user,
passwd=pwd,
@@ -84,34 +84,41 @@ class MySQL(BaseSQL):
class DetaBase:
def __init__(self):
self.__deta = Deta(os.getenv("PJ_DETA"))
self.__data = self.__deta.Base("times")
self.__image = self.__deta.Base("images")
self.__data = self.__deta.AsyncBase("times")
self.__image = self.__deta.AsyncBase("images")
async def __get(self, _id: str) -> tuple:
result = self.__data.get(_id)
result = await self.__data.get(_id)
if result is None:
await self.__put_data(_id)
return tuple([_id, 0])
await self.update(_id, result["times"])
await self.__image.close()
await self.__data.close()
return tuple([_id, result["times"]])
async def __put_data(self, _id: str) -> bool:
self.__data.put({"times": 0}, _id)
await self.__data.put({"times": 0}, _id)
await self.__image.close()
await self.__data.close()
return True
async def __update_data(self, _id: str, times: int) -> bool:
new = {"times": times + 1}
self.__data.update(new, _id)
await self.__data.update(new, _id)
await self.__image.close()
await self.__data.close()
return True
async def __insert_data(self, _id: str) -> bool:
await self.__put_data(_id)
await self.__image.close()
await self.__data.close()
return True
async def __get_images(self, theme: str) -> list:
response = []
result = self.__image.get(theme)
try:
result = await self.__image.get(theme)
for i in result:
if i != "key":
response.append(tuple([
@@ -120,8 +127,8 @@ class DetaBase:
result[i]["width"],
result[i]["height"]
]))
except TypeError:
return []
await self.__image.close()
await self.__data.close()
return response
async def query(self, _id: str) -> tuple:
@@ -137,7 +144,20 @@ class DetaBase:
return True
async def query_all(self) -> list:
pass
res = await self.__data.fetch()
all_items = res.items
while res.last:
res = await self.__data.fetch(last=res.last)
all_items += res.items
await self.__image.close()
await self.__data.close()
result = []
for i in all_items:
result.append((
i["key"],
i["times"]
))
return result
async def query_image(self, theme: str) -> list:
result = await self.__get_images(theme)
-27
View File
@@ -1,27 +0,0 @@
GET http://localhost:8000/api/query-theme/blacked
Accept: application/json
###
GET http://localhost:8000/api/query-theme/lisu
Accept: application/json
###
GET http://localhost:8000/api/query-theme/moebooru
Accept: application/json
###
GET http://localhost:8000/api/query-theme/lewd
Accept: application/json
###
GET http://localhost:8000/api/query/test
Accept: application/json
###
-9
View File
@@ -1,9 +0,0 @@
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/8/30
# @File Name: __init__.py
__all__ = []