mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-22 21:06:32 +00:00
- 将 ApplicationQueryParams 改为 ApplicationQueryParam - 同步更新相关导入和函数参数类型注解 - 修改 PaginationQueryParams 为 PaginationQueryParam refactor(demo): 重命名查询参数类以统一命名风格 - 将 DemoQueryParams 改为 DemoQueryParam - 同步更新相关导入和函数参数类型注解 - 修改 PaginationQueryParams 为 PaginationQueryParam refactor(gencode): 优化代码生成模块的模型和服务层结构 - 统一模型名称后缀为 Schema,调整相关引用 - 规范 Pydantic schema 的命名和定义 - 删除无用的 Python DAO 模板文件 - 调整导入路径,统一使用 app 目录下的模块路径 - 改进服务层方法签名,添加返回类型注解 - 使用自定义异常 CustomException 替代旧异常 - 统一成功响应格式为 SuccessResponse - 优化代码生成服务中的数据库操作 DAO 调用参数传递 - 优化代码生成业务表和字段模型的字段定义,添加注释和默认值 - 优化生成代码路径处理逻辑和异常信息提示 - 整合分页查询参数定义,统一分页模型 - 修正多个服务方法的参数类型和返回类型 - 删除无用的导入和多余注释,提升代码整洁度
225 lines
7.6 KiB
Python
225 lines
7.6 KiB
Python
import re
|
|
from datetime import datetime
|
|
from typing import List
|
|
|
|
from app.common.constant import GenConstant
|
|
from app.config.setting import settings
|
|
from app.api.v1.module_generator.gencode.schema import GenTableColumnSchema, GenTableSchema
|
|
from .string_util import StringUtil
|
|
|
|
|
|
class GenUtils:
|
|
"""代码生成器工具类"""
|
|
|
|
@classmethod
|
|
def init_table(cls, gen_table: GenTableSchema, oper_name: str) -> None:
|
|
"""
|
|
初始化表信息
|
|
|
|
param gen_table: 业务表对象
|
|
param oper_name: 操作人
|
|
:return:
|
|
"""
|
|
gen_table.class_name = cls.convert_class_name(gen_table.table_name)
|
|
gen_table.package_name = settings.package_name
|
|
gen_table.module_name = cls.get_module_name(settings.package_name)
|
|
gen_table.business_name = cls.get_business_name(gen_table.table_name)
|
|
gen_table.function_name = cls.replace_text(gen_table.table_comment)
|
|
gen_table.function_author = settings.author
|
|
gen_table.create_by = oper_name
|
|
gen_table.create_time = datetime.now()
|
|
gen_table.update_by = oper_name
|
|
gen_table.update_time = datetime.now()
|
|
|
|
@classmethod
|
|
def init_column_field(cls, column: GenTableColumnSchema, table: GenTableSchema) -> None:
|
|
"""
|
|
初始化列属性字段
|
|
|
|
param column: 业务表字段对象
|
|
param table: 业务表对象
|
|
:return:
|
|
"""
|
|
data_type = cls.get_db_type(column.column_type)
|
|
column_name = column.column_name
|
|
column.table_id = table.table_id
|
|
column.create_by = table.create_by
|
|
# 设置Python字段名
|
|
column.python_field = cls.to_camel_case(column_name)
|
|
# 设置默认类型
|
|
column.python_type = StringUtil.get_mapping_value_by_key_ignore_case(
|
|
GenConstant.DB_TO_PYTHON_TYPE_MAPPING, data_type
|
|
)
|
|
column.query_type = GenConstant.QUERY_EQ
|
|
|
|
if cls.arrays_contains(GenConstant.COLUMNTYPE_STR, data_type) or cls.arrays_contains(
|
|
GenConstant.COLUMNTYPE_TEXT, data_type
|
|
):
|
|
# 字符串长度超过500设置为文本域
|
|
column_length = cls.get_column_length(column.column_type)
|
|
html_type = (
|
|
GenConstant.HTML_TEXTAREA
|
|
if column_length >= 500 or cls.arrays_contains(GenConstant.COLUMNTYPE_TEXT, data_type)
|
|
else GenConstant.HTML_INPUT
|
|
)
|
|
column.html_type = html_type
|
|
elif cls.arrays_contains(GenConstant.COLUMNTYPE_TIME, data_type):
|
|
column.html_type = GenConstant.HTML_DATETIME
|
|
elif cls.arrays_contains(GenConstant.COLUMNTYPE_NUMBER, data_type):
|
|
column.html_type = GenConstant.HTML_INPUT
|
|
|
|
# 插入字段(默认所有字段都需要插入)
|
|
column.is_insert = GenConstant.REQUIRE
|
|
|
|
# 编辑字段
|
|
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_EDIT, column_name) and not column.pk:
|
|
column.is_edit = GenConstant.REQUIRE
|
|
# 列表字段
|
|
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_LIST, column_name) and not column.pk:
|
|
column.is_list = GenConstant.REQUIRE
|
|
# 查询字段
|
|
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_QUERY, column_name) and not column.pk:
|
|
column.is_query = GenConstant.REQUIRE
|
|
|
|
# 查询字段类型
|
|
if column_name.lower().endswith('name'):
|
|
column.query_type = GenConstant.QUERY_LIKE
|
|
# 状态字段设置单选框
|
|
if column_name.lower().endswith('status'):
|
|
column.html_type = GenConstant.HTML_RADIO
|
|
# 类型&性别字段设置下拉框
|
|
elif column_name.lower().endswith('type') or column_name.lower().endswith('sex'):
|
|
column.html_type = GenConstant.HTML_SELECT
|
|
# 图片字段设置图片上传控件
|
|
elif column_name.lower().endswith('image'):
|
|
column.html_type = GenConstant.HTML_IMAGE_UPLOAD
|
|
# 文件字段设置文件上传控件
|
|
elif column_name.lower().endswith('file'):
|
|
column.html_type = GenConstant.HTML_FILE_UPLOAD
|
|
# 内容字段设置富文本控件
|
|
elif column_name.lower().endswith('content'):
|
|
column.html_type = GenConstant.HTML_EDITOR
|
|
|
|
column.create_by = table.create_by
|
|
column.create_time = datetime.now()
|
|
column.update_by = table.update_by
|
|
column.update_time = datetime.now()
|
|
|
|
@classmethod
|
|
def arrays_contains(cls, arr: List[str], target_value: str) -> bool:
|
|
"""
|
|
校验数组是否包含指定值
|
|
|
|
param arr: 数组
|
|
param target_value: 需要校验的值
|
|
:return: 校验结果
|
|
"""
|
|
return target_value in arr
|
|
|
|
@classmethod
|
|
def get_module_name(cls, package_name: str) -> str:
|
|
"""
|
|
获取模块名
|
|
|
|
param package_name: 包名
|
|
:return: 模块名
|
|
"""
|
|
return package_name.split('.')[-1]
|
|
|
|
@classmethod
|
|
def get_business_name(cls, table_name: str) -> str:
|
|
"""
|
|
获取业务名
|
|
|
|
param table_name: 业务表名
|
|
:return: 业务名
|
|
"""
|
|
return table_name.split('_')[-1]
|
|
|
|
@classmethod
|
|
def convert_class_name(cls, table_name: str) -> str:
|
|
"""
|
|
表名转换成Python类名
|
|
|
|
param table_name: 业务表名
|
|
:return: Python类名
|
|
"""
|
|
auto_remove_pre = settings.auto_remove_pre
|
|
table_prefix = settings.table_prefix
|
|
if auto_remove_pre and table_prefix:
|
|
search_list = table_prefix.split(',')
|
|
table_name = cls.replace_first(table_name, search_list)
|
|
return StringUtil.convert_to_camel_case(table_name)
|
|
|
|
@classmethod
|
|
def replace_first(cls, replacement: str, search_list: List[str]) -> str:
|
|
"""
|
|
批量替换前缀
|
|
|
|
param replacement: 需要被替换的字符串
|
|
param search_list: 可替换的字符串列表
|
|
:return: 替换后的字符串
|
|
"""
|
|
for search_string in search_list:
|
|
if replacement.startswith(search_string):
|
|
return replacement.replace(search_string, '', 1)
|
|
return replacement
|
|
|
|
@classmethod
|
|
def replace_text(cls, text: str) -> str:
|
|
"""
|
|
关键字替换
|
|
|
|
param text: 需要被替换的字符串
|
|
:return: 替换后的字符串
|
|
"""
|
|
return re.sub(r'(?:表|若依)', '', text)
|
|
|
|
@classmethod
|
|
def get_db_type(cls, column_type: str) -> str:
|
|
"""
|
|
获取数据库类型字段
|
|
|
|
param column_type: 字段类型
|
|
:return: 数据库类型
|
|
"""
|
|
if '(' in column_type:
|
|
return column_type.split('(')[0]
|
|
return column_type
|
|
|
|
@classmethod
|
|
def get_column_length(cls, column_type: str) -> int:
|
|
"""
|
|
获取字段长度
|
|
|
|
param column_type: 字段类型
|
|
:return: 字段长度
|
|
"""
|
|
if '(' in column_type:
|
|
length = len(column_type.split('(')[1].split(')')[0])
|
|
return length
|
|
return 0
|
|
|
|
@classmethod
|
|
def split_column_type(cls, column_type: str) -> List[str]:
|
|
"""
|
|
拆分列类型
|
|
|
|
param column_type: 字段类型
|
|
:return: 拆分结果
|
|
"""
|
|
if '(' in column_type and ')' in column_type:
|
|
return column_type.split('(')[1].split(')')[0].split(',')
|
|
return []
|
|
|
|
@classmethod
|
|
def to_camel_case(cls, text: str) -> str:
|
|
"""
|
|
将字符串转换为驼峰命名
|
|
|
|
param text: 需要转换的字符串
|
|
:return: 驼峰命名
|
|
"""
|
|
parts = text.split('_')
|
|
return parts[0] + ''.join(word.capitalize() for word in parts[1:])
|