mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Update system config to dynamic (#447)
* Update system config to be dynamic * update the configuration interface * custom parameters are allowed * update apis * add more code * finish * fix bugs
This commit is contained in:
@@ -18,8 +18,8 @@ router.include_router(api_router, prefix='/apis', tags=['系统API'])
|
|||||||
router.include_router(casbin_router, prefix='/casbin', tags=['系统Casbin权限'])
|
router.include_router(casbin_router, prefix='/casbin', tags=['系统Casbin权限'])
|
||||||
router.include_router(config_router, prefix='/configs', tags=['系统配置'])
|
router.include_router(config_router, prefix='/configs', tags=['系统配置'])
|
||||||
router.include_router(dept_router, prefix='/depts', tags=['系统部门'])
|
router.include_router(dept_router, prefix='/depts', tags=['系统部门'])
|
||||||
router.include_router(dict_data_router, prefix='/dict_datas', tags=['系统字典数据'])
|
router.include_router(dict_data_router, prefix='/dict-datas', tags=['系统字典数据'])
|
||||||
router.include_router(dict_type_router, prefix='/dict_types', tags=['系统字典类型'])
|
router.include_router(dict_type_router, prefix='/dict-types', tags=['系统字典类型'])
|
||||||
router.include_router(menu_router, prefix='/menus', tags=['系统目录'])
|
router.include_router(menu_router, prefix='/menus', tags=['系统目录'])
|
||||||
router.include_router(role_router, prefix='/roles', tags=['系统角色'])
|
router.include_router(role_router, prefix='/roles', tags=['系统角色'])
|
||||||
router.include_router(user_router, prefix='/users', tags=['系统用户'])
|
router.include_router(user_router, prefix='/users', tags=['系统用户'])
|
||||||
|
|||||||
@@ -4,44 +4,126 @@ from typing import Annotated
|
|||||||
|
|
||||||
from fastapi import APIRouter, Depends, Path, Query
|
from fastapi import APIRouter, Depends, Path, Query
|
||||||
|
|
||||||
from backend.app.admin.schema.config import CreateConfigParam, UpdateConfigParam
|
from backend.app.admin.schema.config import (
|
||||||
|
CreateAnyConfigParam,
|
||||||
|
GetAnyConfigListDetails,
|
||||||
|
SaveConfigParam,
|
||||||
|
UpdateAnyConfigParam,
|
||||||
|
)
|
||||||
from backend.app.admin.service.config_service import config_service
|
from backend.app.admin.service.config_service import config_service
|
||||||
|
from backend.common.pagination import DependsPagination, paging_data
|
||||||
from backend.common.response.response_schema import ResponseModel, response_base
|
from backend.common.response.response_schema import ResponseModel, response_base
|
||||||
from backend.common.security.jwt import DependsJwtAuth
|
from backend.common.security.jwt import DependsJwtAuth
|
||||||
from backend.common.security.permission import RequestPermission
|
from backend.common.security.permission import RequestPermission
|
||||||
from backend.common.security.rbac import DependsRBAC
|
from backend.common.security.rbac import DependsRBAC
|
||||||
|
from backend.database.db_mysql import CurrentSession
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.get('', summary='获取系统配置详情', dependencies=[DependsJwtAuth])
|
@router.get('/website', summary='获取网站配置信息', dependencies=[DependsJwtAuth])
|
||||||
async def get_config() -> ResponseModel:
|
async def get_website_config() -> ResponseModel:
|
||||||
config = await config_service.get()
|
config = await config_service.get_built_in_config('website')
|
||||||
return response_base.success(data=config)
|
return response_base.success(data=config)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
|
'/website',
|
||||||
|
summary='保存网站配置信息',
|
||||||
|
dependencies=[
|
||||||
|
Depends(RequestPermission('sys:config:website:add')),
|
||||||
|
DependsRBAC,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def save_website_config(objs: list[SaveConfigParam]) -> ResponseModel:
|
||||||
|
await config_service.save_built_in_config(objs, 'website')
|
||||||
|
return response_base.success()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/protocol', summary='获取用户协议', dependencies=[DependsJwtAuth])
|
||||||
|
async def get_protocol_config() -> ResponseModel:
|
||||||
|
config = await config_service.get_built_in_config('protocol')
|
||||||
|
return response_base.success(data=config)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
'/protocol',
|
||||||
|
summary='保存用户协议',
|
||||||
|
dependencies=[
|
||||||
|
Depends(RequestPermission('sys:config:protocol:add')),
|
||||||
|
DependsRBAC,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def save_protocol_config(objs: list[SaveConfigParam]) -> ResponseModel:
|
||||||
|
await config_service.save_built_in_config(objs, 'protocol')
|
||||||
|
return response_base.success()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/policy', summary='获取用户政策', dependencies=[DependsJwtAuth])
|
||||||
|
async def get_policy_config() -> ResponseModel:
|
||||||
|
config = await config_service.get_built_in_config('policy')
|
||||||
|
return response_base.success(data=config)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
'/policy',
|
||||||
|
summary='保存用户政策',
|
||||||
|
dependencies=[
|
||||||
|
Depends(RequestPermission('sys:config:policy:add')),
|
||||||
|
DependsRBAC,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def save_policy_config(objs: list[SaveConfigParam]) -> ResponseModel:
|
||||||
|
await config_service.save_built_in_config(objs, 'policy')
|
||||||
|
return response_base.success()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/{pk}', summary='获取系统参数配置详情', dependencies=[DependsJwtAuth])
|
||||||
|
async def get_config(pk: Annotated[int, Path(...)]) -> ResponseModel:
|
||||||
|
config = await config_service.get(pk)
|
||||||
|
return response_base.success(data=config)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
'',
|
'',
|
||||||
summary='创建系统配置',
|
summary='(模糊条件)分页获取所有系统参数配置',
|
||||||
|
dependencies=[
|
||||||
|
DependsJwtAuth,
|
||||||
|
DependsPagination,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def get_pagination_config(
|
||||||
|
db: CurrentSession,
|
||||||
|
name: Annotated[str | None, Query()] = None,
|
||||||
|
type: Annotated[str | None, Query()] = None,
|
||||||
|
) -> ResponseModel:
|
||||||
|
config_select = await config_service.get_select(name=name, type=type)
|
||||||
|
page_data = await paging_data(db, config_select, GetAnyConfigListDetails)
|
||||||
|
return response_base.success(data=page_data)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
'',
|
||||||
|
summary='创建系统参数配置',
|
||||||
dependencies=[
|
dependencies=[
|
||||||
Depends(RequestPermission('sys:config:add')),
|
Depends(RequestPermission('sys:config:add')),
|
||||||
DependsRBAC,
|
DependsRBAC,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def create_config(obj: CreateConfigParam) -> ResponseModel:
|
async def create_config(obj: CreateAnyConfigParam) -> ResponseModel:
|
||||||
await config_service.create(obj=obj)
|
await config_service.create(obj=obj)
|
||||||
return response_base.success()
|
return response_base.success()
|
||||||
|
|
||||||
|
|
||||||
@router.put(
|
@router.put(
|
||||||
'/{pk}',
|
'/{pk}',
|
||||||
summary='更新系统配置',
|
summary='更新系统参数配置',
|
||||||
dependencies=[
|
dependencies=[
|
||||||
Depends(RequestPermission('sys:config:edit')),
|
Depends(RequestPermission('sys:config:edit')),
|
||||||
DependsRBAC,
|
DependsRBAC,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def update_config(pk: Annotated[int, Path(...)], obj: UpdateConfigParam) -> ResponseModel:
|
async def update_config(pk: Annotated[int, Path(...)], obj: UpdateAnyConfigParam) -> ResponseModel:
|
||||||
count = await config_service.update(pk=pk, obj=obj)
|
count = await config_service.update(pk=pk, obj=obj)
|
||||||
if count > 0:
|
if count > 0:
|
||||||
return response_base.success()
|
return response_base.success()
|
||||||
@@ -50,7 +132,7 @@ async def update_config(pk: Annotated[int, Path(...)], obj: UpdateConfigParam) -
|
|||||||
|
|
||||||
@router.delete(
|
@router.delete(
|
||||||
'',
|
'',
|
||||||
summary='(批量)删除系统配置',
|
summary='(批量)删除系统参数配置',
|
||||||
dependencies=[
|
dependencies=[
|
||||||
Depends(RequestPermission('sys:config:del')),
|
Depends(RequestPermission('sys:config:del')),
|
||||||
DependsRBAC,
|
DependsRBAC,
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class AdminSettings(BaseSettings):
|
|||||||
CAPTCHA_LOGIN_EXPIRE_SECONDS: int = 60 * 5 # 过期时间,单位:秒
|
CAPTCHA_LOGIN_EXPIRE_SECONDS: int = 60 * 5 # 过期时间,单位:秒
|
||||||
|
|
||||||
# Config
|
# Config
|
||||||
CONFIG_REDIS_KEY: str = 'fba:config'
|
CONFIG_BUILT_IN_TYPES: list = ['website', 'protocol', 'policy']
|
||||||
|
|
||||||
|
|
||||||
@lru_cache
|
@lru_cache
|
||||||
|
|||||||
@@ -2,35 +2,77 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
|
|
||||||
from sqlalchemy import select
|
from sqlalchemy import Select
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy_crud_plus import CRUDPlus
|
from sqlalchemy_crud_plus import CRUDPlus
|
||||||
|
|
||||||
|
from backend.app.admin.conf import admin_settings
|
||||||
from backend.app.admin.model import Config
|
from backend.app.admin.model import Config
|
||||||
from backend.app.admin.schema.config import CreateConfigParam, UpdateConfigParam
|
from backend.app.admin.schema.config import CreateAnyConfigParam, UpdateAnyConfigParam
|
||||||
|
|
||||||
|
|
||||||
class CRUDConfig(CRUDPlus[Config]):
|
class CRUDConfig(CRUDPlus[Config]):
|
||||||
async def get_one(self, db: AsyncSession) -> Config | None:
|
async def get(self, db: AsyncSession, pk: int) -> Config | None:
|
||||||
"""
|
"""
|
||||||
获取 Config
|
获取系统参数配置
|
||||||
|
|
||||||
:param db:
|
:param db:
|
||||||
|
:param pk:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
query = await db.execute(select(self.model).limit(1))
|
return await self.select_model_by_column(db, id=pk, type__not_in=admin_settings.CONFIG_BUILT_IN_TYPES)
|
||||||
return query.scalars().first()
|
|
||||||
|
|
||||||
async def get_all(self, db: AsyncSession) -> Sequence[Config]:
|
async def get_by_type(self, db: AsyncSession, type: str) -> Sequence[Config]:
|
||||||
"""
|
"""
|
||||||
获取所有 Config
|
通过 type 获取内置系统配置
|
||||||
|
|
||||||
:param db:
|
:param db:
|
||||||
|
:param type:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return await self.select_models(db)
|
return await self.select_models(db, type=type)
|
||||||
|
|
||||||
async def create(self, db: AsyncSession, obj_in: CreateConfigParam) -> None:
|
async def get_by_key_and_type(self, db: AsyncSession, key: str, type: str) -> Config | None:
|
||||||
|
"""
|
||||||
|
通过 name 和 type 获取内置系统配置
|
||||||
|
|
||||||
|
:param db:
|
||||||
|
:param key:
|
||||||
|
:param type:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return await self.select_model_by_column(db, key=key, type=type)
|
||||||
|
|
||||||
|
async def get_by_key(self, db: AsyncSession, key: str, built_in: bool = False) -> Config | None:
|
||||||
|
"""
|
||||||
|
通过 key 获取系统配置参数
|
||||||
|
|
||||||
|
:param db:
|
||||||
|
:param key:
|
||||||
|
:param built_in:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
filters = {'key': key}
|
||||||
|
if not built_in:
|
||||||
|
filters.update({'type__not_in': admin_settings.CONFIG_BUILT_IN_TYPES})
|
||||||
|
return await self.select_model_by_column(db, **filters)
|
||||||
|
|
||||||
|
async def get_list(self, name: str = None, type: str = None) -> Select:
|
||||||
|
"""
|
||||||
|
获取系统参数配置列表
|
||||||
|
|
||||||
|
:param name:
|
||||||
|
:param type:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
filters = {'type__not_in': admin_settings.CONFIG_BUILT_IN_TYPES}
|
||||||
|
if name is not None:
|
||||||
|
filters.update(name__like=f'%{name}%')
|
||||||
|
if type is not None:
|
||||||
|
filters.update(type__like=f'%{type}%')
|
||||||
|
return await self.select_order('created_time', 'desc', **filters)
|
||||||
|
|
||||||
|
async def create(self, db: AsyncSession, obj_in: CreateAnyConfigParam) -> None:
|
||||||
"""
|
"""
|
||||||
创建 Config
|
创建 Config
|
||||||
|
|
||||||
@@ -40,7 +82,7 @@ class CRUDConfig(CRUDPlus[Config]):
|
|||||||
"""
|
"""
|
||||||
await self.create_model(db, obj_in)
|
await self.create_model(db, obj_in)
|
||||||
|
|
||||||
async def update(self, db: AsyncSession, pk: int, obj_in: UpdateConfigParam) -> int:
|
async def update(self, db: AsyncSession, pk: int, obj_in: UpdateAnyConfigParam) -> int:
|
||||||
"""
|
"""
|
||||||
更新 Config
|
更新 Config
|
||||||
|
|
||||||
@@ -59,7 +101,9 @@ class CRUDConfig(CRUDPlus[Config]):
|
|||||||
:param pk:
|
:param pk:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pk)
|
return await self.delete_model_by_column(
|
||||||
|
db, allow_multiple=True, id__in=pk, type__not_in=admin_settings.CONFIG_BUILT_IN_TYPES
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
config_dao: CRUDConfig = CRUDConfig(Config)
|
config_dao: CRUDConfig = CRUDConfig(Config)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from sqlalchemy import String
|
from sqlalchemy import Boolean, String
|
||||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||||
from sqlalchemy.orm import Mapped, mapped_column
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
@@ -13,16 +13,9 @@ class Config(Base):
|
|||||||
__tablename__ = 'sys_config'
|
__tablename__ = 'sys_config'
|
||||||
|
|
||||||
id: Mapped[id_key] = mapped_column(init=False)
|
id: Mapped[id_key] = mapped_column(init=False)
|
||||||
login_title: Mapped[str] = mapped_column(String(20), default='登录 FBA', comment='登录页面标题')
|
name: Mapped[str] = mapped_column(String(20), comment='名称')
|
||||||
login_sub_title: Mapped[str] = mapped_column(
|
type: Mapped[str | None] = mapped_column(String(20), server_default=None, comment='类型')
|
||||||
String(50), default='fastapi_best_architecture', comment='登录页面子标题'
|
key: Mapped[str] = mapped_column(String(50), unique=True, comment='键名')
|
||||||
)
|
value: Mapped[str] = mapped_column(LONGTEXT, comment='键值')
|
||||||
footer: Mapped[str] = mapped_column(String(50), default='FBA', comment='页脚标题')
|
is_frontend: Mapped[str] = mapped_column(Boolean, default=False, comment='是否前端')
|
||||||
logo: Mapped[str] = mapped_column(LONGTEXT, default='Arco', comment='Logo')
|
remark: Mapped[str | None] = mapped_column(LONGTEXT, default=None, comment='备注')
|
||||||
system_title: Mapped[str] = mapped_column(String(20), default='Arco', comment='系统标题')
|
|
||||||
system_comment: Mapped[str] = mapped_column(
|
|
||||||
LONGTEXT,
|
|
||||||
default='基于 FastAPI 构建的前后端分离 RBAC 权限控制系统,采用独特的伪三层架构模型设计,'
|
|
||||||
'内置 fastapi-admin 基本实现,并作为模板库免费开源',
|
|
||||||
comment='系统描述',
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -2,32 +2,35 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from pydantic import ConfigDict, Field
|
from pydantic import ConfigDict
|
||||||
|
|
||||||
from backend.common.schema import SchemaBase
|
from backend.common.schema import SchemaBase
|
||||||
|
|
||||||
|
|
||||||
class ConfigSchemaBase(SchemaBase):
|
class SaveConfigParam(SchemaBase):
|
||||||
login_title: str = Field(default='登陆 FBA')
|
name: str
|
||||||
login_sub_title: str = Field(default='fastapi_best_architecture')
|
key: str
|
||||||
footer: str = Field(default='FBA')
|
value: str
|
||||||
logo: str = Field(default='Arco')
|
|
||||||
system_title: str = Field(default='Arco')
|
|
||||||
system_comment: str = Field(
|
|
||||||
default='基于 FastAPI 构建的前后端分离 RBAC 权限控制系统,采用独特的伪三层架构模型设计,'
|
|
||||||
'内置 fastapi-admin 基本实现,并作为模板库免费开源'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class CreateConfigParam(ConfigSchemaBase):
|
class AnyConfigSchemaBase(SchemaBase):
|
||||||
|
name: str
|
||||||
|
type: str | None
|
||||||
|
key: str
|
||||||
|
value: str
|
||||||
|
is_frontend: bool
|
||||||
|
remark: str | None
|
||||||
|
|
||||||
|
|
||||||
|
class CreateAnyConfigParam(AnyConfigSchemaBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class UpdateConfigParam(ConfigSchemaBase):
|
class UpdateAnyConfigParam(AnyConfigSchemaBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class GetConfigListDetails(ConfigSchemaBase):
|
class GetAnyConfigListDetails(AnyConfigSchemaBase):
|
||||||
model_config = ConfigDict(from_attributes=True)
|
model_config = ConfigDict(from_attributes=True)
|
||||||
|
|
||||||
id: int
|
id: int
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ class ApiService:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
async def update(*, pk: int, obj: UpdateApiParam) -> int:
|
async def update(*, pk: int, obj: UpdateApiParam) -> int:
|
||||||
async with async_db_session.begin() as db:
|
async with async_db_session.begin() as db:
|
||||||
|
api = await api_dao.get(db, pk)
|
||||||
|
if not api:
|
||||||
|
raise errors.NotFoundError(msg='接口不存在')
|
||||||
count = await api_dao.update(db, pk, obj)
|
count = await api_dao.update(db, pk, obj)
|
||||||
return count
|
return count
|
||||||
|
|
||||||
|
|||||||
@@ -1,56 +1,74 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from typing import Sequence
|
||||||
|
|
||||||
|
from sqlalchemy import Select
|
||||||
|
|
||||||
from backend.app.admin.conf import admin_settings
|
from backend.app.admin.conf import admin_settings
|
||||||
from backend.app.admin.crud.crud_config import config_dao
|
from backend.app.admin.crud.crud_config import config_dao
|
||||||
from backend.app.admin.model import Config
|
from backend.app.admin.model import Config
|
||||||
from backend.app.admin.schema.config import CreateConfigParam, UpdateConfigParam
|
from backend.app.admin.schema.config import (
|
||||||
|
CreateAnyConfigParam,
|
||||||
|
SaveConfigParam,
|
||||||
|
UpdateAnyConfigParam,
|
||||||
|
)
|
||||||
from backend.common.exception import errors
|
from backend.common.exception import errors
|
||||||
from backend.database.db_mysql import async_db_session
|
from backend.database.db_mysql import async_db_session
|
||||||
from backend.database.db_redis import redis_client
|
|
||||||
from backend.utils.serializers import select_as_dict
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigService:
|
class ConfigService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get() -> Config | dict:
|
async def get_built_in_config(type: str) -> Sequence[Config]:
|
||||||
async with async_db_session() as db:
|
async with async_db_session() as db:
|
||||||
cache_config = await redis_client.hgetall(admin_settings.CONFIG_REDIS_KEY)
|
return await config_dao.get_by_type(db, type)
|
||||||
if not cache_config:
|
|
||||||
config = await config_dao.get_one(db)
|
|
||||||
if not config:
|
|
||||||
raise errors.NotFoundError(msg='系统配置不存在')
|
|
||||||
data_map = select_as_dict(config)
|
|
||||||
del data_map['created_time']
|
|
||||||
del data_map['updated_time']
|
|
||||||
await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=data_map)
|
|
||||||
return config
|
|
||||||
else:
|
|
||||||
return cache_config
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def create(*, obj: CreateConfigParam) -> None:
|
async def save_built_in_config(objs: list[SaveConfigParam], type: str) -> None:
|
||||||
async with async_db_session.begin() as db:
|
async with async_db_session.begin() as db:
|
||||||
config = await config_dao.get_one(db)
|
for obj in objs:
|
||||||
|
config = await config_dao.get_by_key_and_type(db, obj.key, type)
|
||||||
|
if config is None:
|
||||||
|
if await config_dao.get_by_key(db, obj.key, built_in=True):
|
||||||
|
raise errors.ForbiddenError(msg=f'参数配置 {obj.key} 已存在')
|
||||||
|
await config_dao.create_model(db, obj, flush=True, type=type)
|
||||||
|
else:
|
||||||
|
await config_dao.update_model(db, config.id, obj, type=type)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def get(pk) -> Config | dict:
|
||||||
|
async with async_db_session() as db:
|
||||||
|
config = await config_dao.get(db, pk)
|
||||||
|
if not config:
|
||||||
|
raise errors.NotFoundError(msg='参数配置不存在')
|
||||||
|
return config
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def get_select(*, name: str = None, type: str = None) -> Select:
|
||||||
|
return await config_dao.get_list(name=name, type=type)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def create(*, obj: CreateAnyConfigParam) -> None:
|
||||||
|
async with async_db_session.begin() as db:
|
||||||
|
if obj.type in admin_settings.CONFIG_BUILT_IN_TYPES:
|
||||||
|
raise errors.ForbiddenError(msg='非法类型参数')
|
||||||
|
config = await config_dao.get_by_key(db, obj.key)
|
||||||
if config:
|
if config:
|
||||||
raise errors.ForbiddenError(msg='系统配置已存在')
|
raise errors.ForbiddenError(msg=f'参数配置 {obj.key} 已存在')
|
||||||
await config_dao.create(db, obj)
|
await config_dao.create(db, obj)
|
||||||
await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=obj.model_dump())
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def update(*, pk: int, obj: UpdateConfigParam) -> int:
|
async def update(*, pk: int, obj: UpdateAnyConfigParam) -> int:
|
||||||
async with async_db_session.begin() as db:
|
async with async_db_session.begin() as db:
|
||||||
|
config = await config_dao.get(db, pk)
|
||||||
|
if not config:
|
||||||
|
raise errors.NotFoundError(msg='参数配置不存在')
|
||||||
count = await config_dao.update(db, pk, obj)
|
count = await config_dao.update(db, pk, obj)
|
||||||
await redis_client.hset(admin_settings.CONFIG_REDIS_KEY, mapping=obj.model_dump())
|
|
||||||
return count
|
return count
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def delete(*, pk: list[int]) -> int:
|
async def delete(*, pk: list[int]) -> int:
|
||||||
async with async_db_session.begin() as db:
|
async with async_db_session.begin() as db:
|
||||||
configs = await config_dao.get_all(db)
|
|
||||||
if len(configs) == 1:
|
|
||||||
raise errors.ForbiddenError(msg='系统配置无法彻底删除')
|
|
||||||
count = await config_dao.delete(db, pk)
|
count = await config_dao.delete(db, pk)
|
||||||
await redis_client.delete(admin_settings.CONFIG_REDIS_KEY)
|
|
||||||
return count
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ dependencies = [
|
|||||||
"XdbSearchIP==1.0.2",
|
"XdbSearchIP==1.0.2",
|
||||||
"fastapi_oauth20>=0.0.1a2",
|
"fastapi_oauth20>=0.0.1a2",
|
||||||
"flower==2.0.1",
|
"flower==2.0.1",
|
||||||
"sqlalchemy-crud-plus==1.5.0",
|
"sqlalchemy-crud-plus==1.6.0",
|
||||||
"jinja2==3.1.4",
|
"jinja2==3.1.4",
|
||||||
"aiofiles==24.1.0",
|
"aiofiles==24.1.0",
|
||||||
# When celery version < 6.0.0
|
# When celery version < 6.0.0
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ simpleeval==1.0.0
|
|||||||
six==1.16.0
|
six==1.16.0
|
||||||
sniffio==1.3.1
|
sniffio==1.3.1
|
||||||
sqlalchemy==2.0.30
|
sqlalchemy==2.0.30
|
||||||
sqlalchemy-crud-plus==1.5.0
|
sqlalchemy-crud-plus==1.6.0
|
||||||
starlette==0.37.2
|
starlette==0.37.2
|
||||||
tomli==2.0.2 ; python_full_version < '3.11'
|
tomli==2.0.2 ; python_full_version < '3.11'
|
||||||
tornado==6.4.1
|
tornado==6.4.1
|
||||||
|
|||||||
Generated
+4
-4
@@ -561,7 +561,7 @@ requires-dist = [
|
|||||||
{ name = "python-socketio", extras = ["asyncio"], specifier = ">=5.11.4" },
|
{ name = "python-socketio", extras = ["asyncio"], specifier = ">=5.11.4" },
|
||||||
{ name = "redis", extras = ["hiredis"], specifier = "==5.1.0" },
|
{ name = "redis", extras = ["hiredis"], specifier = "==5.1.0" },
|
||||||
{ name = "sqlalchemy", specifier = "==2.0.30" },
|
{ name = "sqlalchemy", specifier = "==2.0.30" },
|
||||||
{ name = "sqlalchemy-crud-plus", specifier = "==1.5.0" },
|
{ name = "sqlalchemy-crud-plus", specifier = "==1.6.0" },
|
||||||
{ name = "user-agents", specifier = "==2.2.0" },
|
{ name = "user-agents", specifier = "==2.2.0" },
|
||||||
{ name = "uvicorn", extras = ["standard"], specifier = "==0.29.0" },
|
{ name = "uvicorn", extras = ["standard"], specifier = "==0.29.0" },
|
||||||
{ name = "xdbsearchip", specifier = "==1.0.2" },
|
{ name = "xdbsearchip", specifier = "==1.0.2" },
|
||||||
@@ -1641,15 +1641,15 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sqlalchemy-crud-plus"
|
name = "sqlalchemy-crud-plus"
|
||||||
version = "1.5.0"
|
version = "1.6.0"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "pydantic" },
|
{ name = "pydantic" },
|
||||||
{ name = "sqlalchemy" },
|
{ name = "sqlalchemy" },
|
||||||
]
|
]
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/1b/33/473fae4602b7f124b4b2052165bfb6088e8c3be23197ddc3bd7a054ae7e6/sqlalchemy_crud_plus-1.5.0.tar.gz", hash = "sha256:3d4cbdf2bd7638115a48536d11a0f7abfd1ebb64bb86036297239c0ca224c377", size = 41040 }
|
sdist = { url = "https://files.pythonhosted.org/packages/37/6a/99d1908c96ba13da4941e7fa6e254220a14332a2dce244fa423487ea7759/sqlalchemy_crud_plus-1.6.0.tar.gz", hash = "sha256:a09a56c4a9dd909800b4be5868a72b985b1cffd548aa734e3e2199c3395d2a55", size = 41713 }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/46/28/c0023025415d27c9a703ff6de3a116bfc57f2a94600138a540ed959acdfa/sqlalchemy_crud_plus-1.5.0-py3-none-any.whl", hash = "sha256:adec688680e90c83ff789286632b6d74c1934f494af68f3370feb0ddd622d7f4", size = 8325 },
|
{ url = "https://files.pythonhosted.org/packages/d0/e9/0996d7e9be20473f4a80a1f8a5d679bfcbc2e5a567b2f0ca488ccaa2b475/sqlalchemy_crud_plus-1.6.0-py3-none-any.whl", hash = "sha256:b2c77957716023f1ab4beac27588c448b9f9fba12657aa4013b1946d26fffd84", size = 8380 },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
Reference in New Issue
Block a user