refactor
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import requests
|
||||
import json
|
||||
from general_functions import *
|
||||
cursor = create_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"]
|
||||
projectid = 1
|
||||
|
||||
EXAMPLE_ADD_ITEM_QUERY = {
|
||||
"title": "",
|
||||
"projectid": projectid,
|
||||
"listtype": 0,
|
||||
"parent": 0,
|
||||
}
|
||||
|
||||
# print(requests.post(BASE_URL + ADD_LIST_ROUTE, data=ADD_LIST_QUERY, headers = HEADERS).text)
|
||||
|
||||
IDS_DICT = {}
|
||||
|
||||
query = """select * from dbo.lwtopic"""
|
||||
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),
|
||||
"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)
|
||||
|
||||
for i in range(0, 195):
|
||||
number = 1
|
||||
for row in result:
|
||||
if not row:
|
||||
continue
|
||||
|
||||
try:
|
||||
parent = IDS_DICT[str(i)]['id']
|
||||
except KeyError:
|
||||
parent = 0
|
||||
|
||||
try:
|
||||
F_PARENTID = row["F_PARENTID"]
|
||||
except KeyError:
|
||||
F_PARENTID = -1
|
||||
# print('*'*20)
|
||||
# print(row)
|
||||
# print('#'*20)
|
||||
continue
|
||||
|
||||
if F_PARENTID == i : # or (i == 0 and row["F_PARENTID"] == None)
|
||||
ADD_ITEM_QUERY = {
|
||||
"title": row["CAPTION"],
|
||||
"projectid": projectid,
|
||||
"otherid": row["ID"],
|
||||
"childorder": number,
|
||||
"listtype": 0,
|
||||
"parent": parent,
|
||||
}
|
||||
number += 1
|
||||
result = requests.post(BASE_URL + ADD_ITEM_ROUTE, data=ADD_ITEM_QUERY, headers=HEADERS)
|
||||
|
||||
ID = json.loads(result.text)["data"][0]["id"]
|
||||
print(ID)
|
||||
IDS_DICT[str(row["ID"])] = {"id" : int(ID), 'title': row["CAPTION"]}
|
||||
|
||||
with open("list_LWTopic.json", 'w') as file:
|
||||
file.write(json.dumps(IDS_DICT))
|
||||
Reference in New Issue
Block a user