move tanghih codes

This commit is contained in:
hsafaei
2025-04-05 15:42:26 +03:30
parent 3018c08e38
commit c84c635055
10 changed files with 644 additions and 422 deletions
+355
View File
@@ -0,0 +1,355 @@
import json
import requests
from config_base import *
import requests
from html import escape
import json
from decimal import Decimal
import datetime
from config_base import *
from general_functions import *
from text_normalizer import TextNormalizer
# این برنامه یک بار فقط جهت انتقال کدهای دیتابیس آقای شاهنوش به دیتابیس نهایی قوانین انجام شد
# تکرار لازم نیست
# در بقیه حالات اگر کدها تغییر نکرده باشد در خود الاستیک بروز رسانی می شود
#cursor, cnxn = create_cursor('db4minaei')
def import_tanghih_plan_codes(cursor):
plan1 = {}
query = """select * from dbo.[lwplan] where [f_parentid]=766 order by [ordno] """
cursor.execute(query)
i = 0
while 1:
i=i+1
row = cursor.fetchone()
if not row:
break
plan1[row.ID] = normalYehKe(row.caption)
# print(plan1)
plan2 = {}
query = """select * from dbo.[lwplan] where [f_parentid]=21185 order by [ordno] """
cursor.execute(query)
i = 0
while 1:
i=i+1
row = cursor.fetchone()
if not row:
break
plan2[row.ID] = normalYehKe(row.caption)
# print(plan2)
# #########################################################
query = '''
SELECT [f_lwplanid]
,[f_lwlawsecid]
,[planstate]
,[ID]
FROM [db4minaei].[dbo].[lwlawplan]
where f_lwlawsecid is not null and f_lwlawsecid > 0
order by f_lwlawsecid, planstate, f_lwplanid
'''
cursor.execute(query)
text_normal = TextNormalizer()
url = BASE_URL + "repo/majles/section/multi/update"
headers = HEADERS
total_count = 0
bulk_data = []
previd = ''
tcode_main_old = []
tcode_sub_old = []
tcode_main = []
tcode_sub = []
prev_sectionid = 0
prev_planid = 0
prev_planstate = 0
prev2_sectionid = 0
prev2_planid = 0
prev2_planstate = 0
t1= datetime.datetime.now()
print(t1)
i = 0
while 1:
# print(i)
i=i+1
row = cursor.fetchone()
if not row:
break
# if i > 3:
# break
# print(row.sectionId)
sectionid = row.f_lwlawsecid
planid = row.f_lwplanid
planstate = row.planstate
if prev_sectionid == 0 :
prev_sectionid = sectionid
prev_planid = planid
prev_planstate = planstate
continue
if sectionid == prev_sectionid and planid == prev_planid and planstate == prev_planstate :
continue
if sectionid == prev_sectionid :
if prev_planid in plan1 :
if prev_planstate == 1 :
tcode_main_old.append(plan1[prev_planid])
else :
tcode_sub_old.append(plan1[prev_planid])
elif prev_planid in plan2 :
if prev_planstate == 1 :
tcode_main.append(plan2[prev_planid])
else :
tcode_sub.append(plan2[prev_planid])
prev_planid = planid
prev_planstate = planstate
continue
eid='qs'+ str(prev_sectionid)
data={
'id' : eid,
'tcode_main' : tcode_main,
'tcode_sub' : tcode_sub,
'tcode_main_old' : tcode_main_old,
'tcode_sub_old' : tcode_sub_old,
}
# print(eid)
bulk_data.append(data)
prev_sectionid = sectionid
tcode_main_old = []
tcode_sub_old = []
tcode_main = []
tcode_sub = []
if len(bulk_data) >= 100:
total_count += len(bulk_data)
print('=' * 10, ', total='+str(total_count) , str(i)+ ', id=' + eid)
payload = json.dumps(bulk_data, cls=JSONEncoder) #Works!
# limit POST Content-Length 8388608
pops_data = []
while len(payload) > 8388608 :
el = bulk_data.pop()
total_count -= 1
pops_data.append(el)
payload = json.dumps(bulk_data, cls=JSONEncoder) #Works!
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code == 500 :
print(response.text)
bulk_data = []
break
bulk_data = pops_data
# print(response)
# print(response.text)
# print(row.txt)
# /////////////////
prev2_sectionid = prev_sectionid
prev2_planid = prev_planid
prev2_planstate = prev_planstate
prev_sectionid = sectionid
prev_planid = planid
prev_planstate = planstate
# if prev2_sectionid == prev_sectionid :
if prev_planid in plan1 :
if prev_planstate == 1 :
tcode_main_old.append(plan1[prev_planid])
else :
tcode_sub_old.append(plan1[prev_planid])
elif prev_planid in plan2 :
if prev_planstate == 1 :
tcode_main.append(plan2[prev_planid])
else :
tcode_sub.append(plan2[prev_planid])
# else :
eid='qs'+ str(prev_sectionid)
data={
'id' : eid,
'tcode_main' : tcode_main,
'tcode_sub' : tcode_sub,
'tcode_main_old' : tcode_main_old,
'tcode_sub_old' : tcode_sub_old,
}
bulk_data.append(data)
if len(bulk_data) > 0:
total_count += len(bulk_data)
print('=' * 10, ', total='+str(total_count) , str(i)+ ', id=' + eid)
payload = json.dumps(bulk_data, cls=JSONEncoder) #Works!
response = requests.request("POST", url, headers=headers, data=payload)
# print(response.text)
t2= datetime.datetime.now()
print("finished time: " + str(t2))
print("process time: " + str(t2-t1))
print(' end success lawreg_lwphase_info ')
def import_tangih_tree_to_list(cursor):
# -----------------------------------------------------------------
# BASE_URL = "https://api.tavasi.ir/list/"
# ADD_LIST_ROUTE = "project/add"
# ADD_ITEM_ROUTE = "subject/add"
# TITLE = "فهرست موضوعات قانون"
# TYPE = 3
# ADD_LIST_QUERY = {"title": TITLE, "type": TYPE, "item_state": 1}
GET_H = lambda x: x["HIERARCHY"]
GET_F = lambda x: x["F_PARENTID"]
# ADD_LIST_QUERY = {
# "title": TITLE,
# "listtype": 3,
# "item_state": 1,
# "ispublic" : 1,
# }
# requests.post(BASE_URL + ADD_LIST_ROUTE, data=ADD_LIST_QUERY, headers = HEADERS)
projectid = 3
EXAMPLE_ADD_ITEM_QUERY = {
"title": "",
"projectid": projectid,
"listtype": 0,
"parent": 0,
}
query = """select * from dbo.[lwplan]"""
cursor.execute(query)
result = []
while True:
row = cursor.fetchone()
if not row:
break
HIERARCHY = row.hierarchy
HIERARCHY = HIERARCHY.split(".")[-2]
HIERARCHY = HIERARCHY.rjust(3, "0")
result.append(
{
"ID": int(row.ID),
"CODE": row.CODE,
"CAPTION": row.CAPTION,
"F_PARENTID": int(row.F_PARENTID) if row.F_PARENTID else 0,
"HIERARCHY": HIERARCHY,
}
)
# result = sorted(result, key=GET_F)
result = sorted(result, key=GET_H)
childs = {}
IDS_DICT = {}
def insert(rows):
remains =[]
for row in rows:
number = 1
try:
F_PARENTID = row["F_PARENTID"]
except KeyError:
F_PARENTID = -1
print('*'*20)
print(row)
print('#'*20)
continue
# try:
if F_PARENTID == 0:
parent = 0
elif str(F_PARENTID) in IDS_DICT:
parent = IDS_DICT[str(F_PARENTID)]['id']
else:
print(F_PARENTID)
print(IDS_DICT)
print(row)
break
remains.append(row)
continue
# except KeyError:
# print('error')
# print(row)
# continue
if parent in childs:
number = childs[parent] + 1
childs[parent] = number
ADD_ITEM_QUERY = {
"title": row["CAPTION"],
"projectid": projectid,
"otherid": row["ID"],
"meta": row["CODE"],
"childorder": number,
"listtype": 0,
"parent": parent,
}
# result = requests.post(BASE_URL + ADD_ITEM_ROUTE, data=ADD_ITEM_QUERY, headers=HEADERS)
New_ID = json.loads(result.text)["data"][0]["id"]
print(str(row["ID"]) + '=' + str(New_ID))
IDS_DICT[str(row["ID"])] = {"id" : int(New_ID), 'title': row["CAPTION"]}
return remains
remains = insert(result)
# print(IDS_DICT)
count = 1
while remains.__len__() > 0 and count < 2:
print('##################' + str(count))
print( remains.__len__() )
remains = insert(remains)
# print(IDS_DICT)
count = count + 1
with open("list_Tanghih.json", 'w') as file:
file.write(json.dumps(IDS_DICT))