Subversion Repositories SmartDukaan

Rev

Rev 13896 | Rev 13904 | 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 shop2020.utils import EmailAttachmentSender
5
from shop2020.utils.EmailAttachmentSender import get_attachment_part
6
from email.mime.text import MIMEText
7
import email
8
from email.mime.multipart import MIMEMultipart
9
import email.encoders
10
import smtplib
11
 
12
p = []
13
con = None
14
 
15
class __SkuInfo:
16
 
17
    def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
18
                 maxDiscount, discountType, dp, nlcPoints, bestSellerPoints, totalPoints,in_stock,maxprice,showDeal):
19
        self._id = _id
20
        self.skuBundleId = skuBundleId
21
        self.category_id = category_id
22
        self.mrp = mrp
23
        self.available_price = available_price
24
        self.source_id = source_id
25
        self.rank = rank
26
        self.maxNlc = maxNlc
27
        self.minNlc = minNlc
28
        self.schemeAmount = schemeAmount
29
        self.minDiscount = minDiscount
30
        self.maxDiscount = maxDiscount
31
        self.discountType = discountType
32
        self.dp = dp
33
        self.nlcPoints = nlcPoints
34
        self.bestSellerPoints = bestSellerPoints
35
        self.totalPoints = totalPoints
36
        self.in_stock = in_stock
37
        self.maxprice = maxprice
38
        self.showDeal = showDeal 
39
 
40
def get_mongo_connection(host='localhost', port=27017):
41
    global con
42
    if con is None:
43
        print "Establishing connection %s host and port %d" %(host,port)
44
        try:
45
            con = pymongo.MongoClient(host, port)
46
        except Exception, e:
47
            print e
48
            return None
49
    return con
50
 
51
 
52
def getProducts():
53
    global p
54
    data = get_mongo_connection().Catalog.Deals.find().sort([('totalPoints',pymongo.DESCENDING)])
55
    print data.count()
56
    for x in data:
57
        s_info = __SkuInfo(x['_id'], x['skuBundleId'], x['category_id'],x['mrp'],x['available_price'],x['source_id'],x['rank'],x['maxNlc'], \
58
                           x['minNlc'], x['schemeAmount'],x['minDiscount'],x['maxDiscount'],x['discountType'],x['dp'],x['nlcPoints'],x['bestSellerPoints'], \
59
                           x['totalPoints'],x['in_stock'],x['maxprice'],x['showDeal'])
60
        p.append(s_info)
61
 
62
def writeSheet():
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)
85
    sheet.write(0, 13, "Mrp", heading_xf)
86
    sheet.write(0, 14, "DP", heading_xf)
87
    sheet.write(0, 15, "Scheme Amount", heading_xf)
88
    sheet.write(0, 16, "Discount Type", heading_xf)
89
    sheet.write(0, 17, "Min Discount", heading_xf)
90
    sheet.write(0, 18, "Max Discount", heading_xf)
91
    sheet.write(0, 19, "Max Nlc", heading_xf)
92
    sheet.write(0, 20, "Min Nlc", heading_xf)
93
    sheet.write(0, 21, "Max Price", heading_xf)
94
    sheet.write(0, 22, "Rank", heading_xf)
95
    sheet.write(0, 23, "Nlc Points", heading_xf)
96
    sheet.write(0, 24, "Best Seller Points", heading_xf)
97
    sheet.write(0, 25, "Total Points", heading_xf)
98
    sheet.write(0, 26, "Show Deals", heading_xf)
99
 
100
    it = 1
101
    for x in p:
102
        y = list(get_mongo_connection().Catalog.MasterData.find({'_id':x._id}))
103
        sheet.write(it, 0, x._id)
104
        sheet.write(it, 1, x.skuBundleId)
105
        if x.category_id == 3:
106
            category = 'Mobiles'
107
        else:
108
            category = 'Tablets'
109
        sheet.write(it, 2, category)
110
        sheet.write(it, 3, y[0]['brand'])
111
        sheet.write(it, 4, y[0]['product_name'])
112
        if y[0]['status'] ==1:
113
            sheet.write(it, 5, 'Active')
114
        else:
115
            sheet.write(it, 5, 'EOL')
116
        if y[0]['in_stock'] ==1:
117
            sheet.write(it, 6, 'In Stock')
118
        else:
119
            sheet.write(it, 6, 'Out Of Stock')
120
        sheet.write(it, 7, y[0]['source'])
121
        sheet.write(it, 8, y[0]['identifier'])
122
        sheet.write(it, 9, y[0]['secondaryIdentifier'])
123
        sheet.write(it, 10, y[0]['source_product_name'])
124
        sheet.write(it, 11, y[0]['url'])
125
        sheet.write(it, 12, y[0]['available_price'])
126
        sheet.write(it, 13, x.mrp)
127
        sheet.write(it, 14, x.dp)
128
        sheet.write(it, 15, x.schemeAmount)
129
        sheet.write(it, 16, x.discountType)
130
        sheet.write(it, 17, x.minDiscount)
131
        sheet.write(it, 18, x.maxDiscount)
132
        sheet.write(it, 19, x.maxNlc)
133
        sheet.write(it, 20, x.minNlc)
134
        sheet.write(it, 21, x.maxprice)
135
        sheet.write(it, 22, x.rank)
136
        sheet.write(it, 23, x.nlcPoints)
137
        sheet.write(it, 24, x.bestSellerPoints)
138
        sheet.write(it, 25, x.totalPoints)
139
        sheet.write(it, 26, x.showDeal)
140
        it+=1
141
    filename = "/tmp/deal-data"+str(datetime.now())+".xls" 
142
    wbk.save(filename)
143
    smtpServer = smtplib.SMTP('localhost')
144
    smtpServer.set_debuglevel(1)
145
    sender = 'dtr@shop2020.in'
13896 kshitij.so 146
    #recipients = ['kshitij.sood@saholic.com']
13884 kshitij.so 147
    msg = MIMEMultipart()
148
    msg['Subject'] = "DTR Deals data" + ' - ' + str(datetime.now())
149
    msg['From'] = sender
13896 kshitij.so 150
    recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','manoj.kumar@saholic.com']
13884 kshitij.so 151
    msg['To'] = ",".join(recipients)
152
    fileMsg = email.mime.base.MIMEBase('application','vnd.ms-excel')
153
    fileMsg.set_payload(file(filename).read())
154
    email.encoders.encode_base64(fileMsg)
13898 kshitij.so 155
    fileMsg.add_header('Content-Disposition','attachment;filename=Deal_Sheet_Dtr.xls')
13884 kshitij.so 156
    msg.attach(fileMsg)
157
    try:
158
        smtpServer.sendmail(sender, recipients, msg.as_string())
159
        print "Successfully sent email"
160
    except:
161
        print "Error: unable to send email."
162
 
13893 kshitij.so 163
def sendMail():
13884 kshitij.so 164
    getProducts()
165
    writeSheet()
166
 
13893 kshitij.so 167
def main():
168
    sendMail()
169
 
13884 kshitij.so 170
if __name__=='__main__':
171
    main()