Subversion Repositories SmartDukaan

Rev

Rev 14423 | Blame | Compare with Previous | Last modification | View Log | RSS feed

import pymongo
import optparse
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime

ITEMS = []
RECIPIENTS = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','ritesh.chauhan@saholic.com','khushal.bhatia@saholic.com']

con = None
parser = optparse.OptionParser()
parser.add_option("-m", "--m", dest="mongoHost",
                      default="localhost",
                      type="string", help="The HOST where the mongo server is running",
                      metavar="mongo_host")

(options, args) = parser.parse_args()

class __Items():
    
    def __init__(self, skuId, skuBundleId ,productName, source_id, rank):
        self.skuId = skuId
        self.skuBundleId = skuBundleId
        self.productName = productName
        self.source_id = source_id
        self.rank = rank
        
    

def get_mongo_connection(host=options.mongoHost, 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 checkItems():
    global ITEMS
    uniqueBundles = get_mongo_connection().Catalog.Deals.find({'rank':{"$gt":0}}).distinct('skuBundleId')
    dealData = get_mongo_connection().Catalog.Deals.find({"$and":[{"skuBundleId":{"$in":uniqueBundles}},{"$or":[{"maxNlc":None},{"minNlc":None}]}]})
    for deal in dealData:
        masterItem = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
        item = __Items(deal['_id'], deal['skuBundleId'] ,masterItem[0]['product_name'],deal['source_id'],deal['rank'])
        ITEMS.append(item)

def sendMail():
    message="""<html>
            <body>
            <h3>No Max/Min NLC for BestSellers</h3>
            <table border="1" style="width:100%;">
            <thead>
            <tr><th>Item Id</th>
            <th>Catalog Item Id</th>
            <th>Product Name</th>
            <th>Rank</th>
            </tr></thead>
            <tbody>"""
    for item in ITEMS:
        message+="""<tr>
        <td style="text-align:center">"""+str(item.skuId)+"""</td>
        <td style="text-align:center">"""+str(item.skuBundleId)+"""</td>
        <td style="text-align:center">"""+(item.productName)+"""</td>
        <td style="text-align:center">"""+str(item.rank)+"""</td>
        </tr>"""
    message+="""</tbody></table></body></html>"""
    print message
    msg = MIMEMultipart()
    msg['Subject'] = "No NLC Items" + ' - ' + str(datetime.now())
    msg['From'] = ""
    msg['To'] = ",".join(RECIPIENTS)
    msg.preamble = "No NLC Items" + ' - ' + str(datetime.now())
    html_msg = MIMEText(message, 'html')
    msg.attach(html_msg)
    
    smtpServer = smtplib.SMTP('localhost')
    smtpServer.set_debuglevel(1)
    sender = 'dtr@shop2020.in'
    try:
        smtpServer.sendmail(sender, RECIPIENTS, msg.as_string())
        print "Successfully sent email"
    except:
        print "Error: unable to send email."
    

if __name__=='__main__':
    checkItems()
    sendMail()