Subversion Repositories SmartDukaan

Rev

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