From 8456abe1a6b373a61533b70c7332c1296e93667c Mon Sep 17 00:00:00 2001 From: hsafaei Date: Mon, 2 Feb 2026 19:25:35 +0330 Subject: [PATCH] 1 --- app/common/general_functions.py | 30 +++++++ app/monir_gn_tree.py | 154 ++++++++++++++++++++++++++++---- app/monir_options.py | 11 +++ 3 files changed, 179 insertions(+), 16 deletions(-) diff --git a/app/common/general_functions.py b/app/common/general_functions.py index f40151e..e9ade89 100644 --- a/app/common/general_functions.py +++ b/app/common/general_functions.py @@ -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("فصل اول - سیاستها و اصول") # print('prefix :', prefix) # print('title :', title) \ No newline at end of file diff --git a/app/monir_gn_tree.py b/app/monir_gn_tree.py index 196d360..76a56ea 100644 --- a/app/monir_gn_tree.py +++ b/app/monir_gn_tree.py @@ -28,11 +28,11 @@ load_dotenv(dotenv_path="config.env") # host = os.getenv("HOST_160_LOCAL") # password = os.getenv("HOST_160_PASS") -# host = os.getenv("HOST_60") -# password = os.getenv("HOST_60_PASS") +host = os.getenv("HOST_60") +password = os.getenv("HOST_60_PASS") -host = os.getenv("HOST_133") -password = os.getenv("HOST_133_PASS") +# host = os.getenv("HOST_133") +# password = os.getenv("HOST_133_PASS") # print(host) # print(password) @@ -41,6 +41,9 @@ password = os.getenv("HOST_133_PASS") path_base_map = ( "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) text_normalize = TextNormalizer() @@ -174,7 +177,7 @@ def import_project_to_elastic( "tree_key": main_type_key, "tree_label": project_title, "main_type": "tree", - # "group_key" : "", + "group_key" : "موضوعی", "title": title, "parent_id": parent_id, "child_order": child_order, @@ -205,7 +208,7 @@ def import_project_to_elastic( print(t2 - t1) -def import_tlist_projects(): +def import_tlist_projects(index_name_o="gn_tree"): config_env = load_dotenv(dotenv_path="./config.env") MYSQL_username = os.getenv("MYSQL_username") @@ -247,7 +250,7 @@ def import_tlist_projects(): for item in list_projects: print("*" * 100) 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() @@ -256,7 +259,7 @@ def import_option_to_elastic( elastic, main_key, main_label, - group_key="monir_base", + group_key="عناوین انتخابی برنامه", eid="", 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) -def import_option_base_list(elastic): +def import_option_base_list(elastic, index_name_o="gn_option"): op_list = [ { "main_key": "calendar_main_type", @@ -293,6 +296,10 @@ def import_option_base_list(elastic): "main_key": "calendar_domain", "main_label": "تقویم - حوزه درخواست", }, + { + "main_key": "calendar_audience", + "main_label": "تقویم - نوع مخاطبان", + }, { "main_key": "calendar_format", "main_label": "تقویم - قالب جلسه", @@ -346,7 +353,7 @@ def import_option_base_list(elastic): group_key = op["group_key"] 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)) ostan_dic={} + ostan_list = [] number = 0 for line in lines: 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) if ostan not in ostan_dic: + ostan_list.append(ostan) ostan_dic[ostan] = [] 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 + for key, value in ostan_dic.items(): number = number + 1 @@ -387,7 +412,7 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"): shahrs = value eid = 'iros_' + str(number) - main_key = "iros_" + str(number) + main_key = str(ostan) main_label = "شهرهای استان " + ostan group_key = "استان و شهرهای ایران" main_type = "list_simple" @@ -403,15 +428,112 @@ 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__": - # ---مرحله اول - # import_tlist_projects() + ###### -------------- gn_tree ----------------------------- - # import_option_base_list(elastic) + # import_tlist_projects("gn_tree") + + # file_path = r"app/../data/monir/041106_لیست تمام موضوعات.xlsx" + # import_excel_tree_monir(elastic, file_path, "gn_tree") - # import_option_ostan_shahr(elastic) + + ###### -------------- 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_steps_temp", "mn_steps") # elastic.backupIndexToZipfile("G:/Project2/111", "gn_calendar", "gn_calendar") diff --git a/app/monir_options.py b/app/monir_options.py index db12f44..ad3a220 100644 --- a/app/monir_options.py +++ b/app/monir_options.py @@ -21,6 +21,17 @@ def get_public_options(key, add_all_item=False): {"value": "نظام"}, {"value": "عمومی"}, ] + elif key == "calendar_audience": + options = [ + {"value": "حوزوی"}, + {"value": "دانشگاهی"}, + {"value": "اجرایی - نظام"}, + {"value": "عمومی"}, + {"value": "فرهنگستان"}, + {"value": "دفتر"}, + {"value": "درایه"}, + {"value": "سایر سازمانها"}, + ] elif key == "calendar_format": options = [ {"value": "منبر", "title": "منبر"},