Rev 14423 | Blame | Compare with Previous | Last modification | View Log | RSS feed
import pymongoimport optparseimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom datetime import datetimeITEMS = []RECIPIENTS = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','ritesh.chauhan@saholic.com','khushal.bhatia@saholic.com']con = Noneparser = 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 = skuIdself.skuBundleId = skuBundleIdself.productName = productNameself.source_id = source_idself.rank = rankdef get_mongo_connection(host=options.mongoHost, port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn condef checkItems():global ITEMSuniqueBundles = 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 messagemsg = 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()