Subversion Repositories SmartDukaan

Rev

Rev 16020 | Rev 16869 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

import pymongo
import xlwt
from datetime import datetime
from email.mime.multipart import MIMEMultipart
import email.encoders
import smtplib

con = None

statusMap = {1:'Active',2:'EOL',3:'In Process',4:'Exclusive'}

class __SkuInfo:
    
    def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
                 maxDiscount, discountType, dp, nlcPoints, bestSellerPoints, totalPoints,in_stock,maxprice,showDeal, catalogBestSellerPoints, prepaidDeal):
        self._id = _id
        self.skuBundleId = skuBundleId
        self.category_id = category_id
        self.mrp = mrp
        self.available_price = available_price
        self.source_id = source_id
        self.rank = rank
        self.maxNlc = maxNlc
        self.minNlc = minNlc
        self.schemeAmount = schemeAmount
        self.minDiscount = minDiscount
        self.maxDiscount = maxDiscount
        self.discountType = discountType
        self.dp = dp
        self.nlcPoints = nlcPoints
        self.bestSellerPoints = bestSellerPoints
        self.totalPoints = totalPoints
        self.in_stock = in_stock
        self.maxprice = maxprice
        self.showDeal = showDeal
        self.catalogBestSellerPoints = catalogBestSellerPoints
        self.prepaidDeal = prepaidDeal  

def get_mongo_connection(host='localhost', port=27017):
    global con
    if con is None:
        print "Establishing connection %s host and port %d" %(host,port)
        try:
            con = pymongo.MongoClient(host, port)
        except Exception, e:
            print e
            return None
    return con

def writeSheet():
    p = []
    data = get_mongo_connection().Catalog.Deals.find().sort([('totalPoints',pymongo.DESCENDING)])
    print data.count()
    for x in data:
        if x['source_id'] == 5 and x['rank'] ==0:
            continue
        s_info = __SkuInfo(x['_id'], x['skuBundleId'], x['category_id'],x['mrp'],x['available_price'],x['source_id'],x['rank'],x['maxNlc'], \
                           x['minNlc'], x['schemeAmount'],x['minDiscount'],x['maxDiscount'],x['discountType'],x['dp'],x['nlcPoints'],x['bestSellerPoints'], \
                           x['totalPoints'],x['in_stock'],x['maxprice'],x['showDeal'],x['catalogBestSellerPoints'],x['prepaidDeal'])
        p.append(s_info)
    
    wbk = xlwt.Workbook()
    sheet = wbk.add_sheet('Deals')
    xstr = lambda s: s or ""
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
    
    excel_integer_format = '0'
    integer_style = xlwt.XFStyle()
    integer_style.num_format_str = excel_integer_format

    sheet.write(0, 0, "Item ID", heading_xf)
    sheet.write(0, 1, "Catalog Item Id", heading_xf)
    sheet.write(0, 2, "Category", heading_xf)
    sheet.write(0, 3, "Brand", heading_xf)
    sheet.write(0, 4, "Product Name", heading_xf)
    sheet.write(0, 5, "Item Status", heading_xf)
    sheet.write(0, 6, "Stock Status", heading_xf)
    sheet.write(0, 7, "MarketPlace", heading_xf)
    sheet.write(0, 8, "MarketPlace Identifier", heading_xf)
    sheet.write(0, 9, "MarketPlace Secondary Identifier", heading_xf)
    sheet.write(0, 10, "Product Name (Marketplace)", heading_xf)
    sheet.write(0, 11, "Url", heading_xf)
    sheet.write(0, 12, "Price", heading_xf)
    sheet.write(0, 13, "Mrp", heading_xf)
    sheet.write(0, 14, "DP", heading_xf)
    sheet.write(0, 15, "Scheme Amount", heading_xf)
    sheet.write(0, 16, "Discount Type", heading_xf)
    sheet.write(0, 17, "Min Discount", heading_xf)
    sheet.write(0, 18, "Max Discount", heading_xf)
    sheet.write(0, 19, "Max Nlc", heading_xf)
    sheet.write(0, 20, "Min Nlc", heading_xf)
    sheet.write(0, 21, "Max Price", heading_xf)
    sheet.write(0, 22, "Rank", heading_xf)
    sheet.write(0, 23, "Nlc Points", heading_xf)
    sheet.write(0, 24, "Best Seller Points", heading_xf)
    sheet.write(0, 25, "Catalog Best Seller Points", heading_xf)
    sheet.write(0, 26, "Total Points", heading_xf)
    sheet.write(0, 27, "COD Deal", heading_xf)
    sheet.write(0, 28, "Prepaid Deal", heading_xf)
    
    it = 1
    for x in p:
        y = list(get_mongo_connection().Catalog.MasterData.find({'_id':x._id}))
        sheet.write(it, 0, x._id)
        sheet.write(it, 1, x.skuBundleId)
        if x.category_id == 3:
            category = 'Mobiles'
        else:
            category = 'Tablets'
        sheet.write(it, 2, category)
        sheet.write(it, 3, y[0]['brand'])
        sheet.write(it, 4, y[0]['product_name'])
        sheet.write(it, 5, statusMap.get(y[0]['status']))
        if y[0]['in_stock'] ==1:
            sheet.write(it, 6, 'In Stock')
        else:
            sheet.write(it, 6, 'Out Of Stock')
        sheet.write(it, 7, y[0]['source'])
        sheet.write(it, 8, y[0]['identifier'])
        sheet.write(it, 9, y[0]['secondaryIdentifier'])
        sheet.write(it, 10, y[0]['source_product_name'])
        sheet.write(it, 11, y[0]['marketPlaceUrl'])
        sheet.write(it, 12, y[0]['available_price'])
        sheet.write(it, 13, x.mrp)
        sheet.write(it, 14, x.dp)
        sheet.write(it, 15, x.schemeAmount)
        sheet.write(it, 16, x.discountType)
        sheet.write(it, 17, x.minDiscount)
        sheet.write(it, 18, x.maxDiscount)
        sheet.write(it, 19, x.maxNlc)
        sheet.write(it, 20, x.minNlc)
        sheet.write(it, 21, x.maxprice)
        sheet.write(it, 22, x.rank)
        sheet.write(it, 23, x.nlcPoints)
        sheet.write(it, 24, x.bestSellerPoints)
        sheet.write(it, 25, x.catalogBestSellerPoints)
        sheet.write(it, 26, x.totalPoints)
        sheet.write(it, 27, x.showDeal)
        sheet.write(it, 28, x.prepaidDeal)
        it+=1
    filename = "/tmp/deal-data"+str(datetime.now())+".xls" 
    wbk.save(filename)
    smtpServer = smtplib.SMTP('localhost')
    #smtpServer.set_debuglevel(1)
    sender = 'dtr@shop2020.in'
    #recipients = ['kshitij.sood@saholic.com']
    msg = MIMEMultipart()
    msg['Subject'] = "DTR Deals data" + ' - ' + str(datetime.now())
    msg['From'] = sender
    recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','manoj.kumar@saholic.com']
    msg['To'] = ",".join(recipients)
    fileMsg = email.mime.base.MIMEBase('application','vnd.ms-excel')
    fileMsg.set_payload(file(filename).read())
    email.encoders.encode_base64(fileMsg)
    fileMsg.add_header('Content-Disposition','attachment;filename=Deal_Sheet_Dtr.xls')
    msg.attach(fileMsg)
    try:
        smtpServer.sendmail(sender, recipients, msg.as_string())
        print "Successfully sent email"
    except:
        print "Error: unable to send email."

def sendMail():
    writeSheet()

def main():
    sendMail()

if __name__=='__main__':
    main()