From 06185d498676ff40eac4d430f55366bed7dacb68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=93=82=E5=B1=91?= <80164656+boxie123@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:10:57 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20datetime=E8=A2=AB=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E3=80=81=E6=96=B0=E5=BB=BA=E7=A9=BA=E9=97=B4=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E4=BF=9D=E5=AD=98=20(#827)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: datetime被过滤、新建空间配置无法保存 * 添加注释 * 降级sqlalchemy版本要求 --- biliup/database/models.py | 3 +++ biliup/web/__init__.py | 15 +++++++++------ pyproject.toml | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/biliup/database/models.py b/biliup/database/models.py index 079997d..877b0a1 100644 --- a/biliup/database/models.py +++ b/biliup/database/models.py @@ -44,6 +44,9 @@ class BaseModel(DeclarativeBase): temp = copy.deepcopy(self.__dict__) # 深复制避免对原数据影响 result = dict() for key, value in temp.items(): # 遍历删除不能被 json 序列化的键值对 + if isinstance(value, datetime): # 特殊处理,保留 datetime + result[key] = value + continue try: json.dumps(value) result[key] = value diff --git a/biliup/web/__init__.py b/biliup/web/__init__.py index 2fa2e9f..109c1b4 100644 --- a/biliup/web/__init__.py +++ b/biliup/web/__init__.py @@ -393,19 +393,22 @@ async def users(request): # record = Configuration.get(Configuration.key == 'config') record = Session.execute( select(Configuration).where(Configuration.key == 'config') - ).scalar_one() + ).scalar_one() # 判断是否只有一行空间配置 record.value = json.dumps(json_data) - Session.commit() + Session.flush() + resp = record.as_dict() # to_save = Configuration(key='config', value=json.dumps(json_data), id=record.id) - except NoResultFound: + except NoResultFound: # 如果数据库中没有空间配置行,新建 to_save = Configuration(key='config', value=json.dumps(json_data)) # to_save.save() Session.add(to_save) - return web.json_response(to_save.as_dict()) - except MultipleResultsFound as e: + Session.flush() + resp = to_save.as_dict() + except MultipleResultsFound as e: # 如果有多行,报错 return web.json_response({"status": 500, 'error': f"有多个空间配置同时存在: {e}"}, status=500) + Session.commit() config.load_from_db() - return web.json_response(record.as_dict()) + return web.json_response(resp) @routes.post('/v1/dump') diff --git a/pyproject.toml b/pyproject.toml index f56507a..a707b65 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ dependencies = [ 'importlib-resources; python_version<"3.9"', 'tomli >= 1.1.0 ; python_version < "3.11"', 'protobuf >= 4.23.0', - "SQLAlchemy>=2.0.27", + "SQLAlchemy>=2.0.0", "alembic>=1.12.1", ] dynamic = ["version"]