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.dasset import get_dasset_tree_api, get_dasset_list_api, add_dasset_api, edit_dasset_api, delete_dasset_api, \ get_dasset_detail_api, get_dasset_tree_for_edit_option_api @app.callback( output=dict( dasset_table_data=Output('dasset-list-table', 'data', allow_duplicate=True), dasset_table_key=Output('dasset-list-table', 'key'), dasset_table_defaultexpandedrowkeys=Output('dasset-list-table', 'defaultExpandedRowKeys'), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True), fold_click=Output('dasset-fold', 'nClicks') ), inputs=dict( search_click=Input('dasset-search', 'nClicks'), refresh_click=Input('dasset-refresh', 'nClicks'), operations=Input('dasset-operations-store', 'data'), fold_click=Input('dasset-fold', 'nClicks') ), state=dict( dasset_name=State('dasset-dasset_name-input', 'value'), #dasset_remark=State('dasset-dasset_remark-input', 'value'), status_select=State('dasset-status-select', 'value'), in_default_expanded_row_keys=State('dasset-list-table', 'defaultExpandedRowKeys'), button_perms=State('dasset-button-perms-container', 'data') ), prevent_initial_call=True ) def get_dasset_table_data(search_click, refresh_click, operations, fold_click, dasset_name, status_select, in_default_expanded_row_keys, button_perms): """ 获取数据资产表格数据回调(进行表格相关增删查改操作后均会触发此回调) dasset_remark, """ query_params = dict( dasset_name=dasset_name, #dasset_remark=dasset_remark, status=status_select ) if search_click or refresh_click or operations or fold_click: table_info = get_dasset_list_api(query_params) #print(6661,table_info,6661) 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['dasset_id'])) item['key'] = str(item['dasset_id']) if item['dasset_parent_id'] == 0: item['operation'] = [ { 'content': '修改', 'type': 'link', 'icon': 'antd-edit' } if 'system:dasset:edit' in button_perms else {}, { 'content': '新增', 'type': 'link', 'icon': 'antd-plus' } if 'system:dasset:add' in button_perms else {}, ] elif item['status'] == '1': item['operation'] = [ { 'content': '修改', 'type': 'link', 'icon': 'antd-edit' } if 'system:dasset:edit' in button_perms else {}, { 'content': '删除', 'type': 'link', 'icon': 'antd-delete' } if 'system:dasset:remove' in button_perms else {}, ] else: item['operation'] = [ { 'content': '修改', 'type': 'link', 'icon': 'antd-edit' } if 'system:dasset:edit' in button_perms else {}, { 'content': '新增', 'type': 'link', 'icon': 'antd-plus' } if 'system:dasset:add' in button_perms else {}, { 'content': '删除', 'type': 'link', 'icon': 'antd-delete' } if 'system:dasset: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, 'dasset_id', 'dasset_parent_id') if fold_click: if in_default_expanded_row_keys: return dict( dasset_table_data=table_data_new, dasset_table_key=str(uuid.uuid4()), dasset_table_defaultexpandedrowkeys=[], api_check_token_trigger={'timestamp': time.time()}, fold_click=None ) return dict( dasset_table_data=table_data_new, dasset_table_key=str(uuid.uuid4()), dasset_table_defaultexpandedrowkeys=default_expanded_row_keys, api_check_token_trigger={'timestamp': time.time()}, fold_click=None ) return dict( dasset_table_data=dash.no_update, dasset_table_key=dash.no_update, dasset_table_defaultexpandedrowkeys=dash.no_update, api_check_token_trigger={'timestamp': time.time()}, fold_click=None ) return dict( dasset_table_data=dash.no_update, dasset_table_key=dash.no_update, dasset_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, null, {'type': 'reset'}] } return window.dash_clientside.no_update; } ''', [Output('dasset-dasset_name-input', 'value'), Output('dasset-dasset_remark-input', 'value'), Output('dasset-status-select', 'value'), Output('dasset-operations-store', 'data')], Input('dasset-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('dasset-search-form-container', 'hidden'), Output('dasset-hidden-tooltip', 'title')], Input('dasset-hidden', 'nClicks'), State('dasset-search-form-container', 'hidden'), prevent_initial_call=True ) @app.callback( output=dict( modal_visible=Output('dasset-modal', 'visible', allow_duplicate=True), modal_title=Output('dasset-modal', 'title'), dasset_parent_id_div_ishidden=Output('dasset-dasset_parent_id-div', 'hidden'), dasset_parent_id_tree=Output({'type': 'dasset-form-value', 'index': 'dasset_parent_id'}, 'treeData'), form_value=Output({'type': 'dasset-form-value', 'index': ALL}, 'value'), form_label_validate_status=Output({'type': 'dasset-form-label', 'index': ALL, 'required': True}, 'validateStatus', allow_duplicate=True), form_label_validate_info=Output({'type': 'dasset-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('dasset-edit-id-store', 'data'), modal_type=Output('dasset-operations-store-bk', 'data') ), inputs=dict( operation_click=Input({'type': 'dasset-operation-button', 'index': ALL}, 'nClicks'), button_click=Input('dasset-list-table', 'nClicksButton') ), state=dict( clicked_content=State('dasset-list-table', 'clickedContent'), recently_button_clicked_row=State('dasset-list-table', 'recentlyButtonClickedRow') ), prevent_initial_call=True ) def add_edit_dasset_modal(operation_click, button_click, clicked_content, recently_button_clicked_row): """ 显示新增或编辑数据资产弹窗回调 """ trigger_id = dash.ctx.triggered_id if trigger_id == {'index': 'add', 'type': 'dasset-operation-button'} or ( trigger_id == 'dasset-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]] dasset_params = dict(dasset_name='') if trigger_id == 'dasset-list-table' and clicked_content == '修改': dasset_params['dasset_id'] = int(recently_button_clicked_row['key']) tree_info = get_dasset_tree_for_edit_option_api(dasset_params) else: tree_info = get_dasset_tree_api(dasset_params) if tree_info['code'] == 200: tree_data = tree_info['data'] if trigger_id == {'index': 'add', 'type': 'dasset-operation-button'} or (trigger_id == 'dasset-list-table' and clicked_content == '新增'): dasset_info = dict( dasset_parent_id=None if trigger_id == {'index': 'add', 'type': 'dasset-operation-button'} else str(recently_button_clicked_row['key']), dasset_name=None, dasset_remark=None, # dasset_area=None, clas=None, usages=None, data_sec_cifd=None, dasset_order_num=None, status='0', ) return dict( modal_visible=True, modal_title='新增数据资产', dasset_parent_id_div_ishidden=False, dasset_parent_id_tree=tree_data, form_value=[dasset_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 == 'dasset-list-table' and clicked_content == '修改': dasset_id = int(recently_button_clicked_row['key']) dasset_info_res = get_dasset_detail_api(dasset_id=dasset_id) if dasset_info_res['code'] == 200: dasset_info = dasset_info_res['data'] return dict( modal_visible=True, modal_title='编辑数据资产', #dasset_parent_id_div_ishidden=dasset_info.get('dasset_parent_id') == 0, dasset_parent_id_div_ishidden=dasset_info.get('dasset_parent_id'), dasset_parent_id_tree=tree_data, form_value=[dasset_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=dasset_info, modal_type={'type': 'edit'} ) return dict( modal_visible=dash.no_update, modal_title=dash.no_update, dasset_parent_id_div_ishidden=dash.no_update, dasset_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': 'dasset-form-label', 'index': ALL, 'required': True}, 'validateStatus', allow_duplicate=True), form_label_validate_info=Output({'type': 'dasset-form-label', 'index': ALL, 'required': True}, 'help', allow_duplicate=True), modal_visible=Output('dasset-modal', 'visible'), operations=Output('dasset-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('dasset-modal', 'okCounts') ), state=dict( modal_type=State('dasset-operations-store-bk', 'data'), edit_row_info=State('dasset-edit-id-store', 'data'), form_value=State({'type': 'dasset-form-value', 'index': ALL}, 'value'), form_label=State({'type': 'dasset-form-label', 'index': ALL, 'required': True}, 'label') ), prevent_initial_call=True ) def dasset_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['dasset_id'] = edit_row_info.get('dasset_id') if edit_row_info else None api_res = {} modal_type = modal_type.get('type') print(params_add,999999999) if modal_type == 'add': api_res = add_dasset_api(params_add) if modal_type == 'edit': api_res = edit_dasset_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('dasset-delete-text', 'children'), Output('dasset-delete-confirm-modal', 'visible'), Output('dasset-delete-ids-store', 'data')], [Input('dasset-list-table', 'nClicksButton')], [State('dasset-list-table', 'clickedContent'), State('dasset-list-table', 'recentlyButtonClickedRow')], prevent_initial_call=True ) def dasset_delete_modal(button_click, clicked_content, recently_button_clicked_row): """ 显示删除数据资产二次确认弹窗回调 """ if button_click: if clicked_content == '删除': dasset_ids = recently_button_clicked_row['key'] else: return dash.no_update return [ f'是否确认删除本数据资产?', True, {'dasset_ids': dasset_ids} ] raise PreventUpdate @app.callback( [Output('dasset-operations-store', 'data', allow_duplicate=True), Output('api-check-token', 'data', allow_duplicate=True), Output('global-message-container', 'children', allow_duplicate=True)], Input('dasset-delete-confirm-modal', 'okCounts'), State('dasset-delete-ids-store', 'data'), prevent_initial_call=True ) def dasset_delete_confirm(delete_confirm, dasset_ids_data): """ 删除数据资产弹窗确认回调,实现删除操作 """ if delete_confirm: params = dasset_ids_data delete_button_info = delete_dasset_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('dasset-operations-store', 'data', allow_duplicate=True), Output('api-check-token', 'data', allow_duplicate=True), Output('global-message-container', 'children', allow_duplicate=True)], [Input('dasset-list-table', 'recentlySwitchDataIndex'), Input('dasset-list-table', 'recentlySwitchStatus'), Input('dasset-list-table', 'recentlySwitchRow')], prevent_initial_call=True ) def table_switch_dasset_status(recently_switch_data_index, recently_switch_status, recently_switch_row): """ 表格内切换标准状态回调 """ if recently_switch_data_index: if recently_switch_status: params = dict(dasset_id=int(recently_switch_row['key']), status='0', type='status') else: params = dict(dasset_id=int(recently_switch_row['key']), status='1', type='status') edit_button_result = edit_dasset_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