Skip to content

Commit

Permalink
新增excel格式的导入导出
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Nov 30, 2023
1 parent ce946b5 commit 699459e
Show file tree
Hide file tree
Showing 14 changed files with 402 additions and 91 deletions.
3 changes: 2 additions & 1 deletion domain_admin/api/domain_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ def export_domain_file():
group_ids = request.json.get('group_ids')
expire_days = request.json.get('expire_days')
role = request.json.get('role')
ext = request.json.get('ext', 'csv')

order_prop = request.json.get('order_prop') or 'expire_days'
order_type = request.json.get('order_type') or 'ascending'
Expand Down Expand Up @@ -408,7 +409,7 @@ def export_domain_file():

group_service.load_group_name(lst)

filename = domain_service.export_domain_to_file(lst)
filename = domain_service.export_domain_to_file(rows=lst, ext=ext)

return {
'name': filename,
Expand Down
7 changes: 4 additions & 3 deletions domain_admin/api/domain_info_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ def export_domain_info_file():
group_ids = request.json.get('group_ids')
domain_expire_days = request.json.get('domain_expire_days')
role = request.json.get('role')
ext = request.json.get('ext', 'csv')

order_prop = request.json.get('order_prop') or 'domain_expire_days'
order_type = request.json.get('order_type') or 'ascending'
Expand All @@ -358,7 +359,7 @@ def export_domain_info_file():
}

# 列表数据
query = domain_info_service.get_domain_inf_query(**params)
query = domain_info_service.get_domain_info_query(**params)

ordering = domain_info_service.get_ordering(order_prop=order_prop, order_type=order_type)

Expand All @@ -379,7 +380,7 @@ def export_domain_info_file():
# 分组名
group_service.load_group_name(lst)

filename = domain_info_service.export_domain_to_file(lst)
filename = domain_info_service.export_domain_to_file(ext=ext, rows=lst)

return {
'name': filename,
Expand Down Expand Up @@ -429,7 +430,7 @@ def get_domain_info_list():
}

# 列表数据
query = domain_info_service.get_domain_inf_query(**params)
query = domain_info_service.get_domain_info_query(**params)

total = query.count()

Expand Down
41 changes: 41 additions & 0 deletions domain_admin/model/domain_info_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,44 @@ def tags(self, value):
def tags_str(self):
if self.tags:
return '、'.join(self.tags)


# 数据导入导出字段关系
FIELD_MAPPING = [
{
'name': '域名',
'field': 'domain',
},
{
'name': '注册时间',
'field': 'domain_start_date',
},
{
'name': '到期时间',
'field': 'domain_expire_date',
},
{
'name': '剩余天数',
'field': 'real_domain_expire_days',
},
{
'name': '分组',
'field': 'group_name',
},
{
'name': '标签',
'field': 'tags_str',
},
{
'name': '主办单位名称',
'field': 'icp_company',
},
{
'name': 'ICP备案/许可证号',
'field': 'icp_licence',
},
{
'name': '备注',
'field': 'comment',
},
]
33 changes: 33 additions & 0 deletions domain_admin/model/domain_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,36 @@ def expire_status(self):
return None
else:
return False


# 数据导入导出字段关系
FIELD_MAPPING = [
{
'name': '域名',
'field': 'domain',
},
{
'name': '端口',
'field': 'port',
},
{
'name': '证书颁发时间',
'field': 'start_date',
},
{
'name': '证书过期时间',
'field': 'expire_date',
},
{
'name': '证书天数',
'field': 'real_time_expire_days',
},
{
'name': '分组',
'field': 'group_name',
},
{
'name': '备注',
'field': 'alias',
},
]
65 changes: 25 additions & 40 deletions domain_admin/service/domain_info_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from datetime import datetime, timedelta
import random

from domain_admin.model import domain_info_model
from peewee import chunked

from domain_admin.enums.role_enum import RoleEnum
Expand All @@ -19,7 +20,7 @@
from domain_admin.model.group_model import GroupModel
from domain_admin.model.group_user_model import GroupUserModel
from domain_admin.service import render_service, file_service, group_service, async_task_service
from domain_admin.utils import whois_util, datetime_util, domain_util, icp_util
from domain_admin.utils import whois_util, datetime_util, domain_util, icp_util, file_util


def add_domain_info(
Expand Down Expand Up @@ -235,68 +236,52 @@ def add_domain_from_file(filename, user_id):
"""
# logger.info('user_id: %s, filename: %s', user_id, filename)

lst = list(domain_util.parse_domain_from_file(filename))
lst = list(domain_util.parse_domain_from_file(filename, domain_info_model.FIELD_MAPPING))

# 导入分组
group_name_list = [item.group_name for item in lst]
group_map = group_service.get_or_create_group_map(group_name_list, user_id)
group_name_list = [item.get('group_name') for item in lst if item.get('group_name')]
if group_name_list:
group_map = group_service.get_or_create_group_map(group_name_list, user_id)
else:
group_map = {}

lst = [
{
'domain': item.root_domain,
'comment': item.alias,
'domain': item['domain'],
'comment': item.get('comment'),
'group_id': group_map.get(item.get('group_name'), 0),
'tags_raw': json.dumps(item.get('tags'), ensure_ascii=False),
'user_id': user_id,
'group_id': group_map.get(item.group_name, 0),
'tags_raw': json.dumps(item.tags, ensure_ascii=False)
} for item in lst if item.root_domain
} for item in lst if item.get('root_domain')
]

for batch in chunked(lst, 500):
DomainInfoModel.insert_many(batch).on_conflict_ignore().execute()


def export_domain_to_file(rows):
def export_domain_to_file(rows, ext):
"""
导出域名到文件
:param ext: 导出格式
:param rows:
:return:
"""
# 域名数据
# rows = DomainInfoModel.select().where(
# DomainInfoModel.user_id == user_id
# ).order_by(
# DomainInfoModel.domain_expire_time.asc(),
# DomainInfoModel.id.desc(),
# )
#
# # 分组数据
# group_rows = GroupModel.select(
# GroupModel.id,
# GroupModel.name,
# ).where(
# GroupModel.user_id == user_id
# )
#
# group_map = {row.id: row.name for row in group_rows}
#
# lst = []
# for row in list(rows):
# row.group_name = group_map.get(row.group_id, '')
# lst.append(row)

content = render_service.render_template('domain-export.csv', {'list': rows})

filename = datetime.now().strftime("domain_%Y%m%d%H%M%S") + '.csv'

filename = datetime.now().strftime("domain_%Y%m%d%H%M%S") + '.' + ext
temp_filename = file_service.resolve_temp_file(filename)
# print(temp_filename)
with io.open(temp_filename, 'w', encoding='utf-8') as f:
f.write(content)

if ext == 'txt':
lst = [row['domain'] for row in rows]
else:
lst = file_util.convert_to_export(rows, domain_info_model.FIELD_MAPPING)

# content = render_service.render_template('domain-export.csv', {'list': rows})
file_util.write_data_to_file(temp_filename, lst)

return filename


def get_domain_inf_query(keyword, group_ids, domain_expire_days, role, user_id):
def get_domain_info_query(keyword, group_ids, domain_expire_days, role, user_id):
user_group_ids = None

if role == RoleEnum.ADMIN:
Expand Down
66 changes: 29 additions & 37 deletions domain_admin/service/domain_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from domain_admin.enums.role_enum import RoleEnum
from domain_admin.log import logger
from domain_admin.model import domain_model
from domain_admin.model.address_model import AddressModel
from domain_admin.model.domain_info_model import DomainInfoModel
from domain_admin.model.domain_model import DomainModel
Expand All @@ -21,7 +22,7 @@
from domain_admin.model.user_model import UserModel
from domain_admin.service import file_service, async_task_service
from domain_admin.service import render_service, group_service
from domain_admin.utils import datetime_util, cert_util
from domain_admin.utils import datetime_util, cert_util, file_util
from domain_admin.utils import domain_util
from domain_admin.utils.cert_util import cert_socket_v2, cert_openssl_v2
from domain_admin.utils.flask_ext.app_exception import ForbiddenAppException
Expand Down Expand Up @@ -364,20 +365,23 @@ def auto_import_from_domain(root_domain, group_id=0, user_id=0):
def add_domain_from_file(filename, user_id):
logger.info('user_id: %s, filename: %s', user_id, filename)

lst = list(domain_util.parse_domain_from_file(filename))
lst = list(domain_util.parse_domain_from_file(filename, domain_model.FIELD_MAPPING))

# 导入分组
group_name_list = [item.group_name for item in lst]
group_map = group_service.get_or_create_group_map(group_name_list, user_id)
group_name_list = [item.get('group_name') for item in lst if item.get('group_name')]
if group_name_list:
group_map = group_service.get_or_create_group_map(group_name_list, user_id)
else:
group_map = {}

lst = [
{
'domain': item.domain,
'root_domain': item.root_domain,
'port': item.port,
'alias': item.alias,
'domain': item['domain'],
'root_domain': domain_util.get_root_domain(item['domain']),
'port': item.get('port'),
'alias': item.get('alias', ''),
'user_id': user_id,
'group_id': group_map.get(item.group_name, 0),
'group_id': group_map.get(item.get('group_name'), 0),
} for item in lst
]

Expand All @@ -387,40 +391,29 @@ def add_domain_from_file(filename, user_id):
DomainModel.insert_many(batch).on_conflict_ignore().execute()


def export_domain_to_file(rows):
def export_domain_to_file(rows, ext):
"""
导出域名到文件
:param rows:
:return:
"""
# # 域名数据
# rows = DomainModel.select().where(
# DomainModel.user_id == user_id
# ).order_by(
# DomainModel.expire_days.asc(),
# DomainModel.id.desc(),
# )
#
# # 分组数据
# group_rows = GroupModel.select().where(
# GroupModel.user_id == user_id
# )
#
# group_map = {row.id: row.name for row in group_rows}
#
# lst = []
# for row in list(rows):
# row.group_name = group_map.get(row.group_id, '')
# lst.append(row)

content = render_service.render_template('cert-export.csv', {'list': rows})

filename = datetime.now().strftime("cert_%Y%m%d%H%M%S") + '.csv'

# content = render_service.render_template('cert-export.csv', {'list': rows})

filename = datetime.now().strftime("cert_%Y%m%d%H%M%S") + '.' + ext
temp_filename = file_service.resolve_temp_file(filename)
# print(temp_filename)
with io.open(temp_filename, 'w', encoding='utf-8') as f:
f.write(content)

if ext == 'txt':
lst = [row['domain'] for row in rows]
else:
lst = file_util.convert_to_export(rows, domain_model.FIELD_MAPPING)

file_util.write_data_to_file(temp_filename, lst)

# temp_filename = file_service.resolve_temp_file(filename)
# # print(temp_filename)
# with io.open(temp_filename, 'w', encoding='utf-8') as f:
# f.write(content)

return filename

Expand Down Expand Up @@ -477,7 +470,6 @@ def load_address_count(lst):


def get_domain_list_query(keyword, group_id, group_ids, expire_days, user_id, role):

user_group_ids = None

if role == RoleEnum.ADMIN:
Expand Down
2 changes: 1 addition & 1 deletion domain_admin/service/file_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_temp_filename(ext):

def save_temp_file(update_file):
"""保存上传的文件"""
ext = update_file.filename.split('.')[-1]
ext = file_util.get_filename_ext(update_file.filename)
filename = get_temp_filename(ext)
update_file.save(filename)
return filename
Expand Down
Loading

0 comments on commit 699459e

Please sign in to comment.