Skip to content

Commit 3be4287

Browse files
committed
feat: Support for batch uploading data table notes #557
1 parent a6dcec0 commit 3be4287

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

backend/apps/datasource/api/datasource.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ def insert_pg(df, tableName, engine):
387387

388388

389389
t_sheet = "数据表列表"
390+
t_s_col = "Sheet名称"
390391
t_n_col = "表名"
391392
t_c_col = "表备注"
392393
f_n_col = "字段名"
@@ -406,7 +407,8 @@ def inner():
406407
if id == 0: # download template
407408
file_name = '批量上传备注'
408409
df_list = [
409-
{'sheet': t_sheet, 'c1_h': t_n_col, 'c2_h': t_c_col, 'c1': ["user", "score"],
410+
{'sheet': t_sheet, 'c0_h': t_s_col, 'c1_h': t_n_col, 'c2_h': t_c_col, 'c0': ["数据表1", "数据表2"],
411+
'c1': ["user", "score"],
410412
'c2': ["用来存放用户信息的数据表", "用来存放用户课程信息的数据表"]},
411413
{'sheet': '数据表1', 'c1_h': f_n_col, 'c2_h': f_c_col, 'c1': ["id", "name"],
412414
'c2': ["用户id", "用户姓名"]},
@@ -421,14 +423,15 @@ def inner():
421423
raise HTTPException(400, "No tables")
422424

423425
df_list = []
424-
df1 = {'sheet': t_sheet, 'c1_h': t_n_col, 'c2_h': t_c_col, 'c1': [], 'c2': []}
426+
df1 = {'sheet': t_sheet, 'c0_h': t_s_col,'c1_h': t_n_col, 'c2_h': t_c_col,'c0': [], 'c1': [], 'c2': []}
425427
df_list.append(df1)
426-
for table in tables:
428+
for index, table in enumerate(tables):
429+
df1['c0'].append(f"Sheet{index}")
427430
df1['c1'].append(table.table_name)
428431
df1['c2'].append(table.custom_comment)
429432

430433
fields = session.query(CoreField).filter(CoreField.table_id == table.id).all()
431-
df_fields = {'sheet': table.table_name, 'c1_h': f_n_col, 'c2_h': f_c_col, 'c1': [], 'c2': []}
434+
df_fields = {'sheet': f"Sheet{index}", 'c1_h': f_n_col, 'c2_h': f_c_col, 'c1': [], 'c2': []}
432435
for field in fields:
433436
df_fields['c1'].append(field.field_name)
434437
df_fields['c2'].append(field.custom_comment)
@@ -437,10 +440,14 @@ def inner():
437440
# build dataframe and export
438441
output = io.BytesIO()
439442

440-
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
441-
for df in df_list:
442-
pd.DataFrame({df['c1_h']: df['c1'], df['c2_h']: df['c2']}).to_excel(writer, sheet_name=df['sheet'],
443-
index=False)
443+
with (pd.ExcelWriter(output, engine='xlsxwriter') as writer):
444+
for index, df in enumerate(df_list):
445+
if index == 0:
446+
pd.DataFrame({df['c0_h']: df['c0'], df['c1_h']: df['c1'], df['c2_h']: df['c2']}
447+
).to_excel(writer, sheet_name=df['sheet'], index=False)
448+
else:
449+
pd.DataFrame({df['c1_h']: df['c1'], df['c2_h']: df['c2']}).to_excel(writer, sheet_name=df['sheet'],
450+
index=False)
444451

445452
output.seek(0)
446453

@@ -486,10 +493,14 @@ async def upload_ds_schema(session: SessionDep, id: int = Path(..., description=
486493

487494
# print(field_sheets)
488495

496+
# sheet table mapping
497+
sheet_table_map = {}
498+
489499
# get data and update
490500
# update table comment
491501
if table_sheet and len(table_sheet) > 0:
492502
for table in table_sheet:
503+
sheet_table_map[table[t_s_col]] = table[t_n_col]
493504
session.query(CoreTable).filter(
494505
and_(CoreTable.ds_id == id, CoreTable.table_name == table[t_n_col])).update(
495506
{'custom_comment': table[t_c_col]})
@@ -499,8 +510,9 @@ async def upload_ds_schema(session: SessionDep, id: int = Path(..., description=
499510
for fields in field_sheets:
500511
if len(fields['data']) > 0:
501512
# get table id
513+
table_name = sheet_table_map[fields['sheet_name']]
502514
table = session.query(CoreTable).filter(
503-
and_(CoreTable.ds_id == id, CoreTable.table_name == fields['sheet_name'])).first()
515+
and_(CoreTable.ds_id == id, CoreTable.table_name == table_name)).first()
504516
if table:
505517
for field in fields['data']:
506518
session.query(CoreField).filter(
@@ -512,4 +524,4 @@ async def upload_ds_schema(session: SessionDep, id: int = Path(..., description=
512524

513525
return True
514526
except Exception as e:
515-
raise HTTPException(status_code=500, detail=f"解析 Excel 失败: {str(e)}")
527+
raise HTTPException(status_code=500, detail=f"Parse Excel Failed: {str(e)}")

0 commit comments

Comments
 (0)