This commit is contained in:
hsafaei
2026-02-02 19:25:35 +03:30
parent 7962b6767e
commit 8456abe1a6
3 changed files with 179 additions and 16 deletions
+30
View File
@@ -1295,6 +1295,36 @@ def excel_to_json(excel_file, required_columns=None, sheet_name=0):
def extract_excel_fields(file_path: str, fields: list = [], sheet_name: str = None):
"""
فایل اکسل را باز کرده و فقط فیلدهای خواسته‌شده را به‌صورت لیست از دیکشنری‌ها برمی‌گرداند.
:param file_path: مسیر فایل اکسل (.xlsx یا .xls)
:param fields: لیست نام ستون‌هایی که باید خوانده شوند
:return: لیستی از دیکشنری‌ها شامل داده‌های ستون‌های انتخاب‌شده
"""
try:
# خواندن فایل
df = pd.read_excel(file_path, sheet_name=sheet_name)
df.columns = df.columns.map(str)
if len(fields) > 0:
missing = [f for f in fields if f not in df.columns]
if missing:
print(f"⚠️ This columns mot found in file {missing}")
# for i in df.columns:
# print(f'-field ->{i}<-')
df = df[[f for f in fields if f in df.columns]]
# تبدیل به لیست از دیکشنری‌ها
data = df.to_dict(orient="records")
return data
except Exception as e:
print(f"❌ خطا در خواندن فایل اکسل: {e}")
return []
# prefix, title = normalHeadingTitle("فصل اول - سیاستها و اصول") # prefix, title = normalHeadingTitle("فصل اول - سیاستها و اصول")
# print('prefix :', prefix) # print('prefix :', prefix)
# print('title :', title) # print('title :', title)
+137 -15
View File
@@ -28,11 +28,11 @@ load_dotenv(dotenv_path="config.env")
# host = os.getenv("HOST_160_LOCAL") # host = os.getenv("HOST_160_LOCAL")
# password = os.getenv("HOST_160_PASS") # password = os.getenv("HOST_160_PASS")
# host = os.getenv("HOST_60") host = os.getenv("HOST_60")
# password = os.getenv("HOST_60_PASS") password = os.getenv("HOST_60_PASS")
host = os.getenv("HOST_133") # host = os.getenv("HOST_133")
password = os.getenv("HOST_133_PASS") # password = os.getenv("HOST_133_PASS")
# print(host) # print(host)
# print(password) # print(password)
@@ -41,6 +41,9 @@ password = os.getenv("HOST_133_PASS")
path_base_map = ( path_base_map = (
"G:\Project2\gateaway_py\elastic_backend/elastic_backend/app/schemas/mappings/" "G:\Project2\gateaway_py\elastic_backend/elastic_backend/app/schemas/mappings/"
) )
# path_base_map = (
# "G:\Project2\tavasi_60\src\repo\_other/"
# )
elastic = ElasticHelper(host, password, "elastic", path_base_map) elastic = ElasticHelper(host, password, "elastic", path_base_map)
text_normalize = TextNormalizer() text_normalize = TextNormalizer()
@@ -174,7 +177,7 @@ def import_project_to_elastic(
"tree_key": main_type_key, "tree_key": main_type_key,
"tree_label": project_title, "tree_label": project_title,
"main_type": "tree", "main_type": "tree",
# "group_key" : "", "group_key" : "موضوعی",
"title": title, "title": title,
"parent_id": parent_id, "parent_id": parent_id,
"child_order": child_order, "child_order": child_order,
@@ -205,7 +208,7 @@ def import_project_to_elastic(
print(t2 - t1) print(t2 - t1)
def import_tlist_projects(): def import_tlist_projects(index_name_o="gn_tree"):
config_env = load_dotenv(dotenv_path="./config.env") config_env = load_dotenv(dotenv_path="./config.env")
MYSQL_username = os.getenv("MYSQL_username") MYSQL_username = os.getenv("MYSQL_username")
@@ -247,7 +250,7 @@ def import_tlist_projects():
for item in list_projects: for item in list_projects:
print("*" * 100) print("*" * 100)
print(item) print(item)
import_project_to_elastic(cursor, elastic, item["id"], item["title"], "gn_tree") import_project_to_elastic(cursor, elastic, item["id"], item["title"], index_name_o)
cursor.close() cursor.close()
@@ -256,7 +259,7 @@ def import_option_to_elastic(
elastic, elastic,
main_key, main_key,
main_label, main_label,
group_key="monir_base", group_key="عناوین انتخابی برنامه",
eid="", eid="",
index_name_o="gn_option", index_name_o="gn_option",
): ):
@@ -279,7 +282,7 @@ def import_option_to_elastic(
elastic.es.index(index=index_name_o, id=eid, document=data) elastic.es.index(index=index_name_o, id=eid, document=data)
def import_option_base_list(elastic): def import_option_base_list(elastic, index_name_o="gn_option"):
op_list = [ op_list = [
{ {
"main_key": "calendar_main_type", "main_key": "calendar_main_type",
@@ -293,6 +296,10 @@ def import_option_base_list(elastic):
"main_key": "calendar_domain", "main_key": "calendar_domain",
"main_label": "تقویم - حوزه درخواست", "main_label": "تقویم - حوزه درخواست",
}, },
{
"main_key": "calendar_audience",
"main_label": "تقویم - نوع مخاطبان",
},
{ {
"main_key": "calendar_format", "main_key": "calendar_format",
"main_label": "تقویم - قالب جلسه", "main_label": "تقویم - قالب جلسه",
@@ -346,7 +353,7 @@ def import_option_base_list(elastic):
group_key = op["group_key"] group_key = op["group_key"]
import_option_to_elastic( import_option_to_elastic(
elastic, op["main_key"], op["main_label"], group_key, eid elastic, op["main_key"], op["main_label"], group_key, eid, index_name_o
) )
@@ -361,6 +368,7 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"):
print(len(lines)) print(len(lines))
ostan_dic={} ostan_dic={}
ostan_list = []
number = 0 number = 0
for line in lines: for line in lines:
items = line.strip('\n').split('\t') items = line.strip('\n').split('\t')
@@ -374,12 +382,29 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"):
shahr = text_normalize.normalize_qanon(shahr) shahr = text_normalize.normalize_qanon(shahr)
if ostan not in ostan_dic: if ostan not in ostan_dic:
ostan_list.append(ostan)
ostan_dic[ostan] = [] ostan_dic[ostan] = []
ostan_dic[ostan].append(shahr) ostan_dic[ostan].append(shahr)
eid = 'iros_0'
main_key = "ir_ostan"
main_label = "استانهای ایران"
group_key = "استان و شهرهای ایران"
main_type = "list_simple"
data = {
"id": eid,
"version_key": "b1",
"main_key": main_key,
"main_label": main_label,
"group_key": group_key,
"titles": ostan_list,
}
elastic.es.index(index=index_name_o, id=eid, document=data)
number = 0 number = 0
for key, value in ostan_dic.items(): for key, value in ostan_dic.items():
number = number + 1 number = number + 1
@@ -387,7 +412,7 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"):
shahrs = value shahrs = value
eid = 'iros_' + str(number) eid = 'iros_' + str(number)
main_key = "iros_" + str(number) main_key = str(ostan)
main_label = "شهرهای استان " + ostan main_label = "شهرهای استان " + ostan
group_key = "استان و شهرهای ایران" group_key = "استان و شهرهای ایران"
main_type = "list_simple" main_type = "list_simple"
@@ -403,14 +428,111 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"):
##### -------------------------------------------------------------
def convert_excel_data_to_json(item):
_id = "gnp" + str(item["id"])
tree_label = item["person_title"]
person_id = item["person_id"]
title = item["title"]
group_key = ""
if person_id == 1103 :
group_key = "استاد حسینی"
tree_label = "فهرست موضوعی جلسات استاد حسینی"
elif person_id == 1128 :
group_key = "استاد میرباقری"
tree_label = "فهرست موضوعی جلسات استاد میرباقری"
papa_id = item["papa_id"]
if papa_id == 0:
parent_id = str(0)
else:
parent_id = "gnp" + str(papa_id)
child_order = item["order"]
return {
"id": _id, # "gnt01741"
"version_key": "portal_2",
"tree_key": "tree" + str(person_id),
"tree_label": tree_label, # "فهرست موضوعات مسائل و پاسخها",
"group_key": group_key,
"main_type": "tree",
"title": title, # "پیامدهای روبنایی",
"parent_id": parent_id,
"child_order": child_order,
"tags": [],
"meta_info": {},
}
def import_excel_tree_monir(elastic, file_path, index_name_o = "gn_tree"):
data_excel = extract_excel_fields(file_path, sheet_name="Sheet1")
print(f'data_excel {len(data_excel)}')
try:
f_data = []
for i in data_excel:
f_data.append(convert_excel_data_to_json(i))
except Exception as e:
print(
f"*************",
)
traceback.print_exc()
bulk_data=[]
total_count = 0
data_list = f_data
i = 0
for data in data_list :
eid = data["id"]
i = i + 1
if len(bulk_data) >= 100:
total_count += len(bulk_data)
print('=' * 10, 'to_elastic , total='+str(total_count) , str(i)+ ', id=' + eid )
elastic.importBulkList(bulk_data, index_name_o)
bulk_data = []
bulk_data.append(
{
'_id' : eid,
'_source' : data
}
)
if len(bulk_data) > 0:
total_count += len(bulk_data)
print('=' * 10, ' to_elastic , total='+str(total_count) )
elastic.importBulkList(bulk_data, index_name_o)
bulk_data = []
print("end success import_to_elastic")
##### -------------------------------------------------------------
if __name__ == "__main__": if __name__ == "__main__":
# ---مرحله اول ###### -------------- gn_tree -----------------------------
# import_tlist_projects()
# import_option_base_list(elastic) # import_tlist_projects("gn_tree")
# import_option_ostan_shahr(elastic) # file_path = r"app/../data/monir/041106_لیست تمام موضوعات.xlsx"
# import_excel_tree_monir(elastic, file_path, "gn_tree")
###### -------------- gn_option -----------------------------
# import_option_base_list(elastic, "gn_option")
# import_option_ostan_shahr(elastic, "gn_option")
###### -------------- -----------------------------
# elastic.backupIndexToZipfile("G:/Project2/111", "mn_sanad_temp", "mn_sanad") # elastic.backupIndexToZipfile("G:/Project2/111", "mn_sanad_temp", "mn_sanad")
# elastic.backupIndexToZipfile("G:/Project2/111", "mn_steps_temp", "mn_steps") # elastic.backupIndexToZipfile("G:/Project2/111", "mn_steps_temp", "mn_steps")
+11
View File
@@ -21,6 +21,17 @@ def get_public_options(key, add_all_item=False):
{"value": "نظام"}, {"value": "نظام"},
{"value": "عمومی"}, {"value": "عمومی"},
] ]
elif key == "calendar_audience":
options = [
{"value": "حوزوی"},
{"value": "دانشگاهی"},
{"value": "اجرایی - نظام"},
{"value": "عمومی"},
{"value": "فرهنگستان"},
{"value": "دفتر"},
{"value": "درایه"},
{"value": "سایر سازمانها"},
]
elif key == "calendar_format": elif key == "calendar_format":
options = [ options = [
{"value": "منبر", "title": "منبر"}, {"value": "منبر", "title": "منبر"},