forked from TencentBlueKing/bk-nodeman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 优化excel模板(closed TencentBlueKing#2437)
- Loading branch information
1 parent
7f47072
commit cc60a66
Showing
9 changed files
with
591 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
import logging | ||
import re | ||
from typing import Any, Dict, List | ||
|
||
from django.core.files.uploadedfile import InMemoryUploadedFile | ||
from openpyxl import Workbook, load_workbook | ||
|
||
from apps.node_man import constants, models | ||
from apps.node_man.handlers.cmdb import CmdbHandler | ||
from apps.node_man.tools.excel import ExcelTools | ||
from apps.node_man.tools.host import HostTools | ||
|
||
logger = logging.getLogger("app") | ||
|
||
MAIN_SHEET_NAME = "bk_nodeman_info" | ||
|
||
|
||
class ExcelHandler: | ||
@classmethod | ||
def generate_excel_template(cls): | ||
|
||
# 整合数据转为下拉框所需列表, [id]name 格式 | ||
all_install_channel = [ | ||
f"[{item['id']}]{item['name']}" for item in list(models.InstallChannel.objects.all().values()) | ||
] | ||
all_install_channel.insert(0, "[0]default") | ||
all_biz = [ | ||
f"[{item['bk_biz_id']}]{item['bk_biz_name']}" | ||
for item in CmdbHandler().biz(param={"action": "agent_operate"}) | ||
] | ||
all_cloud = [ | ||
f"[{item['bk_cloud_id']}]{item['bk_cloud_name']}" for item in list(models.Cloud.objects.all().values()) | ||
] | ||
all_cloud.insert(0, f"[{constants.DEFAULT_CLOUD}]{constants.DEFAULT_CLOUD_NAME}") | ||
all_ap = [f"[{item['id']}]{item['name']}" for item in list(models.AccessPoint.objects.all().values())] | ||
|
||
all_os = list(constants.OsType) | ||
all_auth_type = [str(type) for type in constants.ExcelAuthType.get_member_value__alias_map().values()] | ||
all_addressing = [str(type) for type in constants.CmdbAddressingType.get_member_value__alias_map().values()] | ||
all_enable_compression = ["True", "False"] | ||
|
||
# 生成excel模板 | ||
excel = Workbook() | ||
excel_sheet = excel.active | ||
excel_sheet.title = MAIN_SHEET_NAME | ||
|
||
excel_field: Dict[Any, str] = constants.ExcelField.get_member_value__alias_map() | ||
excel_field_list = list(excel_field.keys()) | ||
for col, key in enumerate(excel_field_list, start=1): | ||
title_row_cell = excel_sheet.cell(row=1, column=col, value=str(excel_field[key])) | ||
ExcelTools.set_font_style(title_row_cell, font_size=16, color="538DD5", bold=True) | ||
|
||
key_row_cell = excel_sheet.cell(row=2, column=col, value=str(key)) | ||
ExcelTools.set_font_style(key_row_cell, font_size=12, color="538DD5", bold=True) | ||
|
||
optional_row_cell = excel_sheet.cell(row=3, column=col, value=constants.EXCEL_TITLE_OPTIONAL[key]) | ||
if constants.EXCEL_TITLE_OPTIONAL[key] == constants.EXCEL_REQUIRED: | ||
ExcelTools.set_font_style(optional_row_cell, font_size=12, color="C0504D") | ||
else: | ||
ExcelTools.set_font_style(optional_row_cell, font_size=12, color="E26B0A") | ||
|
||
describe_row_cell = excel_sheet.cell(row=4, column=col, value=constants.EXCEL_TITLE_DESCRIBE[key]) | ||
ExcelTools.set_font_style(describe_row_cell, font_size=12, color="000000") | ||
|
||
if key == constants.ExcelField.OS_TYPE.value: | ||
ExcelTools.create_dropdown(excel, 5, col, key, MAIN_SHEET_NAME, all_os) | ||
elif key == constants.ExcelField.INSTALL_CHANNEL.value: | ||
ExcelTools.create_dropdown(excel, 5, col, key, MAIN_SHEET_NAME, all_install_channel) | ||
elif key == constants.ExcelField.AUTH_TYPE.value: | ||
ExcelTools.create_dropdown(excel, 5, col, key, MAIN_SHEET_NAME, all_auth_type) | ||
elif key == constants.ExcelField.BIZ.value: | ||
ExcelTools.create_dropdown(excel, 5, col, key, MAIN_SHEET_NAME, all_biz) | ||
elif key == constants.ExcelField.CLOUD.value: | ||
ExcelTools.create_dropdown(excel, 5, col, key, MAIN_SHEET_NAME, all_cloud) | ||
elif key == constants.ExcelField.AP.value: | ||
ExcelTools.create_dropdown(excel, 5, col, key, MAIN_SHEET_NAME, all_ap) | ||
elif key == constants.ExcelField.ADDRESS_TYPE.value: | ||
ExcelTools.create_dropdown(excel, 5, col, key, MAIN_SHEET_NAME, all_addressing) | ||
elif key == constants.ExcelField.DATA_COMPRESSION.value: | ||
ExcelTools.create_dropdown(excel, 5, col, key, MAIN_SHEET_NAME, all_enable_compression) | ||
else: | ||
pass | ||
|
||
ExcelTools.fill_color(excel_sheet, 1, 4, 1, len(excel_field_list), "D9D9D9") | ||
ExcelTools.adjust_row_height(excel_sheet, 1, 3, 20) | ||
ExcelTools.adjust_row_height(excel_sheet, 4, 4, 115) | ||
ExcelTools.adjust_col_width(excel_sheet, 1, len(excel_field_list), 35) | ||
ExcelTools.set_alignment(excel_sheet, "center", "left") | ||
|
||
return excel | ||
|
||
def analyze_excel(self, file: InMemoryUploadedFile) -> List[Dict]: | ||
|
||
# 解析excel | ||
excel = load_workbook(filename=file) | ||
excel_sheet = excel.active | ||
keys = [cell.value for cell in excel_sheet[2]] | ||
|
||
# 正则匹配处理 [id]name 类型的下拉框内容 | ||
pattern = r"\[(\d+)\]" | ||
|
||
# 获取加密ciper | ||
cipher = HostTools.get_asymmetric_cipher() | ||
|
||
required_list = [ | ||
key for key, value in constants.EXCEL_TITLE_OPTIONAL.items() if value == constants.EXCEL_REQUIRED | ||
] | ||
|
||
excel_data = [] | ||
for row in excel_sheet.iter_rows(min_row=5, values_only=True): | ||
row_data = {keys[i]: cell for i, cell in enumerate(row)} | ||
|
||
if ( | ||
row_data[constants.ExcelField.INNER_IPV4.value] is None | ||
and row_data[constants.ExcelField.INNER_IPV6.value] is None | ||
): | ||
raise ValueError("内网IPV4和内网IPV6不能同时为空") | ||
|
||
for key in required_list: | ||
if row_data[key] is None: | ||
raise ValueError(f"{key}不能为空") | ||
|
||
if row_data[constants.ExcelField.INSTALL_CHANNEL.value] is not None: | ||
install_channel = re.findall(pattern, row_data[constants.ExcelField.INSTALL_CHANNEL.value]) | ||
if not install_channel: | ||
raise ValueError("安装通道格式错误") | ||
row_data[constants.ExcelField.INSTALL_CHANNEL.value] = int(install_channel[0]) | ||
|
||
if row_data[constants.ExcelField.BIZ.value] is not None: | ||
biz = re.findall(pattern, row_data[constants.ExcelField.BIZ.value]) | ||
if not biz: | ||
raise ValueError("业务格式错误") | ||
row_data[constants.ExcelField.BIZ.value] = int(biz[0]) | ||
|
||
if row_data[constants.ExcelField.CLOUD.value] is not None: | ||
cloud = re.findall(pattern, row_data[constants.ExcelField.CLOUD.value]) | ||
if not cloud: | ||
raise ValueError("云区域格式错误") | ||
row_data[constants.ExcelField.CLOUD.value] = int(cloud[0]) | ||
|
||
if row_data[constants.ExcelField.AP.value] is not None: | ||
ap = re.findall(pattern, row_data[constants.ExcelField.AP.value]) | ||
if not ap: | ||
raise ValueError("接入点格式错误") | ||
row_data[constants.ExcelField.AP.value] = int(ap[0]) | ||
|
||
credentials: str = str(row_data[constants.ExcelField.CREDENTIALS.value]) | ||
if ( | ||
row_data[constants.ExcelField.AUTH_TYPE.value] | ||
== constants.ExcelAuthType.get_member_value__alias_map()[constants.ExcelAuthType.PASSWORD.value] | ||
): | ||
row_data[constants.ExcelField.AUTH_TYPE.value] = constants.ExcelAuthType.PASSWORD.value | ||
row_data["password"] = HostTools.encrypt_with_friendly_exc_handle(cipher, credentials, ValueError) | ||
else: | ||
row_data[constants.ExcelField.AUTH_TYPE.value] = constants.ExcelAuthType.KEY.value | ||
row_data["key"] = HostTools.encrypt_with_friendly_exc_handle(cipher, credentials, ValueError) | ||
|
||
del row_data[constants.ExcelField.CREDENTIALS.value] | ||
|
||
if ( | ||
row_data[constants.ExcelField.ADDRESS_TYPE.value] | ||
== constants.CmdbAddressingType.get_member_value__alias_map()[constants.CmdbAddressingType.STATIC.value] | ||
): | ||
row_data[constants.ExcelField.ADDRESS_TYPE.value] = constants.CmdbAddressingType.STATIC.value | ||
else: | ||
row_data[constants.ExcelField.ADDRESS_TYPE.value] = constants.CmdbAddressingType.DYNAMIC.value | ||
|
||
excel_data.append(row_data) | ||
|
||
res = {"host": excel_data} | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class ExcelDownloadSerializer(serializers.Serializer): | ||
pass | ||
|
||
|
||
class ExcelUploadSerializer(serializers.Serializer): | ||
file = serializers.FileField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
from typing import List | ||
|
||
from openpyxl import Workbook | ||
from openpyxl.cell.cell import Cell | ||
from openpyxl.styles import Alignment, Font, PatternFill | ||
from openpyxl.styles.fills import FILL_SOLID | ||
from openpyxl.worksheet.datavalidation import DataValidation | ||
from openpyxl.worksheet.worksheet import Worksheet | ||
|
||
DEFAULT_DROP_DOWN_ROW = 1000 | ||
|
||
|
||
class ExcelTools: | ||
@classmethod | ||
def fill_color( | ||
cls, | ||
excel: Worksheet, | ||
start_row: int, | ||
end_row: int, | ||
start_col: int, | ||
end_col: int, | ||
color: str, | ||
fill_type: str = FILL_SOLID, | ||
): | ||
fill = PatternFill(start_color=color, end_color=color, fill_type=fill_type) | ||
|
||
for row in range(start_row, end_row + 1): | ||
for col in range(start_col, end_col + 1): | ||
excel.cell(row=row, column=col).fill = fill | ||
|
||
@classmethod | ||
def create_dropdown( | ||
cls, excel: Workbook, start_row: int, col: int, src_sheet: str, dst_sheet: str, options: List[str] | ||
): | ||
sheet = excel.create_sheet(title=src_sheet) | ||
main_sheet = excel[dst_sheet] | ||
for i, option in enumerate(options, start=1): | ||
sheet[f"A{i}"] = option | ||
|
||
dv = DataValidation(type="list", formula1=f"={src_sheet}!$A$1:$A${len(options)}", allow_blank=True) | ||
|
||
# 默认提供1000行数据下拉 | ||
main_sheet.add_data_validation(dv) | ||
dv.ranges.append(f"{chr(64 + col)}{start_row}:{chr(64 + col)}{DEFAULT_DROP_DOWN_ROW}") | ||
|
||
@classmethod | ||
def adjust_row_height(cls, excel: Worksheet, start_row: int, end_row: int, height: float): | ||
for row in range(start_row, end_row + 1): | ||
excel.row_dimensions[row].height = height | ||
|
||
@classmethod | ||
def adjust_col_width(cls, excel: Worksheet, start_col: int, end_col: int, width: float): | ||
for col in range(start_col, end_col + 1): | ||
excel.column_dimensions[chr(64 + col)].width = width | ||
|
||
@classmethod | ||
def set_alignment(cls, excel: Worksheet, vertical: str, horizontal: str): | ||
alignment = Alignment(wrap_text=True, vertical=vertical, horizontal=horizontal) | ||
for row in excel.iter_rows(): | ||
for cell in row: | ||
cell.alignment = alignment | ||
|
||
@classmethod | ||
def set_font_style( | ||
cls, | ||
cell: Cell, | ||
font_size: int, | ||
color: str = "000000", | ||
name: str = "SimSun", | ||
bold: bool = False, | ||
italic: bool = False, | ||
strike: bool = False, | ||
): | ||
font = Font(size=font_size, color=color, name=name, bold=bold, italic=italic, strike=strike) | ||
cell.font = font |
Oops, something went wrong.