Subversion Repositories SmartDukaan

Rev

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

from elixir import *
from shop2020.model.v1.order.impl import DataService
from shop2020.thriftpy.model.v1.order.ttypes import RechargeOrderStatus
from shop2020.model.v1.order.impl.model.RechargeOrder import RechargeOrder
from shop2020.model.v1.order.impl.model.MobileRechargeOrder import MobileRechargeOrder
from shop2020.model.v1.order.impl.model.DTHRechargeOrder import DTHRechargeOrder
import optparse
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime, timedelta
from sqlalchemy.sql import func

user_list = []
RECIPIENTS = ['rajneesh.arora@saholic.com','khushal.bhatia@saholic.com','kshitij.sood@saholic.com','anikendra.das@saholic.com','amit.gupta@saholic.com']

class __Users():
    
    def __init__(self, id, email, amount):
        self.id = id
        self.email = email
        self.amount = amount

def getHighVolumeTransactions():
    global user_list
    d = datetime.now()
    d = d + timedelta(days = -1)
    todate = datetime(d.year, d.month, d.day)
    tomorrow = todate + timedelta(days=1)
    
    all_users= session.query(func.sum(RechargeOrder.totalAmount),RechargeOrder.userId, RechargeOrder.userEmailId).filter(RechargeOrder.creationTimestamp.between(todate, tomorrow)).filter(RechargeOrder.status == RechargeOrderStatus.RECHARGE_SUCCESSFUL).group_by(RechargeOrder.userId)
    for all_user in all_users:
        if all_user[0] >=2000:
            user = __Users(all_user[1], all_user[2], all_user[0]) 
            user_list.append(user)

def sendAlert():
    message="""<html>
            <body>
            <h3>Users with success amount >= 2000</h3>
            <table border="1" style="width:100%;">
            <thead>
            <tr><th>User Id</th>
            <th>Email</th>
            <th>Amount</th>
            </tr></thead>
            <tbody>"""
    if len(user_list) == 0:
        message+="""<tr>
        <td colspan="3" style="text-align:center">"""+"No Record found"+"""</td>
        </tr>"""
    else:
        for user in user_list:
            message+="""<tr>
            <td style="text-align:center">"""+str(user.id)+"""</td>
            <td style="text-align:center">"""+(user.email)+"""</td>
            <td style="text-align:center">"""+str(user.amount)+"""</td>
            </tr>"""
    message+="""</tbody></table></body></html>"""
    print message
    msg = MIMEMultipart()
    msg['Subject'] = "Previous day high volume transactions" + ' - ' + str(datetime.now())
    msg['From'] = ""
    msg['To'] = ",".join(RECIPIENTS)
    msg.preamble = "Previous day high volume transactions" + ' - ' + str(datetime.now())
    html_msg = MIMEText(message, 'html')
    msg.attach(html_msg)
    
    smtpServer = smtplib.SMTP('localhost')
    smtpServer.set_debuglevel(1)
    sender = 'cnc.center@shop2020.in'
    try:
        smtpServer.sendmail(sender, RECIPIENTS, msg.as_string())
        print "Successfully sent email"
    except:
        print "Error: unable to send email."

def main():
    parser = optparse.OptionParser()
    parser.add_option("-H", "--host", dest="hostname",
                      default="localhost",
                      type="string", help="The HOST where the DB server is running",
                      metavar="HOST")
    (options, args) = parser.parse_args()
    try:
        DataService.initialize(db_hostname=options.hostname, echoOn=True)
        getHighVolumeTransactions()
        sendAlert()
    finally:
        session.close()
if __name__=='__main__':
    main()