feat: 国际化配置添加按钮级权限控制
This commit is contained in:
104
api/i18n.py
104
api/i18n.py
@@ -12,24 +12,27 @@ from fastapi import APIRouter, Depends, Path, Request, Query
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from annotation.auth import Auth
|
||||
from annotation.log import Log
|
||||
from config.constant import BusinessType, RedisKeyConfig
|
||||
from controller.login import LoginController
|
||||
from models import I18n, Locale
|
||||
from schemas.common import BaseResponse
|
||||
from schemas.common import BaseResponse, DeleteListParams
|
||||
from schemas.i18n import AddLocaleParams, GetLocaleInfoResponse, AddI18nParams, GetI18nInfoResponse, \
|
||||
GetI18nInfoListResponse, GetI18nListResponse
|
||||
from utils.response import Response
|
||||
|
||||
i18nAPI = APIRouter(
|
||||
prefix="/i18n",
|
||||
dependencies=[Depends(LoginController.get_current_user)]
|
||||
)
|
||||
|
||||
|
||||
@i18nAPI.post("/addLocale", response_class=JSONResponse, response_model=BaseResponse, summary="添加国际化类型")
|
||||
@Log(title="添加国际化类型", business_type=BusinessType.INSERT)
|
||||
async def add_locale(request: Request, params: AddLocaleParams, current_user=Depends(LoginController.get_current_user)):
|
||||
if await Locale.get_or_none(code=params.code):
|
||||
@Auth(["locale:btn:add"])
|
||||
async def add_locale(request: Request, params: AddLocaleParams):
|
||||
if await Locale.get_or_none(code=params.code, del_flag=1):
|
||||
return Response.error(msg="该语言代码已存在!")
|
||||
locale = await Locale.create(
|
||||
code=params.code,
|
||||
@@ -46,23 +49,41 @@ async def add_locale(request: Request, params: AddLocaleParams, current_user=Dep
|
||||
@i18nAPI.post("/deleteLocale/{id}", response_class=JSONResponse, response_model=BaseResponse,
|
||||
summary="删除国际化类型")
|
||||
@Log(title="删除国际化类型", business_type=BusinessType.DELETE)
|
||||
async def delete_locale(request: Request, id: str = Path(description="国际化类型ID"),
|
||||
current_user=Depends(LoginController.get_current_user)):
|
||||
if locale := await Locale.get_or_none(id=id):
|
||||
@Auth(["locale:btn:delete"])
|
||||
async def delete_locale(request: Request, id: str = Path(description="国际化类型ID")):
|
||||
if locale := await Locale.get_or_none(id=id, del_flag=1):
|
||||
# 移除语言
|
||||
await I18n.filter(locale_id=locale.id).delete()
|
||||
await locale.delete()
|
||||
await I18n.filter(locale_id=locale.id, del_flag=1).update(del_flag=0)
|
||||
locale.del_flag = 0
|
||||
await locale.save()
|
||||
return Response.success(msg="删除成功!")
|
||||
else:
|
||||
return Response.error(msg="该国际化类型不存在!")
|
||||
|
||||
|
||||
@i18nAPI.delete("/deleteLocaleList", response_class=JSONResponse, response_model=BaseResponse,
|
||||
summary="批量删除国际化类型")
|
||||
@i18nAPI.post("/deleteLocaleList", response_class=JSONResponse, response_model=BaseResponse,
|
||||
summary="批量删除国际化类型")
|
||||
@Log(title="批量删除国际化类型", business_type=BusinessType.DELETE)
|
||||
@Auth(["locale:btn:delete"])
|
||||
async def delete_locale_list(request: Request, params: DeleteListParams):
|
||||
for id in set(params.ids):
|
||||
if locale := await Locale.get_or_none(id=id, del_flag=1):
|
||||
# 移除语言
|
||||
await I18n.filter(locale_id=locale.id, del_flag=1).update(del_flag=0)
|
||||
locale.del_flag = 0
|
||||
await locale.save()
|
||||
return Response.success(msg="删除成功!")
|
||||
|
||||
|
||||
@i18nAPI.put("/updateLocale/{id}", response_class=JSONResponse, response_model=BaseResponse, summary="修改国际化类型")
|
||||
@i18nAPI.post("/updateLocale/{id}", response_class=JSONResponse, response_model=BaseResponse, summary="修改国际化类型")
|
||||
@Log(title="修改国际化类型", business_type=BusinessType.UPDATE)
|
||||
@Auth(["locale:btn:update"])
|
||||
async def update_locale(request: Request, params: AddLocaleParams, id: str = Path(description="国际化类型ID")):
|
||||
if locale := await Locale.get_or_none(id=id):
|
||||
if await Locale.get_or_none(code=params.code, name=params.name, id=id):
|
||||
if locale := await Locale.get_or_none(id=id, del_flag=1):
|
||||
if await Locale.get_or_none(code=params.code, name=params.name, id=id, del_flag=1):
|
||||
return Response.error(msg="该国际化类型已存在!")
|
||||
locale.code = params.code
|
||||
locale.name = params.name
|
||||
@@ -75,8 +96,9 @@ async def update_locale(request: Request, params: AddLocaleParams, id: str = Pat
|
||||
@i18nAPI.get("/locale/info/{id}", response_class=JSONResponse, response_model=GetLocaleInfoResponse,
|
||||
summary="获取国际化类型信息")
|
||||
@Log(title="获取国际化类型信息", business_type=BusinessType.SELECT)
|
||||
@Auth(["locale:btn:info"])
|
||||
async def get_locale_info(request: Request, id: str = Path(description="国际化类型ID")):
|
||||
if locale := await Locale.get_or_none(id=id):
|
||||
if locale := await Locale.get_or_none(id=id, del_flag=1):
|
||||
locale = {
|
||||
"id": locale.id,
|
||||
"code": locale.code,
|
||||
@@ -92,7 +114,8 @@ async def get_locale_info(request: Request, id: str = Path(description="国际
|
||||
|
||||
|
||||
@i18nAPI.get("/locale/list", response_class=JSONResponse, response_model=BaseResponse, summary="获取国际化类型列表")
|
||||
# @Log(title="获取国际化类型列表", business_type=BusinessType.SELECT)
|
||||
@Log(title="获取国际化类型列表", business_type=BusinessType.SELECT)
|
||||
@Auth(["locale:btn:list"])
|
||||
async def get_locale_list(request: Request,
|
||||
page: int = Query(default=1, description="当前页码"),
|
||||
pageSize: int = Query(default=50, description="每页条数"),
|
||||
@@ -105,8 +128,9 @@ async def get_locale_list(request: Request,
|
||||
'code': code
|
||||
}.items() if v
|
||||
}
|
||||
total = await Locale.filter(**filterArgs).count()
|
||||
data = await Locale.filter(**filterArgs).offset((page - 1) * pageSize).limit(pageSize).distinct().values(
|
||||
total = await Locale.filter(**filterArgs, del_flag=1).count()
|
||||
data = await Locale.filter(**filterArgs, del_flag=1).offset((page - 1) * pageSize).limit(
|
||||
pageSize).distinct().values(
|
||||
id="id",
|
||||
code="code",
|
||||
name="name",
|
||||
@@ -125,10 +149,11 @@ async def get_locale_list(request: Request,
|
||||
|
||||
@i18nAPI.post("/addI18n", response_class=JSONResponse, response_model=BaseResponse, summary="添加国际化内容")
|
||||
@Log(title="添加国际化内容", business_type=BusinessType.INSERT)
|
||||
async def add_i18n(request: Request, params: AddI18nParams, current_user=Depends(LoginController.get_current_user)):
|
||||
if await I18n.get_or_none(key=params.key, locale_id=params.locale_id):
|
||||
@Auth(["i18n:btn:add"])
|
||||
async def add_i18n(request: Request, params: AddI18nParams):
|
||||
if await I18n.get_or_none(key=params.key, locale_id=params.locale_id, del_flag=1):
|
||||
return Response.error(msg="该国际化内容已存在!")
|
||||
locale = await Locale.get_or_none(id=params.locale_id)
|
||||
locale = await Locale.get_or_none(id=params.locale_id, del_flag=1)
|
||||
i18n = await I18n.create(
|
||||
key=params.key,
|
||||
translation=params.translation,
|
||||
@@ -145,22 +170,36 @@ async def add_i18n(request: Request, params: AddI18nParams, current_user=Depends
|
||||
@i18nAPI.post("/deleteI18n/{id}", response_class=JSONResponse, response_model=BaseResponse,
|
||||
summary="删除国际化内容")
|
||||
@Log(title="删除国际化内容", business_type=BusinessType.DELETE)
|
||||
async def delete_i18n(request: Request, id: str = Path(description="国际化内容ID"),
|
||||
current_user=Depends(LoginController.get_current_user)):
|
||||
if i18n := await I18n.get_or_none(id=id):
|
||||
await i18n.delete()
|
||||
@Auth(["i18n:btn:delete"])
|
||||
async def delete_i18n(request: Request, id: str = Path(description="国际化内容ID")):
|
||||
if i18n := await I18n.get_or_none(id=id, del_flag=1):
|
||||
i18n.del_flag = 0
|
||||
await i18n.save()
|
||||
return Response.success(msg="删除成功!")
|
||||
else:
|
||||
return Response.error(msg="该国际化内容不存在!")
|
||||
|
||||
|
||||
@i18nAPI.delete("/deleteI18nList", response_class=JSONResponse, response_model=BaseResponse,
|
||||
summary="批量删除国际化内容")
|
||||
@i18nAPI.post("/deleteI18nList", response_class=JSONResponse, response_model=BaseResponse, summary="批量删除国际化内容")
|
||||
@Log(title="批量删除国际化内容", business_type=BusinessType.DELETE)
|
||||
@Auth(["i18n:btn:delete"])
|
||||
async def delete_i18n_list(request: Request, params: DeleteListParams):
|
||||
for id in set(params.ids):
|
||||
if i18n := await I18n.get_or_none(id=id, del_flag=1):
|
||||
i18n.del_flag = 0
|
||||
await i18n.save()
|
||||
return Response.success(msg="删除成功!")
|
||||
|
||||
|
||||
@i18nAPI.put("/updateI18n/{id}", response_class=JSONResponse, response_model=BaseResponse, summary="修改国际化内容")
|
||||
@i18nAPI.post("/updateI18n/{id}", response_class=JSONResponse, response_model=BaseResponse, summary="修改国际化内容")
|
||||
@Log(title="修改国际化内容", business_type=BusinessType.UPDATE)
|
||||
async def update_i18n(request: Request, params: AddI18nParams, id: str = Path(description="国际化内容ID"),
|
||||
current_user=Depends(LoginController.get_current_user)):
|
||||
if i18n := await I18n.get_or_none(id=id):
|
||||
locale = await Locale.get_or_none(id=params.locale_id)
|
||||
@Auth(["i18n:btn:update"])
|
||||
async def update_i18n(request: Request, params: AddI18nParams, id: str = Path(description="国际化内容ID")):
|
||||
if i18n := await I18n.get_or_none(id=id, del_flag=1):
|
||||
locale = await Locale.get_or_none(id=params.locale_id, del_flag=1)
|
||||
i18n.key = params.key
|
||||
i18n.translation = params.translation
|
||||
i18n.locale = locale
|
||||
@@ -173,8 +212,9 @@ async def update_i18n(request: Request, params: AddI18nParams, id: str = Path(de
|
||||
@i18nAPI.get("/info/{id}", response_class=JSONResponse, response_model=GetI18nInfoResponse,
|
||||
summary="获取国际化内容信息")
|
||||
@Log(title="获取国际化内容信息", business_type=BusinessType.SELECT)
|
||||
@Auth(["i18n:btn:info"])
|
||||
async def get_i18n_info(request: Request, id: str = Path(description="国际化内容ID")):
|
||||
if i18n := await I18n.get_or_none(id=id):
|
||||
if i18n := await I18n.get_or_none(id=id, del_flag=1):
|
||||
i18n = {
|
||||
"id": i18n.id,
|
||||
"key": i18n.key,
|
||||
@@ -193,6 +233,7 @@ async def get_i18n_info(request: Request, id: str = Path(description="国际化
|
||||
|
||||
@i18nAPI.get("/list", response_class=JSONResponse, response_model=GetI18nListResponse, summary="获取国际化内容列表")
|
||||
@Log(title="获取国际化内容列表", business_type=BusinessType.SELECT)
|
||||
@Auth(["i18n:btn:list"])
|
||||
async def get_i18n_list(request: Request,
|
||||
page: int = Query(default=1, description="当前页码"),
|
||||
pageSize: int = Query(default=50, description="每页条数"),
|
||||
@@ -207,8 +248,8 @@ async def get_i18n_list(request: Request,
|
||||
'translation': translation
|
||||
}.items() if v
|
||||
}
|
||||
total = await I18n.filter(**filterArgs).count()
|
||||
data = await I18n.filter(**filterArgs).offset((page - 1) * pageSize).limit(pageSize).values(
|
||||
total = await I18n.filter(**filterArgs, del_flag=1).count()
|
||||
data = await I18n.filter(**filterArgs, del_flag=1).offset((page - 1) * pageSize).limit(pageSize).values(
|
||||
id="id",
|
||||
key="key",
|
||||
translation="translation",
|
||||
@@ -229,14 +270,15 @@ async def get_i18n_list(request: Request,
|
||||
|
||||
@i18nAPI.get("/infoList/{id}", response_class=JSONResponse, response_model=GetI18nInfoListResponse,
|
||||
summary="获取国际化列表")
|
||||
# @Log(title="获取国际化列表", business_type=BusinessType.SELECT)
|
||||
@Log(title="获取国际化列表", business_type=BusinessType.SELECT)
|
||||
@Auth(["i18n:btn:infoList"])
|
||||
async def get_i18n_info_list(request: Request, id: str = Path(description="国际化内容语言ID")):
|
||||
if locale := await Locale.get_or_none(id=id):
|
||||
if locale := await Locale.get_or_none(id=id, del_flag=1):
|
||||
result = await request.app.state.redis.get(f'{RedisKeyConfig.TRANSLATION_INFO.key}:{id}')
|
||||
if result:
|
||||
result = eval(result)
|
||||
return Response.success(data=result)
|
||||
data = await I18n.filter(locale_id=locale.id).values(
|
||||
data = await I18n.filter(locale_id=locale.id, del_flag=1).values(
|
||||
id="id",
|
||||
key="key",
|
||||
translation="translation",
|
||||
|
||||
Reference in New Issue
Block a user