Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
14772 kshitij.so 1
'''Created on Mar 10, 2015
2
 
3
@author: amit
4
'''
5
from datetime import date
6
from email import encoders
7
from email.mime.base import MIMEBase
8
from email.mime.multipart import MIMEMultipart
9
from email.mime.text import MIMEText
10
import MySQLdb
11
import datetime
12
import smtplib
13
import xlwt
14
 
15
 
16
 
17
DB_HOST = "localhost"
18
DB_USER = "root"
19
DB_PASSWORD = "shop2020"
20
DB_NAME = "dtr"
21
TMP_FILE = "/tmp/useractivationreport.xls"  
22
 
23
# KEY NAMES
24
SENDER = "cnc.center@shop2020.in"
25
PASSWORD = "5h0p2o2o"
26
SUBJECT = "DTR User Activation report for " + date.today().isoformat()
27
SMTP_SERVER = "smtp.gmail.com"
28
SMTP_PORT = 587    
29
 
30
 
31
SEGMENTATION_QUERY = """
32
SELECT id,email,first_name,mobile_number,mobile_verified,referrer,created 
33
FROM users WHERE lower(referrer) not like 'emp%'
34
"""
35
USER_QUERY="""
36
SELECT id,email,first_name,mobile_number,mobile_verified,referrer,created 
37
FROM users WHERE (referrer IS NULL AND id > 350) OR lower(referrer) not like 'emp%' 
38
"""
39
 
40
 
41
date_format = xlwt.XFStyle()
42
date_format.num_format_str = 'yyyy/mm/dd'
43
 
44
datetime_format = xlwt.XFStyle()
45
datetime_format.num_format_str = 'yyyy/mm/dd HH:MM AM/PM'
46
 
47
default_format = xlwt.XFStyle()
48
 
49
 
50
def getDbConnection():
51
    return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
52
 
53
 
54
def generateActivationReport():
55
    selectSql = USER_QUERY
56
    conn = getDbConnection()
57
    try:
58
        # prepare a cursor object using cursor() method
59
        cursor = conn.cursor()
60
        # Execute the SQL command
61
        # Fetch source id.
62
        cursor.execute(selectSql)
63
        result = cursor.fetchall()
64
 	workbook = xlwt.Workbook()
65
        worksheet = workbook.add_sheet("User")
66
        boldStyle = xlwt.XFStyle()
67
        f = xlwt.Font()
68
        f.bold = True
69
        boldStyle.font = f
70
        column = 0
71
        row = 0
72
 
73
        worksheet.write(row, 0, 'User Id', boldStyle)
74
        worksheet.write(row, 1, 'Email', boldStyle)
75
        worksheet.write(row, 2, 'Name', boldStyle)
76
        worksheet.write(row, 3, 'Mobile Number', boldStyle)
77
        worksheet.write(row, 4, 'Mobile Verified', boldStyle)
78
        worksheet.write(row, 5, 'Referrer', boldStyle)
79
        worksheet.write(row, 6, 'Created', boldStyle)
80
 
81
        for r in result:
82
            row += 1
83
            column = 0
84
            for data in r :
85
                worksheet.write(row, column, int(data) if type(data) is float else data, datetime_format if type(data) is datetime.datetime else default_format)
86
                column += 1
87
        workbook.save(TMP_FILE)
20046 rajender 88
        sendmail(["rajender.singh@shop2020.in",'amit.sirohi@saholic.com','shailesh.kumar@saholic.com'], "", TMP_FILE, SUBJECT)
14772 kshitij.so 89
    except:
90
        print "Could not create report"
91
 
92
 
93
def sendmail(email, message, fileName, title):
94
    if email == "":
95
        return
96
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
97
    mailServer.ehlo()
98
    mailServer.starttls()
99
    mailServer.ehlo()
100
 
101
    # Create the container (outer) email message.
102
    msg = MIMEMultipart()
103
    msg['Subject'] = title
104
    msg.preamble = title
105
    html_msg = MIMEText(message, 'html')
106
    msg.attach(html_msg)
107
 
108
    fileMsg = MIMEBase('application', 'vnd.ms-excel')
109
    fileMsg.set_payload(file(TMP_FILE).read())
110
    encoders.encode_base64(fileMsg)
111
    fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
112
    msg.attach(fileMsg)
113
 
14804 kshitij.so 114
#    fileMsg1 = MIMEBase('application', 'vnd.ms-excel')
115
#    fileMsg1.set_payload(file("/root/snapdeal.csv" ).read())
116
#    encoders.encode_base64(fileMsg1)
117
#    fileMsg1.add_header('Content-Disposition', 'attachment;filename=Affiliate')
118
#    msg.attach(fileMsg1)
119
 
20046 rajender 120
    MAILTO = ['rajender.singh@saholic.com','amit.sirohi@saholic.com','shailesh.kumar@saholic.com']
14772 kshitij.so 121
    mailServer.login(SENDER, PASSWORD)
122
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
123
 
124
def main():
125
    generateActivationReport()
126
 
127
 
128
if __name__ == '__main__':
129
    main()
130
 
131
 
132