/
sync_NEW.py
311 lines (279 loc) · 14.3 KB
/
sync_NEW.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import sqlite3
import os
import luigi
import time
from simple_salesforce import Salesforce
#from tinydb import TinyDB, where,Query
from datetime import datetime, timedelta
import requests
#db = TinyDB('db.json')
#CN = Query()
import salesforce_reporting
# import requests,json
# from flask import jsonify
# from SFDC import dbmodels
attemts=['RHID','RHIT']
#db_path=os.path.abspath("\\\\IXI-APPSVR02\\RHID Sustaining Production\\ttproj.db")
#db_path2=os.path.abspath("\\\\IXI-APPSVR02\\RapidHIT Sustaining\\ttproj.db")
# while True:
class ixisync(luigi.Task):
def requires(self):
return []
def output(self):
return []
#return luigi.LocalTarget("squares.txt")
def run(self):
sf = Salesforce(username='customerservice@integenx.com', password='password12345', security_token='3GKZny4jRWgmXNUmvFjzKhggQ')
print('NEW START.............................................................................................................')
for attemt in attemts:
# try:
# c.close();
# conn5.commit();
# conn5.close();
#
# except Exception as e:
# print(e)
# try:
# c.close();
# conn6.commit();
# conn6.close();
# except Exception as e:
# print(e)
# if attemt=='RHID': db_path=os.path.abspath("\\\\IXI-APPSVR02\\RHID Sustaining Production\\ttproj.db")
# if attemt=='RHIT': db_path=os.path.abspath("\\\\IXI-APPSVR02\\RapidHIT Sustaining\\ttproj.db")
#while True:
#time.sleep(60)
# print("After 5 sec")
date_S = datetime.now() - timedelta(days=1)
date_E = datetime.now() - timedelta(days=0)
print(str(date_S)[0:10])
print(attemt)
hastrack= False
solution=""
try:
if attemt=='RHID': db_path=os.path.abspath("\\\\IXI-APPSVR02\\RHID Sustaining Production\\ttproj.db")
if attemt=='RHIT': db_path=os.path.abspath("\\\\IXI-APPSVR02\\RapidHIT Sustaining\\ttproj.db")
conn5 = sqlite3.connect(db_path)
c = conn5.cursor()
hourago= datetime.now() - timedelta(hours = 1)
#s='select DefectNum,Summary,Status from DEFECTS inner join DEFECTEVTS on DEFECTEVTS.ParentID=DEFECTS.idRecord where dateEvent> '+ str(hourago).rpartition(':')[0]
# s='select idRecord,DefectNum,Summary,Status,dateModify from DEFECTS where dateModify >'+ repr(str(hourago).rpartition(':')[0])
print(str(date_S).rpartition(':')[0])
#print (str(date_E).rpartition(':')[4])
s='select idRecord,DefectNum,Summary,Status,dateModify from DEFECTS where dateModify >='+ repr(str(date_S).rpartition(':')[0])
print(s)
#s='select idRecord,DefectNum,Summary,Status,dateModify from DEFECTS where dateModify >='+ repr(str(date_S).rpartition(':')[0]) + ' and dateModify <='+ repr(str(date_E).rpartition(':')[0])
#s='select dateModify,idRecord,DefectNum,Summary,Status,dateModify from DEFECTS where DefectNum =2976'
#'+ repr(str(hourago).rpartition(':')[0])
c.execute(s)
rows=c.fetchall()
defect_as_dict=[]
#file = open("sync.txt","w")
for row in rows:
DefectNum=row[1]
print(DefectNum)
Summary=row[2]
Status=row[3]
if attemt=='RHID': s='select Custvalue from CUSTMVAL where idCustRec=681 and ParentID=' + repr(str(row[0])) + ''
if attemt=='RHIT': s='select Custvalue from CUSTMVAL where idCustRec=1072 and ParentID=' + repr(str(row[0])) + ''
c.execute(s)
rows1=c.fetchall()
casenumber=0
for row1 in rows1:
casenumber=row1[0]
print(casenumber)
#Synceddict=db.search(CN.casenumber == casenumber)
s='select Name,dateEvent,DEFECTEVTS.Notes ,OrderNum from DEFECTEVTS inner join EVENTS on DEFECTEVTS.EvtDefID=EVENTS.idRecord and ParentID=' + str(row[0]) + ' order by OrderNum'
c.execute(s)
tracks_as_dict=[]
tracktext=""
tracks=c.fetchall()
for track in tracks:
hastrack= True
if (track[0]=="Verified"): solution=track[2]
track_as_dict={
'Event': track[0],
'Date': track[1],
'Notes': track[2]
}
if (hastrack):
tracks_as_dict.append(track_as_dict)
tracktext=str(tracks_as_dict)
#UPDATE WORKFLOW
list=sf.query("SELECT Id,Defect_Ref__c FROM Case WHERE CaseNumber = " + repr(str(casenumber)))
dict=list["records"]
if len(dict)> 0:
cid=dict[0]['Id']
CurrentDefectNumber=dict[0]['Defect_Ref__c']
sf.Case.update(cid,{'Workflow_from_ALM__c': tracktext})
if CurrentDefectNumber is None:
#sf.headers({'Sforce-Auto-Assign': 'FALSE'})
sf.Case.update(cid,{'Defect_Ref__c': DefectNum})
#Write the log
file = open("C:\Python34\ixi_TBOX34\sync.txt","a")
logstr="|"+ str(casenumber) + "|" + str(DefectNum) +"|" + attemt +"|" + repr(str(datetime.now()))
file.write(logstr+"\n")
file.close()
#UPDATE SOLUTIONS
if Status == 10:
if (str(solution) != ""):
list=sf.query("SELECT Id FROM Case WHERE CaseNumber = " + repr(str(casenumber)))
dict=list["records"]
if len(dict)> 0:
print(dict[0]['Id'])
cid=dict[0]['Id']
mydict = sf.Solution.create({'SolutionNote':solution, 'SolutionName': 'solution - '+ Summary})
#db.insert({'casenumber': casenumber})
print(mydict['id'])
sid=mydict['id']
solution = sf.Solution.get(mydict['id'])
print(mydict['success'])
#orderdisc=solution('OrderDict')
print(solution['SolutionNumber'])
sn=solution['SolutionNumber']
mydict = sf.CaseSolution.create({'CaseId': cid , 'SolutionId':sid})
print(mydict)
#Update case
#file.close()
#sf.Case.Update
except Exception as e:
print(e)
if __name__ == '__main__':
luigi.run()
# def DoSync():
# sf = Salesforce(username='customerservice@integenx.com', password='password12345', security_token='3GKZny4jRWgmXNUmvFjzKhggQ')
# print('NEW START.............................................................................................................')
# for attemt in attemts:
# # try:
# # c.close();
# # conn5.commit();
# # conn5.close();
# #
# # except Exception as e:
# # print(e)
# # try:
# # c.close();
# # conn6.commit();
# # conn6.close();
# # except Exception as e:
# # print(e)
#
# # if attemt=='RHID': db_path=os.path.abspath("\\\\IXI-APPSVR02\\RHID Sustaining Production\\ttproj.db")
# # if attemt=='RHIT': db_path=os.path.abspath("\\\\IXI-APPSVR02\\RapidHIT Sustaining\\ttproj.db")
# #while True:
# #time.sleep(60)
# # print("After 5 sec")
#
# date_S = datetime.now() - timedelta(days=1)
# date_E = datetime.now() - timedelta(days=0)
# print(str(date_S)[0:10])
# print(attemt)
# hastrack= False
# solution=""
# try:
# if attemt=='RHID': db_path=os.path.abspath("\\\\IXI-APPSVR02\\RHID Sustaining Production\\ttproj.db")
# if attemt=='RHIT': db_path=os.path.abspath("\\\\IXI-APPSVR02\\RapidHIT Sustaining\\ttproj.db")
# conn5 = sqlite3.connect(db_path)
# c = conn5.cursor()
# hourago= datetime.now() - timedelta(hours = 1)
# #s='select DefectNum,Summary,Status from DEFECTS inner join DEFECTEVTS on DEFECTEVTS.ParentID=DEFECTS.idRecord where dateEvent> '+ str(hourago).rpartition(':')[0]
# # s='select idRecord,DefectNum,Summary,Status,dateModify from DEFECTS where dateModify >'+ repr(str(hourago).rpartition(':')[0])
#
# print(str(date_S).rpartition(':')[0])
# #print (str(date_E).rpartition(':')[4])
# s='select idRecord,DefectNum,Summary,Status,dateModify from DEFECTS where dateModify >='+ repr(str(date_S).rpartition(':')[0])
# print(s)
# #s='select idRecord,DefectNum,Summary,Status,dateModify from DEFECTS where dateModify >='+ repr(str(date_S).rpartition(':')[0]) + ' and dateModify <='+ repr(str(date_E).rpartition(':')[0])
# #s='select dateModify,idRecord,DefectNum,Summary,Status,dateModify from DEFECTS where DefectNum =2976'
# #'+ repr(str(hourago).rpartition(':')[0])
# c.execute(s)
# rows=c.fetchall()
# defect_as_dict=[]
# #file = open("sync.txt","w")
# for row in rows:
#
# DefectNum=row[1]
# print(DefectNum)
# Summary=row[2]
# Status=row[3]
# if attemt=='RHID': s='select Custvalue from CUSTMVAL where idCustRec=681 and ParentID=' + repr(str(row[0])) + ''
# if attemt=='RHIT': s='select Custvalue from CUSTMVAL where idCustRec=1072 and ParentID=' + repr(str(row[0])) + ''
# c.execute(s)
# rows1=c.fetchall()
# casenumber=0
# for row1 in rows1:
# casenumber=row1[0]
# print(casenumber)
# #Synceddict=db.search(CN.casenumber == casenumber)
# s='select Name,dateEvent,DEFECTEVTS.Notes ,OrderNum from DEFECTEVTS inner join EVENTS on DEFECTEVTS.EvtDefID=EVENTS.idRecord and ParentID=' + str(row[0]) + ' order by OrderNum'
# c.execute(s)
# tracks_as_dict=[]
# tracktext=""
# tracks=c.fetchall()
# for track in tracks:
# hastrack= True
# if (track[0]=="Verified"): solution=track[2]
# track_as_dict={
# 'Event': track[0],
# 'Date': track[1],
# 'Notes': track[2]
# }
# if (hastrack):
# tracks_as_dict.append(track_as_dict)
# tracktext=str(tracks_as_dict)
#
#
#
# #UPDATE WORKFLOW
#
# list=sf.query("SELECT Id,Defect_Ref__c FROM Case WHERE CaseNumber = " + repr(str(casenumber)))
# dict=list["records"]
# if len(dict)> 0:
# cid=dict[0]['Id']
# CurrentDefectNumber=dict[0]['Defect_Ref__c']
# sf.Case.update(cid,{'Workflow_from_ALM__c': tracktext})
# if CurrentDefectNumber is None:
# #sf.headers({'Sforce-Auto-Assign': 'FALSE'})
# sf.Case.update(cid,{'Defect_Ref__c': DefectNum})
#
#
# #Write the log
# file = open("C:\Python34\ixi_TBOX34\sync.txt","a")
# logstr="|"+ str(casenumber) + "|" + str(DefectNum) +"|" + attemt +"|" + repr(str(datetime.now()))
# file.write(logstr+"\n")
# file.close()
#
#
# #UPDATE SOLUTIONS
# if Status == 10:
#
# if (str(solution) != ""):
#
# list=sf.query("SELECT Id FROM Case WHERE CaseNumber = " + repr(str(casenumber)))
# dict=list["records"]
# if len(dict)> 0:
# print(dict[0]['Id'])
# cid=dict[0]['Id']
# mydict = sf.Solution.create({'SolutionNote':solution, 'SolutionName': 'solution - '+ Summary})
# #db.insert({'casenumber': casenumber})
# print(mydict['id'])
# sid=mydict['id']
# solution = sf.Solution.get(mydict['id'])
# print(mydict['success'])
# #orderdisc=solution('OrderDict')
# print(solution['SolutionNumber'])
# sn=solution['SolutionNumber']
#
# mydict = sf.CaseSolution.create({'CaseId': cid , 'SolutionId':sid})
# print(mydict)
#
# #Update case
# #file.close()
# #sf.Case.Update
#
# except Exception as e:
# print(e)
#
#
# # print('Waiting...')
# # time.sleep(3000)