urban-investment-research/init.py

47 lines
1.5 KiB
Python
Raw Normal View History

2023-03-23 16:08:39 +08:00
# 初始化数据创建、只需要调用一次的接口
import json
from Context.common import common_db
from Mods.CommonInformation.Mods.IndustryMain.Models import IndustryMain
from Mods.CommonInformation.Mods.ProductMain.Models import ProductMain
from Utils.PrintUtils import print_error
from Utils.SqlAlchemyUtils import tree_table_to_json
def save_industry_main_json():
"""
保存行业分类数据
"""
db = common_db.get_db_i()
try:
2023-04-06 15:41:47 +08:00
tree = []
for item in db.query(IndustryMain).filter_by(type='一级行业').all():
tree.append({"text": item.name, "value": item.code})
# tree = tree_table_to_json(db, IndustryMain, key_str='code')
json.dump({'data': tree}, open('static/industry_main_use.json', 'w', encoding="utf-8"), ensure_ascii=False, )
2023-03-23 16:08:39 +08:00
except Exception as e:
print_error(e)
finally:
db.close()
def save_product_main_json():
"""
保存产品分类数据
"""
db = common_db.get_db_i()
try:
2023-04-06 15:41:47 +08:00
# tree = []
# for item in db.query(ProductMain).filter_by(type='一级分类').all():
# tree.append({"text": item.name, "value": item.code})
2023-03-23 16:08:39 +08:00
tree = tree_table_to_json(db, ProductMain, key_str='code')
2023-04-06 15:41:47 +08:00
# json.dump({'data': tree}, open('static/product_main_use.json', 'w', encoding="utf-8"), ensure_ascii=False, )
2023-03-23 16:08:39 +08:00
except Exception as e:
print_error(e)
finally:
db.close()
save_industry_main_json()
save_product_main_json()