Subversion Repositories SmartDukaan

Rev

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