Subversion Repositories SmartDukaan

Rev

Rev 16357 | Go to most recent revision | Details | 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="""
33
select x.user_id,x.product_name,x.brand,x.category_id,x.count,x.source,x.weight 
34
  from
35
    (
36
    select c.user_id,c.product_name,c.brand,c.category_id,count(c.id) as count,"clicks" as source,(uw.weightage * count(c.id)) as weight 
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 
39
    c.brand not like '' and c.user_id!=0 group by c.category_id,c.brand,c.store_product_id,c.user_id
40
 
41
    UNION
42
 
43
    select m.userId as user_id,m.productName as product_name,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.productCode,m.userId
46
 
47
    UNION
48
 
49
    select fo.user_id,fo.title as product_name,fo.brand,fo.category as category_id,count(fo.id) as count,"orders" as source, (uw.weightage * count(fo.id)) as weight
50
    from flipkartorders fo join user_segmentation_weigthage uw on "orders"=uw.weightage_type 
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.catalogId,fo.user_id
52
 
53
    UNION
54
 
55
    select uf.user_id,uf.filters as product_name,"NULL" as brand,"NULL" as category_id,count(1) as count,
56
    "filters" as source,(uw.weightage * count(1)) as weight from user_filters uf join user_segmentation_weigthage uw on "filters"=uw.weightage_type  where uf.type='brand' group by uf.filters,uf.user_id
57
    ) as x 
58
 
59
"""
60
 
61
 
62
def getDbConnection():
63
    return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
64
 
65
def generateUserSegmentationLinksReport():
66
    datesql= USER_SEGMENTATION_LINK_QUERY
67
    conn = getDbConnection()
68
 
69
    global workbook
70
    workbook = xlwt.Workbook()
71
 
72
    worksheet = workbook.add_sheet("User Segmentation")
73
    boldStyle = xlwt.XFStyle()
74
 
75
    style = xlwt.XFStyle()
76
    pattern = xlwt.Pattern()
77
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN
78
    pattern.pattern_fore_colour = xlwt.Style.colour_map['yellow']
79
    style.pattern = pattern
80
    f = xlwt.Font()
81
    f.bold = True
82
    boldStyle.font = f
83
 
84
    column = 0
85
    row = 0
86
    worksheet.write(row,column,'User Id',style)
87
    worksheet.write(row,column+1,'Product Name',style)
88
    worksheet.write(row,column+2,'Brand',style)
89
    worksheet.write(row,column+3,'Category Id',style)
90
    worksheet.write(row,column+4,'Count',style)
91
    worksheet.write(row,column+5,'Source',style)
92
    worksheet.write(row,column+6,'Weight',style)
93
 
94
    cursor = conn.cursor()
95
    cursor.execute(datesql)
96
    result = cursor.fetchall()
97
 
98
    for r in result:
99
        row=row+1
100
        worksheet.write(row,column,r[0])
101
        worksheet.write(row,column+1,r[1])
102
        worksheet.write(row,column+2,r[2])
103
        worksheet.write(row,column+3,r[3])
104
        worksheet.write(row,column+4,r[4])
105
        worksheet.write(row,column+5,r[5])
106
        worksheet.write(row,column+6,r[6])
107
    conn.close()
108
    workbook.save(TMP_FILE)   
109
 
110
 
111
def addUsersForLinks():
112
    user_id=''
113
    product_name=''
114
    brand=''
115
    category_id='' 
116
    source=''
117
    count=''
118
    weight=''
119
 
120
    datesql= USER_SEGMENTATION_LINK_QUERY
121
    conn = getDbConnection()
122
    cursor = conn.cursor()
123
    cursor.execute(datesql)
124
    result = cursor.fetchall()
125
 
126
    for r in result:
127
        print r[3]
128
        user_id=r[0]
129
        product_name=r[1]
130
        brand=r[2]
131
        if r[3] == 'Mobiles':
132
            category_id=3
133
        elif r[3] == 'Tablets':
134
            category_id=5
135
        else:
136
            category_id=r[3] 
137
        source=r[5]
138
        count=r[4]
139
        weight=r[6]
140
 
141
        sql = "insert into userlinkssegmentation (user_id, product_name, brand, category_id, source, count,weight) values(%s, '%s', '%s', %s, '%s', %s,%s)" %(user_id,product_name,brand, category_id, source, count,weight)
142
        print sql
143
        cursor.execute(sql)
144
        conn.commit()
145
 
146
    conn.close()
147
 
148
def deleteUserForLinks():
149
    conn = getDbConnection()
150
    cursor = conn.cursor()
151
    deleteAllUserGroups = "delete from userlinkssegmentation"
152
    try:
153
        cursor.execute(deleteAllUserGroups)
154
        conn.commit()
155
    except:
156
        conn.rollback()
157
    finally:
158
        conn.close()    
159
 
160
def main():
161
    #generateUserSegmentationLinksReport()
162
    deleteUserForLinks()
163
    addUsersForLinks()
164
 
165
def sendmail(email, message, fileName, title):
166
    if email == "":
167
        return
168
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
169
    mailServer.ehlo()
170
    mailServer.starttls()
171
    mailServer.ehlo()
172
    msg = MIMEMultipart()
173
    msg['Subject'] = title
174
    msg.preamble = title
175
    fileMsg = MIMEBase('application', 'vnd.ms-excel')
176
    fileMsg.set_payload(file(TMP_FILE).read())
177
    encoders.encode_base64(fileMsg)
178
    fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
179
    msg.attach(fileMsg)
180
    MAILTO = ['manas.kapoor@saholic.com']
181
    mailServer.login(SENDER, PASSWORD)
182
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
183
 
184
 
185
if __name__=='__main__':
186
    main()