Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
14726 kshitij.so 1
from elixir import *
2
from shop2020.model.v1.order.impl import DataService
3
from shop2020.thriftpy.model.v1.order.ttypes import RechargeOrderStatus
4
from shop2020.model.v1.order.impl.model.RechargeOrder import RechargeOrder
5
from shop2020.model.v1.order.impl.model.MobileRechargeOrder import MobileRechargeOrder
6
from shop2020.model.v1.order.impl.model.DTHRechargeOrder import DTHRechargeOrder
7
import optparse
8
import smtplib
9
from email.mime.text import MIMEText
10
from email.mime.multipart import MIMEMultipart
11
from datetime import datetime, timedelta
12
from sqlalchemy.sql import func
13
 
14
user_list = []
20172 aman.kumar 15
RECIPIENTS = ['rajneesh.arora@saholic.com','khushal.bhatia@saholic.com','kshitij.sood@saholic.com','anikendra.das@saholic.com','amit.gupta@saholic.com']
14726 kshitij.so 16
 
17
class __Users():
18
 
19
    def __init__(self, id, email, amount):
20
        self.id = id
21
        self.email = email
22
        self.amount = amount
23
 
24
def getHighVolumeTransactions():
25
    global user_list
26
    d = datetime.now()
14727 kshitij.so 27
    d = d + timedelta(days = -1)
14726 kshitij.so 28
    todate = datetime(d.year, d.month, d.day)
29
    tomorrow = todate + timedelta(days=1)
30
 
31
    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)
32
    for all_user in all_users:
33
        if all_user[0] >=2000:
34
            user = __Users(all_user[1], all_user[2], all_user[0]) 
35
            user_list.append(user)
36
 
37
def sendAlert():
38
    message="""<html>
39
            <body>
40
            <h3>Users with success amount >= 2000</h3>
41
            <table border="1" style="width:100%;">
42
            <thead>
43
            <tr><th>User Id</th>
44
            <th>Email</th>
45
            <th>Amount</th>
46
            </tr></thead>
47
            <tbody>"""
48
    if len(user_list) == 0:
49
        message+="""<tr>
50
        <td colspan="3" style="text-align:center">"""+"No Record found"+"""</td>
51
        </tr>"""
52
    else:
53
        for user in user_list:
54
            message+="""<tr>
55
            <td style="text-align:center">"""+str(user.id)+"""</td>
56
            <td style="text-align:center">"""+(user.email)+"""</td>
57
            <td style="text-align:center">"""+str(user.amount)+"""</td>
58
            </tr>"""
59
    message+="""</tbody></table></body></html>"""
60
    print message
61
    msg = MIMEMultipart()
62
    msg['Subject'] = "Previous day high volume transactions" + ' - ' + str(datetime.now())
63
    msg['From'] = ""
64
    msg['To'] = ",".join(RECIPIENTS)
65
    msg.preamble = "Previous day high volume transactions" + ' - ' + str(datetime.now())
66
    html_msg = MIMEText(message, 'html')
67
    msg.attach(html_msg)
68
 
69
    smtpServer = smtplib.SMTP('localhost')
70
    smtpServer.set_debuglevel(1)
71
    sender = 'cnc.center@shop2020.in'
72
    try:
73
        smtpServer.sendmail(sender, RECIPIENTS, msg.as_string())
74
        print "Successfully sent email"
75
    except:
76
        print "Error: unable to send email."
77
 
78
def main():
79
    parser = optparse.OptionParser()
80
    parser.add_option("-H", "--host", dest="hostname",
81
                      default="localhost",
82
                      type="string", help="The HOST where the DB server is running",
83
                      metavar="HOST")
84
    (options, args) = parser.parse_args()
85
    try:
86
        DataService.initialize(db_hostname=options.hostname, echoOn=True)
87
        getHighVolumeTransactions()
88
        sendAlert()
89
    finally:
90
        session.close()
91
if __name__=='__main__':
92
    main()