refactor auto import
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
from config_base import *
|
||||
from general_functions import *
|
||||
|
||||
cursor, cnxn = create_cursor('db4minaei')
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
|
||||
# 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))
|
||||
Reference in New Issue
Block a user