pipeline-sdlc/wwwroot/sd_projects/get_sd_projects.dspy

161 lines
3.7 KiB
Plaintext

ns = params_kw.copy()
userorgid = await get_userorgid()
if not userorgid:
return {
"widgettype":"Error",
"options":{
"title":"Authorization Error",
"timeout":3,
"cwidth":16,
"cheight":9,
"message":"Please login"
}
}
ns['org_id'] = userorgid
ns['userorgid'] = userorgid
debug(f'get_sd_projects.dspy:{ns=}')
if not ns.get('page'):
ns['page'] = 1
if not ns.get('sort'):
ns['sort'] = ["created_at desc"]
sql = '''select a.*, b.status_text, c.project_type_text
from (select * from sd_projects where 1=1 [[filterstr]]) a left join (select k as status,
v as status_text from appcodes_kv where parentid='sd_project_status') b on a.status = b.status left join (select k as project_type,
v as project_type_text from appcodes_kv where parentid='sd_project_type') c on a.project_type = c.project_type'''
filterjson = params_kw.get('data_filter')
if filterjson and isinstance(filterjson, str):
try:
filterjson = json.loads(filterjson)
except (json.JSONDecodeError, TypeError):
filterjson = None
fields_str=r'''[
{
"name": "id",
"title": "主键ID",
"type": "str",
"length": 32,
"nullable": "no"
},
{
"name": "name",
"title": "项目名称",
"type": "str",
"length": 200,
"nullable": "no"
},
{
"name": "description",
"title": "项目描述",
"type": "text"
},
{
"name": "project_type",
"title": "项目类型",
"type": "str",
"length": 50,
"nullable": "no",
"default": "'web_app'"
},
{
"name": "tech_stack",
"title": "技术栈配置(JSON)",
"type": "text"
},
{
"name": "repo_url",
"title": "代码仓库地址",
"type": "str",
"length": 500
},
{
"name": "pipeline_id",
"title": "关联Pipeline定义ID",
"type": "str",
"length": 32
},
{
"name": "status",
"title": "项目状态",
"type": "str",
"length": 20,
"nullable": "no",
"default": "'draft'"
},
{
"name": "org_id",
"title": "组织ID",
"type": "str",
"length": 32,
"nullable": "no",
"default": "'0'"
},
{
"name": "created_by",
"title": "创建人",
"type": "str",
"length": 32
},
{
"name": "created_at",
"title": "创建时间",
"type": "timestamp",
"nullable": "no"
},
{
"name": "updated_at",
"title": "更新时间",
"type": "timestamp"
}
]'''
ori_fields = json.loads(fields_str)
if not filterjson:
fields = [ f['name'] for f in ori_fields ]
filterjson = default_filterjson(fields, ns)
# 确保 logined 过滤条件始终生效
if filterjson:
if not isinstance(filterjson, dict) or 'AND' not in filterjson:
filterjson = {'AND': [filterjson] if filterjson else []}
filterjson['AND'].append({'field': 'org_id', 'op': '=', 'var': '__logined_orgid__'})
ns['__logined_orgid__'] = userorgid
filterdic = ns.copy()
filterdic['filterstr'] = ''
filterdic['userorgid'] = '${userorgid}$'
filterdic['userid'] = '${userid}$'
if filterjson:
dbf = DBFilter(filterjson)
conds = dbf.gen(ns)
if conds:
ns.update(dbf.consts)
conds = f' and {conds}'
filterdic['filterstr'] = conds
ac = ArgsConvert('[[', ']]')
vars = ac.findAllVariables(sql)
NameSpace = {v:'${' + v + '}$' for v in vars if v != 'filterstr' }
filterdic.update(NameSpace)
sql = ac.convert(sql, filterdic)
debug(f'{sql=}')
db = DBPools()
dbname = get_module_dbname('pipeline_sdlc')
async with db.sqlorContext(dbname) as sor:
r = await sor.sqlPaging(sql, ns)
return r
return {
"total":0,
"rows":[]
}