import dash import time import uuid from dash.dependencies import Input, Output, State, ALL from dash.exceptions import PreventUpdate import feffery_utils_components as fuc from server import app from utils.common import validate_data_not_empty from utils.tree_tool import list_to_tree from api.dscatalog import get_dscatalog_tree_api, get_dscatalog_list_api, add_dscatalog_api, edit_dscatalog_api, delete_dscatalog_api, \ get_dscatalog_detail_api, get_dscatalog_tree_for_edit_option_api @app.callback( output=dict( dscatalog_table_data=Output('dscatalog-list-table', 'data', allow_duplicate=True), dscatalog_table_key=Output('dscatalog-list-table', 'key'), dscatalog_table_defaultexpandedrowkeys=Output('dscatalog-list-table', 'defaultExpandedRowKeys'), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True), fold_click=Output('dscatalog-fold', 'nClicks') ), inputs=dict( search_click=Input('dscatalog-search', 'nClicks'), refresh_click=Input('dscatalog-refresh', 'nClicks'), operations=Input('dscatalog-operations-store', 'data'), fold_click=Input('dscatalog-fold', 'nClicks') ), state=dict( dscatalog_name=State('dscatalog-dscatalog_name-input', 'value'), #dscatalog_remark=State('dscatalog-dscatalog_remark-input', 'value'), dscatalog-status-select status_select=State('dscatalog-status-select', 'value'), in_default_expanded_row_keys=State('dscatalog-list-table', 'defaultExpandedRowKeys'), button_perms=State('dscatalog-button-perms-container', 'data') ), prevent_initial_call=True ) def get_dscatalog_table_data(search_click, refresh_click, operations, fold_click, dscatalog_name, status_select, in_default_expanded_row_keys, button_perms): """ 获取数据资产表格数据回调(进行表格相关增删查改操作后均会触发此回调) """ print(111) query_params = dict( dscatalog_name=dscatalog_name, #dscatalog_remark=dscatalog_remark, status=status_select ) if search_click or refresh_click or operations or fold_click: table_info = get_dscatalog_list_api(query_params) default_expanded_row_keys = [] if table_info['code'] == 200: table_data = table_info['data']['rows'] for item in table_data: default_expanded_row_keys.append(str(item['dscatalog_id'])) item['key'] = str(item['dscatalog_id']) if item['dscatalog_parent_id'] == 0: item['operation'] = [ { 'content': '修改', 'type': 'link', 'icon': 'antd-edit' } if 'system:dscatalog:edit' in button_perms else {}, { 'content': '新增', 'type': 'link', 'icon': 'antd-plus' } if 'system:dscatalog:add' in button_perms else {}, ] elif item['status'] == '1': item['operation'] = [ { 'content': '修改', 'type': 'link', 'icon': 'antd-edit' } if 'system:dscatalog:edit' in button_perms else {}, { 'content': '删除', 'type': 'link', 'icon': 'antd-delete' } if 'system:dscatalog:remove' in button_perms else {}, ] else: item['operation'] = [ { 'content': '修改', 'type': 'link', 'icon': 'antd-edit' } if 'system:dscatalog:edit' in button_perms else {}, { 'content': '新增', 'type': 'link', 'icon': 'antd-plus' } if 'system:dscatalog:add' in button_perms else {}, { 'content': '删除', 'type': 'link', 'icon': 'antd-delete' } if 'system:dscatalog:remove' in button_perms else {}, ] if item['status'] == '0': #item['status'] = dict(tag='正常', color='blue') item['status'] = dict(checked=True) else: #item['status'] = dict(tag='停用', color='volcano') item['status'] = dict(checked=False) table_data_new = list_to_tree(table_data, 'dscatalog_id', 'dscatalog_parent_id') #print(8888888888888,table_data_new) if fold_click: if in_default_expanded_row_keys: return dict( dscatalog_table_data=table_data_new, dscatalog_table_key=str(uuid.uuid4()), dscatalog_table_defaultexpandedrowkeys=[], api_check_token_trigger={'timestamp': time.time()}, fold_click=None ) return dict( dscatalog_table_data=table_data_new, dscatalog_table_key=str(uuid.uuid4()), dscatalog_table_defaultexpandedrowkeys=default_expanded_row_keys, api_check_token_trigger={'timestamp': time.time()}, fold_click=None ) return dict( dscatalog_table_data=dash.no_update, dscatalog_table_key=dash.no_update, dscatalog_table_defaultexpandedrowkeys=dash.no_update, api_check_token_trigger={'timestamp': time.time()}, fold_click=None ) return dict( dscatalog_table_data=dash.no_update, dscatalog_table_key=dash.no_update, dscatalog_table_defaultexpandedrowkeys=dash.no_update, api_check_token_trigger=dash.no_update, fold_click=None ) # 重置数据资产搜索表单数据回调 app.clientside_callback( ''' (reset_click) => { if (reset_click) { return [null, null, {'type': 'reset'}] } return window.dash_clientside.no_update; } ''', [Output('dscatalog-dscatalog_name-input', 'value'), #Output('dscatalog-dscatalog_remark-input', 'value'), Output('dscatalog-status-select', 'value'), Output('dscatalog-operations-store', 'data')], Input('dscatalog-reset', 'nClicks'), prevent_initial_call=True ) # 隐藏/显示数据资产搜索表单回调 app.clientside_callback( ''' (hidden_click, hidden_status) => { if (hidden_click) { return [ !hidden_status, hidden_status ? '隐藏搜索' : '显示搜索' ] } return window.dash_clientside.no_update; } ''', [Output('dscatalog-search-form-container', 'hidden'), Output('dscatalog-hidden-tooltip', 'title')], Input('dscatalog-hidden', 'nClicks'), State('dscatalog-search-form-container', 'hidden'), prevent_initial_call=True ) @app.callback( output=dict( modal_visible=Output('dscatalog-modal', 'visible', allow_duplicate=True), modal_title=Output('dscatalog-modal', 'title'), dscatalog_parent_id_div_ishidden=Output('dscatalog-dscatalog_parent_id-div', 'hidden'), dscatalog_parent_id_tree=Output({'type': 'dscatalog-form-value', 'index': 'dscatalog_parent_id'}, 'treeData'), form_value=Output({'type': 'dscatalog-form-value', 'index': ALL}, 'value'), form_label_validate_status=Output({'type': 'dscatalog-form-label', 'index': ALL, 'required': True}, 'validateStatus', allow_duplicate=True), form_label_validate_info=Output({'type': 'dscatalog-form-label', 'index': ALL, 'required': True}, 'help', allow_duplicate=True), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True), edit_row_info=Output('dscatalog-edit-id-store', 'data'), modal_type=Output('dscatalog-operations-store-bk', 'data') ), inputs=dict( operation_click=Input({'type': 'dscatalog-operation-button', 'index': ALL}, 'nClicks'), button_click=Input('dscatalog-list-table', 'nClicksButton') ), state=dict( clicked_content=State('dscatalog-list-table', 'clickedContent'), recently_button_clicked_row=State('dscatalog-list-table', 'recentlyButtonClickedRow') ), prevent_initial_call=True ) def add_edit_dscatalog_modal(operation_click, button_click, clicked_content, recently_button_clicked_row): """ 显示新增或编辑数据资产弹窗回调 """ trigger_id = dash.ctx.triggered_id if trigger_id == {'index': 'add', 'type': 'dscatalog-operation-button'} or ( trigger_id == 'dscatalog-list-table' and clicked_content != '删除'): # 获取所有输出表单项对应value的index form_value_list = [x['id']['index'] for x in dash.ctx.outputs_list[4]] # 获取所有输出表单项对应label的index form_label_list = [x['id']['index'] for x in dash.ctx.outputs_list[5]] dscatalog_params = dict(dscatalog_name='') if trigger_id == 'dscatalog-list-table' and clicked_content == '修改': # print('新增功能', 111111) dscatalog_params['dscatalog_id'] = int(recently_button_clicked_row['key']) tree_info = get_dscatalog_tree_for_edit_option_api(dscatalog_params) else: tree_info = get_dscatalog_tree_api(dscatalog_params) if tree_info['code'] == 200: tree_data = tree_info['data'] if trigger_id == {'index': 'add', 'type': 'dscatalog-operation-button'} or (trigger_id == 'dscatalog-list-table' and clicked_content == '新增'): # print('新增功能',222222) # print('tree_data',tree_data) dscatalog_info = dict( dscatalog_parent_id=None if trigger_id == {'index': 'add', 'type': 'dscatalog-operation-button'} else str(recently_button_clicked_row['key']), dscatalog_name=None, dscatalog_remark=None, dscatalog_area=None, dscatalog_order_num=None, status='0', ) return dict( modal_visible=True, modal_title='新增数据资产', dscatalog_parent_id_div_ishidden=False, dscatalog_parent_id_tree=tree_data, form_value=[dscatalog_info.get(k) for k in form_value_list], form_label_validate_status=[None] * len(form_label_list), form_label_validate_info=[None] * len(form_label_list), api_check_token_trigger={'timestamp': time.time()}, edit_row_info=None, modal_type={'type': 'add'} ) elif trigger_id == 'dscatalog-list-table' and clicked_content == '修改': dscatalog_id = int(recently_button_clicked_row['key']) dscatalog_info_res = get_dscatalog_detail_api(dscatalog_id=dscatalog_id) if dscatalog_info_res['code'] == 200: dscatalog_info = dscatalog_info_res['data'] return dict( modal_visible=True, modal_title='编辑数据资产', dscatalog_parent_id_div_ishidden=dscatalog_info.get('dscatalog_parent_id') == 0, dscatalog_parent_id_tree=tree_data, form_value=[dscatalog_info.get(k) for k in form_value_list], form_label_validate_status=[None] * len(form_label_list), form_label_validate_info=[None] * len(form_label_list), api_check_token_trigger={'timestamp': time.time()}, edit_row_info=dscatalog_info, modal_type={'type': 'edit'} ) return dict( modal_visible=dash.no_update, modal_title=dash.no_update, dscatalog_parent_id_div_ishidden=dash.no_update, dscatalog_parent_id_tree=dash.no_update, form_value=[dash.no_update] * len(form_value_list), form_label_validate_status=[dash.no_update] * len(form_label_list), form_label_validate_info=[dash.no_update] * len(form_label_list), api_check_token_trigger={'timestamp': time.time()}, edit_row_info=None, modal_type=None ) raise PreventUpdate @app.callback( output=dict( form_label_validate_status=Output({'type': 'dscatalog-form-label', 'index': ALL, 'required': True}, 'validateStatus', allow_duplicate=True), form_label_validate_info=Output({'type': 'dscatalog-form-label', 'index': ALL, 'required': True}, 'help', allow_duplicate=True), modal_visible=Output('dscatalog-modal', 'visible'), operations=Output('dscatalog-operations-store', 'data', allow_duplicate=True), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True), global_message_container=Output('global-message-container', 'children', allow_duplicate=True) ), inputs=dict( confirm_trigger=Input('dscatalog-modal', 'okCounts') ), state=dict( modal_type=State('dscatalog-operations-store-bk', 'data'), edit_row_info=State('dscatalog-edit-id-store', 'data'), form_value=State({'type': 'dscatalog-form-value', 'index': ALL}, 'value'), form_label=State({'type': 'dscatalog-form-label', 'index': ALL, 'required': True}, 'label') ), prevent_initial_call=True ) def dscatalog_confirm(confirm_trigger, modal_type, edit_row_info, form_value, form_label): """ 新增或编辑数据资产弹窗确认回调,实现新增或编辑操作 """ if confirm_trigger: # 获取所有输出表单项对应label的index form_label_output_list = [x['id']['index'] for x in dash.ctx.outputs_list[0]] # 获取所有输入表单项对应的value及label form_value_state = {x['id']['index']: x.get('value') for x in dash.ctx.states_list[-2]} form_label_state = {x['id']['index']: x.get('value') for x in dash.ctx.states_list[-1]} if all(validate_data_not_empty(item) for item in [form_value_state.get(k) for k in form_label_output_list]): params_add = form_value_state params_edit = params_add.copy() params_edit['dscatalog_id'] = edit_row_info.get('dscatalog_id') if edit_row_info else None api_res = {} modal_type = modal_type.get('type') if modal_type == 'add': api_res = add_dscatalog_api(params_add) if modal_type == 'edit': api_res = edit_dscatalog_api(params_edit) if api_res.get('code') == 200: if modal_type == 'add': return dict( form_label_validate_status=[None] * len(form_label_output_list), form_label_validate_info=[None] * len(form_label_output_list), modal_visible=False, operations={'type': 'add'}, api_check_token_trigger={'timestamp': time.time()}, global_message_container=fuc.FefferyFancyMessage('新增成功', type='success') ) if modal_type == 'edit': return dict( form_label_validate_status=[None] * len(form_label_output_list), form_label_validate_info=[None] * len(form_label_output_list), modal_visible=False, operations={'type': 'edit'}, api_check_token_trigger={'timestamp': time.time()}, global_message_container=fuc.FefferyFancyMessage('编辑成功', type='success') ) return dict( form_label_validate_status=[None] * len(form_label_output_list), form_label_validate_info=[None] * len(form_label_output_list), modal_visible=dash.no_update, operations=dash.no_update, api_check_token_trigger={'timestamp': time.time()}, global_message_container=fuc.FefferyFancyMessage('处理失败', type='error') ) return dict( form_label_validate_status=[None if validate_data_not_empty(form_value_state.get(k)) else 'error' for k in form_label_output_list], form_label_validate_info=[None if validate_data_not_empty(form_value_state.get(k)) else f'{form_label_state.get(k)}不能为空!' for k in form_label_output_list], modal_visible=dash.no_update, operations=dash.no_update, api_check_token_trigger=dash.no_update, global_message_container=fuc.FefferyFancyMessage('处理失败', type='error') ) raise PreventUpdate @app.callback( [Output('dscatalog-delete-text', 'children'), Output('dscatalog-delete-confirm-modal', 'visible'), Output('dscatalog-delete-ids-store', 'data')], [Input('dscatalog-list-table', 'nClicksButton')], [State('dscatalog-list-table', 'clickedContent'), State('dscatalog-list-table', 'recentlyButtonClickedRow')], prevent_initial_call=True ) def dscatalog_delete_modal(button_click, clicked_content, recently_button_clicked_row): """ 显示删除数据资产二次确认弹窗回调 """ if button_click: if clicked_content == '删除': dscatalog_ids = recently_button_clicked_row['key'] else: return dash.no_update return [ f'是否确认删除本数据标准目录?', True, {'dscatalog_ids': dscatalog_ids} ] raise PreventUpdate @app.callback( [Output('dscatalog-operations-store', 'data', allow_duplicate=True), Output('api-check-token', 'data', allow_duplicate=True), Output('global-message-container', 'children', allow_duplicate=True)], Input('dscatalog-delete-confirm-modal', 'okCounts'), State('dscatalog-delete-ids-store', 'data'), prevent_initial_call=True ) def dscatalog_delete_confirm(delete_confirm, dscatalog_ids_data): """ 删除数据资产弹窗确认回调,实现删除操作 """ if delete_confirm: params = dscatalog_ids_data delete_button_info = delete_dscatalog_api(params) if delete_button_info['code'] == 200: return [ {'type': 'delete'}, {'timestamp': time.time()}, fuc.FefferyFancyMessage('删除成功', type='success') ] return [ dash.no_update, {'timestamp': time.time()}, fuc.FefferyFancyMessage('删除失败', type='error') ] raise PreventUpdate @app.callback( [Output('dscatalog-operations-store', 'data', allow_duplicate=True), Output('api-check-token', 'data', allow_duplicate=True), Output('global-message-container', 'children', allow_duplicate=True)], [Input('dscatalog-list-table', 'recentlySwitchDataIndex'), Input('dscatalog-list-table', 'recentlySwitchStatus'), Input('dscatalog-list-table', 'recentlySwitchRow')], prevent_initial_call=True ) def table_switch_dscatalog_status(recently_switch_data_index, recently_switch_status, recently_switch_row): """ 表格内切换标准状态回调 """ if recently_switch_data_index: if recently_switch_status: params = dict(dscatalog_id=int(recently_switch_row['key']), status='0', type='status') else: params = dict(dscatalog_id=int(recently_switch_row['key']), status='1', type='status') edit_button_result = edit_dscatalog_api(params) if edit_button_result['code'] == 200: return [ {'type': 'switch-status'}, {'timestamp': time.time()}, fuc.FefferyFancyMessage('修改成功', type='success') ] return [ {'type': 'switch-status'}, {'timestamp': time.time()}, fuc.FefferyFancyMessage('修改失败', type='error') ] raise PreventUpdate