| Line 2... |
Line 2... |
| 2 |
Created on Mar 13, 2015
|
2 |
Created on Mar 13, 2015
|
| 3 |
|
3 |
|
| 4 |
@author: amit
|
4 |
@author: amit
|
| 5 |
'''
|
5 |
'''
|
| 6 |
from datetime import date, datetime, timedelta
|
6 |
from datetime import date, datetime, timedelta
|
| 7 |
from dtr.main import Store
|
7 |
from dtr.main import Store, sourceMap
|
| 8 |
from dtr.storage import Mysql
|
8 |
from dtr.storage import Mysql
|
| 9 |
from dtr.storage.Mysql import getOrdersAfterDate, getOrdersByTag
|
9 |
from dtr.storage.Mysql import getOrdersAfterDate, getOrdersByTag
|
| 10 |
from email import encoders
|
10 |
from email import encoders
|
| 11 |
from email.mime.base import MIMEBase
|
11 |
from email.mime.base import MIMEBase
|
| 12 |
from email.mime.multipart import MIMEMultipart
|
12 |
from email.mime.multipart import MIMEMultipart
|
| Line 28... |
Line 28... |
| 28 |
SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()
|
28 |
SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()
|
| 29 |
SMTP_SERVER = "smtp.gmail.com"
|
29 |
SMTP_SERVER = "smtp.gmail.com"
|
| 30 |
SMTP_PORT = 587
|
30 |
SMTP_PORT = 587
|
| 31 |
|
31 |
|
| 32 |
XLS_FILENAME = "dtrsourcesreco.xls"
|
32 |
XLS_FILENAME = "dtrsourcesreco.xls"
|
| - |
|
33 |
XLS_O_FILENAME = "allorders.xls"
|
| 33 |
boldStyle = xlwt.XFStyle()
|
34 |
boldStyle = xlwt.XFStyle()
|
| 34 |
f = xlwt.Font()
|
35 |
f = xlwt.Font()
|
| 35 |
f.bold = True
|
36 |
f.bold = True
|
| 36 |
boldStyle.font = f
|
37 |
boldStyle.font = f
|
| 37 |
i = -1
|
38 |
i = -1
|
| Line 268... |
Line 269... |
| 268 |
affNotReconciledSheet.write(row, inc(), offer.get("conversionStatus"))
|
269 |
affNotReconciledSheet.write(row, inc(), offer.get("conversionStatus"))
|
| 269 |
affNotReconciledSheet.write(row, inc(), offer.get("ip"))
|
270 |
affNotReconciledSheet.write(row, inc(), offer.get("ip"))
|
| 270 |
|
271 |
|
| 271 |
workbook.save(XLS_FILENAME)
|
272 |
workbook.save(XLS_FILENAME)
|
| 272 |
|
273 |
|
| 273 |
def sendmail(email, message, title):
|
274 |
def sendmail(email, message, title, fileName):
|
| 274 |
if email == "":
|
275 |
if email == "":
|
| 275 |
return
|
276 |
return
|
| 276 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
277 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
| 277 |
mailServer.ehlo()
|
278 |
mailServer.ehlo()
|
| 278 |
mailServer.starttls()
|
279 |
mailServer.starttls()
|
| Line 285... |
Line 286... |
| 285 |
html_msg = MIMEText(message, 'html')
|
286 |
html_msg = MIMEText(message, 'html')
|
| 286 |
msg.attach(html_msg)
|
287 |
msg.attach(html_msg)
|
| 287 |
|
288 |
|
| 288 |
#snapdeal more to be added here
|
289 |
#snapdeal more to be added here
|
| 289 |
snapdeal = MIMEBase('application', 'vnd.ms-excel')
|
290 |
snapdeal = MIMEBase('application', 'vnd.ms-excel')
|
| 290 |
snapdeal.set_payload(file(XLS_FILENAME).read())
|
291 |
snapdeal.set_payload(file(fileName).read())
|
| 291 |
encoders.encode_base64(snapdeal)
|
292 |
encoders.encode_base64(snapdeal)
|
| 292 |
snapdeal.add_header('Content-Disposition', 'attachment;filename=' + XLS_FILENAME)
|
293 |
snapdeal.add_header('Content-Disposition', 'attachment;filename=' + fileName)
|
| 293 |
msg.attach(snapdeal)
|
294 |
msg.attach(snapdeal)
|
| 294 |
|
295 |
|
| 295 |
|
296 |
|
| 296 |
email.append('amit.gupta@shop2020.in')
|
297 |
email.append('amit.gupta@shop2020.in')
|
| 297 |
MAILTO = email
|
298 |
MAILTO = email
|
| Line 377... |
Line 378... |
| 377 |
global i
|
378 |
global i
|
| 378 |
rb = open_workbook(XLS_FILENAME,formatting_info=True)
|
379 |
rb = open_workbook(XLS_FILENAME,formatting_info=True)
|
| 379 |
wb = copy(rb)
|
380 |
wb = copy(rb)
|
| 380 |
worksheet = wb.add_sheet("All Users")
|
381 |
worksheet = wb.add_sheet("All Users")
|
| 381 |
worksheet1 = wb.add_sheet("Suspected Users")
|
382 |
worksheet1 = wb.add_sheet("Suspected Users")
|
| - |
|
383 |
workbook = xlwt.Workbook()
|
| - |
|
384 |
sh1 = workbook.add_sheet("All Orders")
|
| 382 |
|
385 |
|
| 383 |
db=client.Dtr
|
386 |
db=client.Dtr
|
| 384 |
results = Mysql.fetchResult('''
|
387 |
results = Mysql.fetchResult('''
|
| 385 |
select ow.*, u.username, crm1.order_count, u.referrer from order_view ow left join users u on u.id = ow.user_id left join (select user_id, count(*) as order_count from order_view where status = 'ORDER_CREATED' group by user_id) as crm1 on ow.user_id = crm1.user_id where lower(u.referrer) not like 'emp%'
|
388 |
select ow.*, u.username, crm1.order_count, u.referrer from order_view ow left join users u on u.id = ow.user_id left join (select user_id, count(*) as order_count from order_view where status = 'ORDER_CREATED' group by user_id) as crm1 on ow.user_id = crm1.user_id where lower(u.referrer) not like 'emp%'
|
| 386 |
''',)
|
389 |
''',)
|
| 387 |
orders = []
|
390 |
orders = []
|
| Line 389... |
Line 392... |
| 389 |
orderH = []
|
392 |
orderH = []
|
| 390 |
row = 0
|
393 |
row = 0
|
| 391 |
i=-1
|
394 |
i=-1
|
| 392 |
worksheet.write(row, inc(), 'User Id', boldStyle)
|
395 |
worksheet.write(row, inc(), 'User Id', boldStyle)
|
| 393 |
worksheet1.write(row, i, 'User Id', boldStyle)
|
396 |
worksheet1.write(row, i, 'User Id', boldStyle)
|
| - |
|
397 |
sh1.write(row, i, 'User Id', boldStyle)
|
| 394 |
worksheet.write(row, inc(), 'User name', boldStyle)
|
398 |
worksheet.write(row, inc(), 'User name', boldStyle)
|
| 395 |
worksheet1.write(row, i, 'User name', boldStyle)
|
399 |
worksheet1.write(row, i, 'User name', boldStyle)
|
| - |
|
400 |
sh1.write(row, i, 'User name', boldStyle)
|
| 396 |
worksheet.write(row, inc(), 'Order Id', boldStyle)
|
401 |
worksheet.write(row, inc(), 'Order Id', boldStyle)
|
| 397 |
worksheet1.write(row, i, 'Order Id', boldStyle)
|
402 |
worksheet1.write(row, i, 'Order Id', boldStyle)
|
| - |
|
403 |
sh1.write(row, i, 'Order Id', boldStyle)
|
| 398 |
worksheet.write(row, inc(), 'Created On', boldStyle)
|
404 |
worksheet.write(row, inc(), 'Created On', boldStyle)
|
| 399 |
worksheet1.write(row, i, 'Created On', boldStyle)
|
405 |
worksheet1.write(row, i, 'Created On', boldStyle)
|
| - |
|
406 |
sh1.write(row, i, 'Created On', boldStyle)
|
| 400 |
worksheet.write(row, inc(), 'Store Id', boldStyle)
|
407 |
worksheet.write(row, inc(), 'Store Id', boldStyle)
|
| 401 |
worksheet1.write(row, i, 'Store Id', boldStyle)
|
408 |
worksheet1.write(row, i, 'Store Id', boldStyle)
|
| - |
|
409 |
sh1.write(row, i, 'Store Id', boldStyle)
|
| 402 |
worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
|
410 |
worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
|
| 403 |
worksheet1.write(row, i, 'Merchant Order Id', boldStyle)
|
411 |
worksheet1.write(row, i, 'Merchant Order Id', boldStyle)
|
| - |
|
412 |
sh1.write(row, i, 'Merchant Order Id', boldStyle)
|
| 404 |
worksheet.write(row, inc(), 'Product title', boldStyle)
|
413 |
worksheet.write(row, inc(), 'Product title', boldStyle)
|
| 405 |
worksheet1.write(row, i, 'Product title', boldStyle)
|
414 |
worksheet1.write(row, i, 'Product title', boldStyle)
|
| - |
|
415 |
sh1.write(row, i, 'Product title', boldStyle)
|
| 406 |
worksheet.write(row, inc(), 'Amount Paid', boldStyle)
|
416 |
worksheet.write(row, inc(), 'Amount Paid', boldStyle)
|
| 407 |
worksheet1.write(row, i, 'Amount Paid', boldStyle)
|
417 |
worksheet1.write(row, i, 'Amount Paid', boldStyle)
|
| - |
|
418 |
sh1.write(row, i, 'Amount Paid', boldStyle)
|
| 408 |
worksheet.write(row, inc(), 'Cashback', boldStyle)
|
419 |
worksheet.write(row, inc(), 'Cashback', boldStyle)
|
| 409 |
worksheet1.write(row, i, 'Cashback', boldStyle)
|
420 |
worksheet1.write(row, i, 'Cashback', boldStyle)
|
| - |
|
421 |
sh1.write(row, i, 'Brand', boldStyle)
|
| 410 |
worksheet.write(row, inc(), 'Order Status', boldStyle)
|
422 |
worksheet.write(row, inc(), 'Order Status', boldStyle)
|
| 411 |
worksheet1.write(row, i, 'Order Status', boldStyle)
|
423 |
worksheet1.write(row, i, 'Order Status', boldStyle)
|
| - |
|
424 |
sh1.write(row, i, 'Model', boldStyle)
|
| 412 |
worksheet.write(row, inc(), 'Detailed Status', boldStyle)
|
425 |
worksheet.write(row, inc(), 'Detailed Status', boldStyle)
|
| 413 |
worksheet1.write(row, i, 'Detailed Status', boldStyle)
|
426 |
worksheet1.write(row, i, 'Detailed Status', boldStyle)
|
| - |
|
427 |
sh1.write(row, i, 'Category', boldStyle)
|
| 414 |
worksheet.write(row, inc(), 'Cashback Status', boldStyle)
|
428 |
worksheet.write(row, inc(), 'Cashback Status', boldStyle)
|
| 415 |
worksheet1.write(row, i, 'Cashback Status', boldStyle)
|
429 |
worksheet1.write(row, i, 'Cashback Status', boldStyle)
|
| 416 |
worksheet.write(row, inc(), 'Reconciled', boldStyle)
|
430 |
worksheet.write(row, inc(), 'Reconciled', boldStyle)
|
| 417 |
worksheet1.write(row, i, 'Reconciled', boldStyle)
|
431 |
worksheet1.write(row, i, 'Reconciled', boldStyle)
|
| 418 |
worksheet.write(row, inc(), 'IP', boldStyle)
|
432 |
worksheet.write(row, inc(), 'IP', boldStyle)
|
| 419 |
worksheet1.write(row, i, 'IP', boldStyle)
|
433 |
worksheet1.write(row, i, 'IP', boldStyle)
|
| 420 |
|
434 |
|
| 421 |
row=0
|
435 |
row=0
|
| 422 |
row2=0
|
436 |
row2=0
|
| - |
|
437 |
row3=0
|
| - |
|
438 |
db1 = client.Catalog
|
| 423 |
for result in results:
|
439 |
for result in results:
|
| 424 |
morder = db.merchantOrder.find_one({"orderId":result[0]})
|
440 |
morder = db.merchantOrder.find_one({"orderId":result[0]})
|
| 425 |
if morder is not None:
|
441 |
if morder is not None:
|
| 426 |
subOrders = morder.get("subOrders")
|
442 |
subOrders = morder.get("subOrders")
|
| 427 |
if subOrders is not None:
|
443 |
if subOrders is not None:
|
| 428 |
for subOrder in subOrders:
|
444 |
for subOrder in subOrders:
|
| - |
|
445 |
row3+=1
|
| - |
|
446 |
i=-1
|
| - |
|
447 |
sh1.write(row3, inc(), result[0])
|
| - |
|
448 |
sh1.write(row3, inc(), result[-3])
|
| - |
|
449 |
sh1.write(row3, inc(), result[1])
|
| - |
|
450 |
sh1.write(row3, inc(), result[-4], datetime_format)
|
| - |
|
451 |
sh1.write(row3, inc(), sourceMap.get(result[2]))
|
| - |
|
452 |
sh1.write(row3, inc(), morder.get("merchantOrderId"))
|
| - |
|
453 |
sh1.write(row3, inc(), subOrder.get("productTitle"))
|
| - |
|
454 |
inc()
|
| - |
|
455 |
if int(subOrder.get("quantity")) > 0:
|
| - |
|
456 |
sh1.write(row3, i, int(subOrder.get("amountPaid"))/int(subOrder.get("quantity")))
|
| - |
|
457 |
res = db1.MasterData.find_one({"source_id":morder.get("storeId"), "identifier":subOrder.get("productCode")})
|
| - |
|
458 |
if res is not None:
|
| - |
|
459 |
sh1.write(row3, inc(), res.get("brand"))
|
| - |
|
460 |
sh1.write(row3, inc(), res.get("model_name"))
|
| - |
|
461 |
sh1.write(row3, inc(), subOrder.get("category"))
|
| 429 |
if subOrder.get("cashBackStatus") == Store.CB_APPROVED or subOrder.get("cashBackStatus") == Store.CB_PENDING:
|
462 |
if subOrder.get("cashBackStatus") == Store.CB_APPROVED or subOrder.get("cashBackStatus") == Store.CB_PENDING:
|
| 430 |
row +=1
|
463 |
row +=1
|
| 431 |
i=-1
|
464 |
i=-1
|
| 432 |
worksheet.write(row, inc(), result[0])
|
465 |
worksheet.write(row, inc(), result[0])
|
| 433 |
worksheet.write(row, inc(), result[-3])
|
466 |
worksheet.write(row, inc(), result[-3])
|
| Line 467... |
Line 500... |
| 467 |
if morder.get("reconciled") and morder.get("storeId")==3:
|
500 |
if morder.get("reconciled") and morder.get("storeId")==3:
|
| 468 |
try:
|
501 |
try:
|
| 469 |
worksheet1.write(row2, inc(), db.snapdealOrderAffiliateInfo.find_one({"adId":morder.get("adId"), "reconciled":True}).get("ip"))
|
502 |
worksheet1.write(row2, inc(), db.snapdealOrderAffiliateInfo.find_one({"adId":morder.get("adId"), "reconciled":True}).get("ip"))
|
| 470 |
except:
|
503 |
except:
|
| 471 |
print "Could not find", morder.get("adId"), "that is reconcired for order", morder.get("orderId")
|
504 |
print "Could not find", morder.get("adId"), "that is reconcired for order", morder.get("orderId")
|
| - |
|
505 |
|
| - |
|
506 |
|
| 472 |
wb.save(XLS_FILENAME)
|
507 |
wb.save(XLS_FILENAME)
|
| 473 |
|
- |
|
| - |
|
508 |
workbook.save(XLS_O_FILENAME)
|
| 474 |
def inc():
|
509 |
def inc():
|
| 475 |
global i
|
510 |
global i
|
| 476 |
i+=1
|
511 |
i+=1
|
| 477 |
return i
|
512 |
return i
|
| 478 |
|
513 |
|
| - |
|
514 |
def addHeaders(sheet):
|
| - |
|
515 |
global i
|
| - |
|
516 |
i = 0
|
| - |
|
517 |
sheet.write(0, inc(), )
|
| 479 |
def main():
|
518 |
def main():
|
| 480 |
generateSnapDealReco()
|
519 |
generateSnapDealReco()
|
| 481 |
getUserOrders()
|
520 |
getUserOrders()
|
| 482 |
sendmail(["amit.gupta@shop2020.in", "rajneesh.arora@saholic.com"], "", "DTR Affiliate Reco")
|
521 |
sendmail(["amit.gupta@shop2020.in"], "", "DTR Affiliate Reco", XLS_FILENAME)
|
| - |
|
522 |
sendmail(["amit.gupta@shop2020.in"], "", "All Orders", XLS_O_FILENAME)
|
| 483 |
|
523 |
|
| 484 |
if __name__ == '__main__':
|
524 |
if __name__ == '__main__':
|
| 485 |
main()
|
525 |
main()
|
| 486 |
|
526 |
|
| 487 |
|
527 |
|