Subversion Repositories SmartDukaan

Rev

Rev 16359 | Rev 16361 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16356 manas 1
import MySQLdb
2
import xlwt
3
from email import encoders
4
from email.mime.base import MIMEBase
5
from email.mime.multipart import MIMEMultipart
6
from xlrd import open_workbook
7
from xlutils.copy import copy
8
import smtplib
9
from dtr.storage.DataService import brand_preferences
10
 
11
DB_HOST = "localhost"
12
DB_USER = "root"
13
DB_PASSWORD = "shop2020"
14
DB_NAME = "dtr"
15
#TMP_FILE = "/home/kshitij/Downloads/User_Segmentation_Links.xls"
16
TMP_FILE = "User_Segmentation_Links.xls"
17
 
18
SENDER = "cnc.center@shop2020.in"
19
PASSWORD = "5h0p2o2o"
20
SUBJECT = "User Segmentation Report"
21
SMTP_SERVER = "smtp.gmail.com"
22
SMTP_PORT = 587    
23
 
24
date_format = xlwt.XFStyle()
25
date_format.num_format_str = 'yyyy/mm/dd'
26
 
27
datetime_format = xlwt.XFStyle()
28
datetime_format.num_format_str = 'yyyy/mm/dd HH:MM AM/PM'
29
 
30
default_format = xlwt.XFStyle()
31
 
32
USER_SEGMENTATION_LINK_QUERY="""
16359 manas 33
select x.user_id,x.brand,x.category_id,x.count,x.source,x.weight 
16356 manas 34
  from
35
    (
16359 manas 36
    select c.user_id,c.brand,c.category_id,count(c.id) as count,"clicks" as source,(uw.weightage * count(c.id)) as weight 
16356 manas 37
    from clicks c join user_segmentation_weigthage uw on "clicks"=uw.weightage_type
38
    where  c.category_id is not null and c.category_id !=0 and c.brand is not null and 
16359 manas 39
    c.brand not like '' and c.user_id!=0 group by c.category_id,c.brand,c.user_id
16356 manas 40
 
41
    UNION
16359 manas 42
 
43
    select m.userId as user_id,m.brand,m.categoryId as category_id,count(1) as count,"orders" as source,(uw.weightage * count(1)) as weight
44
    from merchantsuborders m join user_segmentation_weigthage uw on "orders"=uw.weightage_type 
45
    where m.categoryId is not null and m.brand is not null and m.userId!=0 group by m.categoryId,m.brand,m.userId
16356 manas 46
 
47
    UNION
16359 manas 48
 
49
    select fo.user_id,fo.brand,fo.category as category_id,count(fo.id) as count,"orders" as source, (uw.weightage * count(fo.id)) as weight
16356 manas 50
    from flipkartorders fo join user_segmentation_weigthage uw on "orders"=uw.weightage_type 
16359 manas 51
    where  fo.category is not null and fo.brand is not null and  fo.brand not like '' and fo.user_id!=0 and fo.user_id is not null group by fo.category,fo.brand,fo.user_id
52
 
16356 manas 53
    UNION
16359 manas 54
 
16360 manas 55
    select bp.user_id,bp.brand,bp.category_id,count(1) as count,"preferences" as source,(uw.weightage* count(1)) as weight
16359 manas 56
    from brand_preferences bp join user_segmentation_weigthage uw on "preferences"=uw.weightage_type  
57
    where user_id!=0 and status='show' group by category_id,brand,user_id;
16356 manas 58
 
16359 manas 59
) as x
60
 
16356 manas 61
"""
62
 
63
 
64
def getDbConnection():
65
    return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
66
 
67
def generateUserSegmentationLinksReport():
68
    datesql= USER_SEGMENTATION_LINK_QUERY
69
    conn = getDbConnection()
70
 
71
    global workbook
72
    workbook = xlwt.Workbook()
73
 
74
    worksheet = workbook.add_sheet("User Segmentation")
75
    boldStyle = xlwt.XFStyle()
76
 
77
    style = xlwt.XFStyle()
78
    pattern = xlwt.Pattern()
79
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN
80
    pattern.pattern_fore_colour = xlwt.Style.colour_map['yellow']
81
    style.pattern = pattern
82
    f = xlwt.Font()
83
    f.bold = True
84
    boldStyle.font = f
85
 
86
    column = 0
87
    row = 0
88
    worksheet.write(row,column,'User Id',style)
89
    worksheet.write(row,column+1,'Product Name',style)
90
    worksheet.write(row,column+2,'Brand',style)
91
    worksheet.write(row,column+3,'Category Id',style)
92
    worksheet.write(row,column+4,'Count',style)
93
    worksheet.write(row,column+5,'Source',style)
94
    worksheet.write(row,column+6,'Weight',style)
95
 
96
    cursor = conn.cursor()
97
    cursor.execute(datesql)
98
    result = cursor.fetchall()
99
 
100
    for r in result:
101
        row=row+1
102
        worksheet.write(row,column,r[0])
103
        worksheet.write(row,column+1,r[1])
104
        worksheet.write(row,column+2,r[2])
105
        worksheet.write(row,column+3,r[3])
106
        worksheet.write(row,column+4,r[4])
107
        worksheet.write(row,column+5,r[5])
108
        worksheet.write(row,column+6,r[6])
109
    conn.close()
110
    workbook.save(TMP_FILE)   
111
 
112
 
113
def addUsersForLinks():
114
    user_id=''
115
    brand=''
116
    category_id='' 
117
    source=''
118
    count=''
119
    weight=''
120
 
121
    datesql= USER_SEGMENTATION_LINK_QUERY
122
    conn = getDbConnection()
123
    cursor = conn.cursor()
124
    cursor.execute(datesql)
125
    result = cursor.fetchall()
126
 
127
    for r in result:
128
        user_id=r[0]
16359 manas 129
        brand=r[1]
130
        if r[2] == 'Mobiles':
16356 manas 131
            category_id=3
16359 manas 132
        elif r[2] == 'Tablets':
16356 manas 133
            category_id=5
134
        else:
16359 manas 135
            category_id=r[2] 
136
        source=r[3]
16356 manas 137
        count=r[4]
16359 manas 138
        weight=r[5]
16356 manas 139
 
16359 manas 140
        sql = "insert into userlinkssegmentation (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
141
        cursor.execute(sql,(user_id,brand, category_id, source, count,weight))
16356 manas 142
        conn.commit()
143
 
144
    conn.close()
145
 
146
def deleteUserForLinks():
147
    conn = getDbConnection()
148
    cursor = conn.cursor()
149
    deleteAllUserGroups = "delete from userlinkssegmentation"
150
    try:
151
        cursor.execute(deleteAllUserGroups)
152
        conn.commit()
153
    except:
154
        conn.rollback()
155
    finally:
156
        conn.close()    
157
 
158
def main():
159
    #generateUserSegmentationLinksReport()
160
    deleteUserForLinks()
161
    addUsersForLinks()
162
 
163
def sendmail(email, message, fileName, title):
164
    if email == "":
165
        return
166
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
167
    mailServer.ehlo()
168
    mailServer.starttls()
169
    mailServer.ehlo()
170
    msg = MIMEMultipart()
171
    msg['Subject'] = title
172
    msg.preamble = title
173
    fileMsg = MIMEBase('application', 'vnd.ms-excel')
174
    fileMsg.set_payload(file(TMP_FILE).read())
175
    encoders.encode_base64(fileMsg)
176
    fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
177
    msg.attach(fileMsg)
178
    MAILTO = ['manas.kapoor@saholic.com']
179
    mailServer.login(SENDER, PASSWORD)
180
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
181
 
182
 
183
if __name__=='__main__':
16359 manas 184
    main()