127 lines
3.5 KiB
Python
127 lines
3.5 KiB
Python
import requests
|
|
import json
|
|
from text_normalizer import TextNormalizer
|
|
from config_base import *
|
|
from general_functions import *
|
|
|
|
# cursor, cnxn = create_cursor()
|
|
|
|
ADD_LIST_ROUTE = "project/add"
|
|
ADD_ITEM_ROUTE = "subject/add"
|
|
TITLE = "موضوعات درختی قانون"
|
|
GET_H = lambda x: x["HIERARCHY"]
|
|
|
|
def insertProejct():
|
|
# ایجاد فهرست جدید
|
|
|
|
ADD_LIST_QUERY = {
|
|
"title": TITLE,
|
|
"type": 3,
|
|
"item_state": 1,
|
|
"ispublic" : 1,
|
|
}
|
|
|
|
# requests.post(BASE_URL + 'list/' + ADD_LIST_ROUTE, data=ADD_LIST_QUERY, headers = HEADERS)
|
|
projectid = 6
|
|
return projectid
|
|
|
|
#----------------------------------------------------------------
|
|
|
|
def insert(rows, projectid, IDS_DICT, childs ):
|
|
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 + 'list/' + 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
|
|
|
|
|
|
def insertProjectItems(cursor, projectid):
|
|
childs = {}
|
|
IDS_DICT = {}
|
|
|
|
query = """select * from dbo.lwtopic"""
|
|
cursor.execute(query)
|
|
text_normal = TextNormalizer()
|
|
|
|
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": text_normal.normalize_qanon(row.CAPTION),
|
|
"F_PARENTID": int(row.F_PARENTID) if row.F_PARENTID else 0,
|
|
"HIERARCHY": HIERARCHY,
|
|
}
|
|
)
|
|
|
|
result = sorted(result, key=GET_H)
|
|
|
|
remains = insert(result, projectid, IDS_DICT, childs )
|
|
# print(IDS_DICT)
|
|
|
|
count = 1
|
|
while remains.__len__() > 0 and count < 2:
|
|
print('##################' + str(count))
|
|
print( remains.__len__() )
|
|
remains = insert(remains, projectid, IDS_DICT, childs )
|
|
# print(IDS_DICT)
|
|
count = count + 1
|
|
|
|
with open("list_LWTopic.json", 'w') as file:
|
|
file.write(json.dumps(IDS_DICT))
|
|
|
|
print('** write list_LWTopic.json ') |