226 lines
5.1 KiB
Python
226 lines
5.1 KiB
Python
import json
|
|
import requests
|
|
from config_base import *
|
|
import pyodbc
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
from html import escape
|
|
import json
|
|
from decimal import Decimal
|
|
import datetime
|
|
import sys
|
|
import traceback
|
|
import re
|
|
|
|
from config_base import *
|
|
from general_functions import *
|
|
|
|
# این برنامه یک بار فقط جهت انتقال کدهای دیتابیس آقای شاهنوش به دیتابیس نهایی قوانین انجام شد
|
|
# تکرار لازم نیست
|
|
|
|
cursor, cnxn = create_cursor('db4minaei')
|
|
|
|
#Does quasi the same things as json.loads from here: https://pypi.org/project/dynamodb-json/
|
|
class JSONEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, Decimal):
|
|
return float(obj)
|
|
return json.JSONEncoder.default(self, obj)
|
|
|
|
|
|
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)
|
|
|
|
|
|
url = BASE_URL + "repo/majles/section/multi/update"
|
|
dict_topic = json.loads(open("list_LWTopic.json").read())
|
|
|
|
headers = HEADERS
|
|
|
|
bulk_count = 1
|
|
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)
|
|
bulk_count +=1
|
|
prev_sectionid = sectionid
|
|
tcode_main_old = []
|
|
tcode_sub_old = []
|
|
tcode_main = []
|
|
tcode_sub = []
|
|
|
|
if bulk_data.__len__() > 100:
|
|
print('=' * 10 )
|
|
print(str(i)+ ', id=' + eid)
|
|
payload = json.dumps(bulk_data, cls=JSONEncoder) #Works!
|
|
response = requests.request("POST", url, headers=headers, data=payload)
|
|
# print(response.text)
|
|
bulk_data = []
|
|
bulk_count = 1
|
|
# print(payload)
|
|
|
|
# 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)
|
|
bulk_count +=1
|
|
|
|
if bulk_data.__len__() > 0:
|
|
print(' *** end bulk count ' , bulk_count)
|
|
payload = json.dumps(bulk_data, cls=JSONEncoder) #Works!
|
|
response = requests.request("POST", url, headers=headers, data=payload)
|
|
|
|
# print(response.text)
|
|
|
|
cursor.close()
|
|
cnxn.close()
|
|
print('end success')
|
|
t2= datetime.datetime.now()
|
|
print(t2-t1)
|
|
|