Subversion Repositories SmartDukaan

Rev

Rev 14750 | Rev 14860 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13884 kshitij.so 1
import pymongo
2
import xlwt
3
from datetime import datetime
4
from email.mime.multipart import MIMEMultipart
5
import email.encoders
6
import smtplib
7
 
8
con = None
9
 
14416 kshitij.so 10
statusMap = {1:'Active',2:'EOL',3:'In Process',4:'Exclusive'}
11
 
13884 kshitij.so 12
class __SkuInfo:
13
 
14
    def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
14812 kshitij.so 15
                 maxDiscount, discountType, dp, nlcPoints, bestSellerPoints, totalPoints,in_stock,maxprice,showDeal, catalogBestSellerPoints):
13884 kshitij.so 16
        self._id = _id
17
        self.skuBundleId = skuBundleId
18
        self.category_id = category_id
19
        self.mrp = mrp
20
        self.available_price = available_price
21
        self.source_id = source_id
22
        self.rank = rank
23
        self.maxNlc = maxNlc
24
        self.minNlc = minNlc
25
        self.schemeAmount = schemeAmount
26
        self.minDiscount = minDiscount
27
        self.maxDiscount = maxDiscount
28
        self.discountType = discountType
29
        self.dp = dp
30
        self.nlcPoints = nlcPoints
31
        self.bestSellerPoints = bestSellerPoints
32
        self.totalPoints = totalPoints
33
        self.in_stock = in_stock
13906 kshitij.so 34
        self.maxprice = maxprice
14115 kshitij.so 35
        self.showDeal = showDeal
14812 kshitij.so 36
        self.catalogBestSellerPoints = catalogBestSellerPoints  
13884 kshitij.so 37
 
38
def get_mongo_connection(host='localhost', port=27017):
39
    global con
40
    if con is None:
41
        print "Establishing connection %s host and port %d" %(host,port)
42
        try:
43
            con = pymongo.MongoClient(host, port)
44
        except Exception, e:
45
            print e
46
            return None
47
    return con
48
 
13904 kshitij.so 49
def writeSheet():
50
    p = []
13884 kshitij.so 51
    data = get_mongo_connection().Catalog.Deals.find().sort([('totalPoints',pymongo.DESCENDING)])
52
    print data.count()
53
    for x in data:
54
        s_info = __SkuInfo(x['_id'], x['skuBundleId'], x['category_id'],x['mrp'],x['available_price'],x['source_id'],x['rank'],x['maxNlc'], \
55
                           x['minNlc'], x['schemeAmount'],x['minDiscount'],x['maxDiscount'],x['discountType'],x['dp'],x['nlcPoints'],x['bestSellerPoints'], \
14812 kshitij.so 56
                           x['totalPoints'],x['in_stock'],x['maxprice'],x['showDeal'],x['catalogBestSellerPoints'])
13884 kshitij.so 57
        p.append(s_info)
13904 kshitij.so 58
 
13884 kshitij.so 59
    wbk = xlwt.Workbook()
60
    sheet = wbk.add_sheet('Deals')
61
    xstr = lambda s: s or ""
62
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
63
 
64
    excel_integer_format = '0'
65
    integer_style = xlwt.XFStyle()
66
    integer_style.num_format_str = excel_integer_format
67
 
68
    sheet.write(0, 0, "Item ID", heading_xf)
69
    sheet.write(0, 1, "Catalog Item Id", heading_xf)
70
    sheet.write(0, 2, "Category", heading_xf)
71
    sheet.write(0, 3, "Brand", heading_xf)
72
    sheet.write(0, 4, "Product Name", heading_xf)
73
    sheet.write(0, 5, "Item Status", heading_xf)
74
    sheet.write(0, 6, "Stock Status", heading_xf)
75
    sheet.write(0, 7, "MarketPlace", heading_xf)
76
    sheet.write(0, 8, "MarketPlace Identifier", heading_xf)
77
    sheet.write(0, 9, "MarketPlace Secondary Identifier", heading_xf)
78
    sheet.write(0, 10, "Product Name (Marketplace)", heading_xf)
79
    sheet.write(0, 11, "Url", heading_xf)
80
    sheet.write(0, 12, "Price", heading_xf)
14812 kshitij.so 81
    sheet.write(0, 13, "Mrp", heading_xf)
82
    sheet.write(0, 14, "DP", heading_xf)
83
    sheet.write(0, 15, "Scheme Amount", heading_xf)
84
    sheet.write(0, 16, "Discount Type", heading_xf)
85
    sheet.write(0, 17, "Min Discount", heading_xf)
86
    sheet.write(0, 18, "Max Discount", heading_xf)
87
    sheet.write(0, 19, "Max Nlc", heading_xf)
88
    sheet.write(0, 20, "Min Nlc", heading_xf)
89
    sheet.write(0, 21, "Max Price", heading_xf)
90
    sheet.write(0, 22, "Rank", heading_xf)
91
    sheet.write(0, 23, "Nlc Points", heading_xf)
92
    sheet.write(0, 24, "Best Seller Points", heading_xf)
93
    sheet.write(0, 25, "Catalog Best Seller Points", heading_xf)
94
    sheet.write(0, 26, "Total Points", heading_xf)
95
    sheet.write(0, 27, "Show Deals", heading_xf)
13884 kshitij.so 96
 
97
    it = 1
98
    for x in p:
99
        y = list(get_mongo_connection().Catalog.MasterData.find({'_id':x._id}))
100
        sheet.write(it, 0, x._id)
101
        sheet.write(it, 1, x.skuBundleId)
102
        if x.category_id == 3:
103
            category = 'Mobiles'
104
        else:
105
            category = 'Tablets'
106
        sheet.write(it, 2, category)
107
        sheet.write(it, 3, y[0]['brand'])
108
        sheet.write(it, 4, y[0]['product_name'])
14416 kshitij.so 109
        sheet.write(it, 5, statusMap.get(y[0]['status']))
13884 kshitij.so 110
        if y[0]['in_stock'] ==1:
111
            sheet.write(it, 6, 'In Stock')
112
        else:
113
            sheet.write(it, 6, 'Out Of Stock')
114
        sheet.write(it, 7, y[0]['source'])
115
        sheet.write(it, 8, y[0]['identifier'])
116
        sheet.write(it, 9, y[0]['secondaryIdentifier'])
117
        sheet.write(it, 10, y[0]['source_product_name'])
118
        sheet.write(it, 11, y[0]['url'])
119
        sheet.write(it, 12, y[0]['available_price'])
14812 kshitij.so 120
        sheet.write(it, 13, x.mrp)
121
        sheet.write(it, 14, x.dp)
122
        sheet.write(it, 15, x.schemeAmount)
123
        sheet.write(it, 16, x.discountType)
124
        sheet.write(it, 17, x.minDiscount)
125
        sheet.write(it, 18, x.maxDiscount)
126
        sheet.write(it, 19, x.maxNlc)
127
        sheet.write(it, 20, x.minNlc)
128
        sheet.write(it, 21, x.maxprice)
129
        sheet.write(it, 22, x.rank)
130
        sheet.write(it, 23, x.nlcPoints)
131
        sheet.write(it, 24, x.bestSellerPoints)
132
        sheet.write(it, 25, x.catalogBestSellerPoints)
133
        sheet.write(it, 26, x.totalPoints)
134
        sheet.write(it, 27, x.showDeal)
13884 kshitij.so 135
        it+=1
136
    filename = "/tmp/deal-data"+str(datetime.now())+".xls" 
137
    wbk.save(filename)
138
    smtpServer = smtplib.SMTP('localhost')
13909 kshitij.so 139
    #smtpServer.set_debuglevel(1)
13884 kshitij.so 140
    sender = 'dtr@shop2020.in'
13896 kshitij.so 141
    #recipients = ['kshitij.sood@saholic.com']
13884 kshitij.so 142
    msg = MIMEMultipart()
143
    msg['Subject'] = "DTR Deals data" + ' - ' + str(datetime.now())
144
    msg['From'] = sender
13896 kshitij.so 145
    recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','manoj.kumar@saholic.com']
13884 kshitij.so 146
    msg['To'] = ",".join(recipients)
147
    fileMsg = email.mime.base.MIMEBase('application','vnd.ms-excel')
148
    fileMsg.set_payload(file(filename).read())
149
    email.encoders.encode_base64(fileMsg)
13898 kshitij.so 150
    fileMsg.add_header('Content-Disposition','attachment;filename=Deal_Sheet_Dtr.xls')
13884 kshitij.so 151
    msg.attach(fileMsg)
152
    try:
153
        smtpServer.sendmail(sender, recipients, msg.as_string())
154
        print "Successfully sent email"
155
    except:
156
        print "Error: unable to send email."
157
 
13893 kshitij.so 158
def sendMail():
13884 kshitij.so 159
    writeSheet()
160
 
13893 kshitij.so 161
def main():
162
    sendMail()
163
 
13884 kshitij.so 164
if __name__=='__main__':
165
    main()