Skip to content

Commit 02d2574

Browse files
committed
Add bulk update interface for config plugin
1 parent 3a32522 commit 02d2574

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

backend/plugin/config/api/v1/sys/config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,21 @@
1414
CreateConfigParam,
1515
GetConfigDetail,
1616
UpdateConfigParam,
17+
UpdateConfigsParam,
1718
)
1819
from backend.plugin.config.service.config_service import config_service
1920

2021
router = APIRouter()
2122

2223

24+
@router.get('/all', summary='获取所有参数配置', dependencies=[DependsJwtAuth])
25+
async def get_all_configs(
26+
type: Annotated[str | None, Query(description='参数配置类型')] = None,
27+
) -> ResponseSchemaModel[list[GetConfigDetail]]:
28+
configs = await config_service.get_all(type=type)
29+
return response_base.success(data=configs)
30+
31+
2332
@router.get('/{pk}', summary='获取参数配置详情', dependencies=[DependsJwtAuth])
2433
async def get_config(pk: Annotated[int, Path(description='参数配置 ID')]) -> ResponseSchemaModel[GetConfigDetail]:
2534
config = await config_service.get(pk=pk)
@@ -57,6 +66,14 @@ async def create_config(obj: CreateConfigParam) -> ResponseModel:
5766
return response_base.success()
5867

5968

69+
@router.put('', summary='批量更新参数配置', dependencies=[Depends(RequestPermission('sys.config.edits')), DependsRBAC])
70+
async def bulk_update_config(objs: list[UpdateConfigsParam]) -> ResponseModel:
71+
count = await config_service.bulk_update(objs=objs)
72+
if count > 0:
73+
return response_base.success()
74+
return response_base.fail()
75+
76+
6077
@router.put(
6178
'/{pk}',
6279
summary='更新参数配置',

backend/plugin/config/crud/crud_config.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33
from typing import Sequence
44

5-
from sqlalchemy import Select
5+
from sqlalchemy import Select, update
66
from sqlalchemy.ext.asyncio import AsyncSession
77
from sqlalchemy_crud_plus import CRUDPlus
88

@@ -23,7 +23,7 @@ async def get(self, db: AsyncSession, pk: int) -> Config | None:
2323
"""
2424
return await self.select_model_by_column(db, id=pk)
2525

26-
async def get_by_type(self, db: AsyncSession, type: str) -> Sequence[Config | None]:
26+
async def get_all(self, db: AsyncSession, type: str) -> Sequence[Config | None]:
2727
"""
2828
通过键名获取参数配置
2929
@@ -81,6 +81,18 @@ async def update(self, db: AsyncSession, pk: int, obj: UpdateConfigParam) -> int
8181
"""
8282
return await self.update_model(db, pk, obj)
8383

84+
async def bulk_update(self, db: AsyncSession, objs: list[UpdateConfigParam]) -> int:
85+
"""
86+
批量更新参数配置
87+
88+
:param db: 数据库会话
89+
:param objs: 批量更新参数配置参数
90+
:return:
91+
"""
92+
params = [obj.model_dump(exclude_unset=True) for obj in objs]
93+
await db.execute(update(self.model), params)
94+
return len(params)
95+
8496
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
8597
"""
8698
批量删除参数配置

backend/plugin/config/plugin.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[plugin]
22
summary = '参数配置'
3-
version = '0.0.1'
4-
description = '通常用于前端工程数据展示'
3+
version = '0.0.2'
4+
description = '通常用于动态配置系统参数/前端工程数据展示'
55
author = 'wu-clan'
66

77
[app]

backend/plugin/config/schema/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class UpdateConfigParam(ConfigSchemaBase):
2626
"""更新参数配置参数"""
2727

2828

29+
class UpdateConfigsParam(UpdateConfigParam):
30+
"""批量更新参数配置参数"""
31+
32+
id: int = Field(description='参数配置 ID')
33+
34+
2935
class GetConfigDetail(ConfigSchemaBase):
3036
"""参数配置详情"""
3137

backend/plugin/config/service/config_service.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from backend.plugin.config.schema.config import (
1111
CreateConfigParam,
1212
UpdateConfigParam,
13+
UpdateConfigsParam,
1314
)
1415

1516

@@ -30,6 +31,17 @@ async def get(*, pk: int) -> Config:
3031
raise errors.NotFoundError(msg='参数配置不存在')
3132
return config
3233

34+
@staticmethod
35+
async def get_all(*, type: str | None):
36+
"""
37+
获取所有参数配置
38+
39+
:param type: 参数配置类型
40+
:return:
41+
"""
42+
async with async_db_session() as db:
43+
return await config_dao.get_all(db, type)
44+
3345
@staticmethod
3446
async def get_select(*, name: str | None, type: str | None) -> Select:
3547
"""
@@ -75,6 +87,27 @@ async def update(*, pk: int, obj: UpdateConfigParam) -> int:
7587
count = await config_dao.update(db, pk, obj)
7688
return count
7789

90+
@staticmethod
91+
async def bulk_update(*, objs: list[UpdateConfigsParam]) -> int:
92+
"""
93+
批量更新参数配置
94+
95+
:param objs: 参数配置批量更新参数
96+
:return:
97+
"""
98+
async with async_db_session.begin() as db:
99+
for batch in range(0, len(objs), 1000):
100+
for obj in objs:
101+
config = await config_dao.get(db, obj.id)
102+
if not config:
103+
raise errors.NotFoundError(msg='参数配置不存在')
104+
if config.key != obj.key:
105+
config = await config_dao.get_by_key(db, obj.key)
106+
if config:
107+
raise errors.ConflictError(msg=f'参数配置 {obj.key} 已存在')
108+
count = await config_dao.bulk_update(db, objs)
109+
return count
110+
78111
@staticmethod
79112
async def delete(*, pks: list[int]) -> int:
80113
"""

backend/plugin/email/utils/send.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ async def send_email(
6868
"""
6969
# 动态配置
7070
dynamic_email_config = None
71+
_email_type_key = 'EMAIL'
72+
_email_status_key = 'EMAIL_STATUS'
73+
_email_host_key = 'EMAIL_HOST'
74+
_email_port_key = 'EMAIL_PORT'
75+
_email_ssl_key = 'EMAIL_SSL'
76+
_email_username_key = 'EMAIL_USERNAME'
77+
_email_password_key = 'EMAIL_PASSWORD'
7178

7279
# 检查 config 插件配置
7380
def get_config_table(conn):
@@ -78,24 +85,24 @@ def get_config_table(conn):
7885
exists = await coon.run_sync(get_config_table)
7986

8087
if exists:
81-
dynamic_email_config = await config_dao.get_by_type(db, 'email')
88+
dynamic_email_config = await config_dao.get_all(db, _email_type_key)
8289

8390
try:
8491
# 动态配置发送
8592
if dynamic_email_config:
8693
configs = {d['key']: d for d in select_list_serialize(dynamic_email_config)}
87-
if configs.get('EMAIL_STATUS'):
94+
if configs.get(_email_status_key):
8895
if len(dynamic_email_config) < 6:
8996
raise errors.NotFoundError(msg='缺少邮件动态配置,请检查系统参数配置-邮件配置')
9097
smtp_client = SMTP(
91-
hostname=configs.get('EMAIL_HOST'),
92-
port=configs.get('EMAIL_PORT'),
93-
use_tls=configs.get('EMAIL_SSL') == '1',
98+
hostname=configs.get(_email_host_key),
99+
port=configs.get(_email_port_key),
100+
use_tls=configs.get(_email_ssl_key) == '1',
94101
)
95-
message = await render_message(subject, configs.get('EMAIL_USERNAME'), content, template) # type: ignore
102+
message = await render_message(subject, configs.get(_email_username_key), content, template) # type: ignore
96103
async with smtp_client:
97-
await smtp_client.login(configs.get('EMAIL_USERNAME'), configs.get('EMAIL_PASSWORD')) # type: ignore
98-
await smtp_client.sendmail(configs.get('EMAIL_USERNAME'), recipients, message) # type: ignore
104+
await smtp_client.login(configs.get(_email_username_key), configs.get(_email_password_key)) # type: ignore
105+
await smtp_client.sendmail(configs.get(_email_username_key), recipients, message) # type: ignore
99106

100107
# 本地配置发送
101108
message = await render_message(subject, settings.EMAIL_USERNAME, content, template)

0 commit comments

Comments
 (0)