编码
模块分为三种,应用、插件、组件,分别对应app、plugins、vendor文件夹。
# 模块编码
# crud代码生成
python main.py crud -t example_student -o app/app1
1
# CRUD代码示例
本平台通过在 API 层编写少量代码,即可实现全面的前端功能自动生成,包括一对一、一对多、多对多关系的数据展示,搜索列配置,列表页生成,功能权限与数据权限管理,API 接口文档自动生成,以及完整的 CRUD 操作支持。该平台不仅显著提高了开发效率,还确保了系统的灵活性、安全性和可维护性。
from senweaver.core.helper import FieldConfig
from typing import List, Union
from senweaver.core.helper import SenweaverFilter, RelationConfig
from senweaver import senweaver_router
from fastapi import APIRouter, Request
from senweaver.utils.response import ResponseBase, success_response
from ..example import module
from ..model.example import Example, ExampleRead
from ..logic.notice_logic import ExampleLogic
router = APIRouter(tags=["example"])
filter_config = SenweaverFilter(
# 查询过滤条件
filters={"id": None, "title__contains": None, "message__contains": None,
"notice_type": None, "level": None, "publish": None},
# 显示的字段
fields=['id', 'title', 'level', "publish", 'notice_type', "notice_user", 'notice_dept', 'notice_role',
'message', "created_time", "user_count", "read_user_count", 'extra_json', "files"],
# 列表展示字段
table_fields=['id', 'title', 'notice_type',
"read_user_count", "publish", "created_time"],
# 字段扩展配置
extra_kwargs={
'extra_json': {'read_only': True},
},
# 可排序字段
ordering_fields=['updated_time', 'created_time'],
# 关系字段
relationships=[
RelationConfig(rel=Example.notice_user, attrs=[
'id', 'username'], format="{username}", many=True, label="被通知用户", read_only=False, required=True, write_only=False, input_type="api-search-user", callbacks={"select": ExampleLogic.get_notice_user}),
RelationConfig(
rel=Example.notice_dept, attrs=[
'id', 'name'], format="{name}", label="被通知部门", many=True, input_type='api-search-dept'),
RelationConfig(
rel=Example.notice_role, attrs=[
'id', 'name'], format="{name}", label="被通知角色", many=True, read_only=False, input_type='api-search-role')
],
# 扩展字段信息
extra_fields=[
FieldConfig(key="files", default=[], annotation=list, write_only=True,
label="上传的附件", input_type="json"),
FieldConfig(key="user_count", default=0, label="用户数量", read_only=True,
annotation=int, input_type="field", callbacks={"select": ExampleLogic.get_user_count}),
FieldConfig(key="read_user_count", default=0, annotation=int, read_only=True,
label="已读用户数量", input_type="field", callbacks={"select": ExampleLogic.get_read_user_count})
],
)
# 动态创建crud、导入导出等相关路由
_router = senweaver_router(
module=module,
model=Example,
path=f"/example",
filter_config=filter_config,
callbacks={"save": ExampleLogic.save},
custom_router=ExampleLogic.add_custom_router
)
router.include_router(_router)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
上次更新: 2025/02/28, 10:58:46