| 14427 |
amit.gupta |
1 |
'''
|
|
|
2 |
Created on Mar 11, 2015
|
|
|
3 |
|
|
|
4 |
@author: amit
|
|
|
5 |
'''
|
|
|
6 |
|
| 14451 |
amit.gupta |
7 |
from datetime import date, datetime, timedelta
|
| 14427 |
amit.gupta |
8 |
from email import encoders
|
|
|
9 |
from email.mime.base import MIMEBase
|
|
|
10 |
from email.mime.multipart import MIMEMultipart
|
|
|
11 |
from email.mime.text import MIMEText
|
|
|
12 |
import MySQLdb
|
|
|
13 |
import smtplib
|
| 14440 |
amit.gupta |
14 |
import traceback
|
| 14427 |
amit.gupta |
15 |
import xlwt
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
DB_HOST = "localhost"
|
|
|
20 |
DB_USER = "root"
|
|
|
21 |
DB_PASSWORD = "shop2020"
|
|
|
22 |
DB_NAME = "dtr"
|
|
|
23 |
xstr = lambda s: '' if s is None else str(s)
|
| 14440 |
amit.gupta |
24 |
|
| 14427 |
amit.gupta |
25 |
def getDbConnection():
|
| 14440 |
amit.gupta |
26 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
| 14427 |
amit.gupta |
27 |
|
|
|
28 |
|
|
|
29 |
ORDERS_QUERY='''
|
| 14470 |
amit.gupta |
30 |
select o.*, u.username from order_view o join users u on u.id=o.user_id order by id desc limit %s offset %s
|
| 14427 |
amit.gupta |
31 |
'''
|
| 14451 |
amit.gupta |
32 |
ORDERS_DATE_QUERY = '''
|
| 14602 |
amit.gupta |
33 |
select o.*, max(d.versioncode), u.username from order_view o join users u on u.id=o.user_id left
|
|
|
34 |
join devices d on (d.user_id=o.user_id and o.created >= d.created) where o.created between '%s' and '%s' and store_id = %s group by o.id
|
| 14451 |
amit.gupta |
35 |
'''
|
| 14468 |
amit.gupta |
36 |
ORDERS_DATE_QUERY1 = '''
|
|
|
37 |
select * from orders where created > %s and store_id = %s
|
|
|
38 |
'''
|
| 14427 |
amit.gupta |
39 |
|
|
|
40 |
ORDERS_COUNT_QUERY='''
|
|
|
41 |
select count(*) from order_view
|
|
|
42 |
'''
|
|
|
43 |
|
|
|
44 |
ORDER_HTML_QUERY = '''
|
|
|
45 |
select rawhtml from orders where id = %s
|
|
|
46 |
'''
|
|
|
47 |
|
|
|
48 |
|
| 14615 |
amit.gupta |
49 |
|
|
|
50 |
|
| 14427 |
amit.gupta |
51 |
def getOrders(page=1, window=50):
|
| 14439 |
amit.gupta |
52 |
try:
|
|
|
53 |
conn = getDbConnection()
|
|
|
54 |
if page==None:
|
|
|
55 |
page = 1
|
|
|
56 |
if window==None:
|
|
|
57 |
window = 50
|
|
|
58 |
skip = (page-1)*window
|
|
|
59 |
# prepare a cursor object using cursor() method
|
|
|
60 |
cursor = conn.cursor()
|
|
|
61 |
# Execute the SQL command
|
|
|
62 |
# Fetch source id.
|
|
|
63 |
cursor.execute(ORDERS_COUNT_QUERY)
|
|
|
64 |
result = cursor.fetchall()
|
|
|
65 |
total_count = result[0][0]
|
|
|
66 |
pages = total_count/window + (0 if total_count%window==0 else 1)
|
|
|
67 |
|
|
|
68 |
query = ORDERS_QUERY%(window,skip)
|
|
|
69 |
cursor.execute(query)
|
|
|
70 |
result = cursor.fetchall()
|
|
|
71 |
template = "<html><head><style>td,th{border:1px solid;padding:2px}</style></head><div>Total Orders: %s</div><br/><body><table style='font-size:12px;border:1px solid grey;border-spacing:0;cell-spacing:0;border-bottom:1px solid grey;border-bottom:1px solid transparent'>%s</table></body></html>"
|
|
|
72 |
tbodyarr = []
|
| 14470 |
amit.gupta |
73 |
colhead="<tr><th>Id</th><th>User Id</th><th>Username</th><th>Store Id</th><th>Order Url</th><th>Subtag</th><th>Statu</th><th>Created on</th></tr>"
|
| 14439 |
amit.gupta |
74 |
tbodyarr.append(colhead)
|
|
|
75 |
for row in result:
|
| 14470 |
amit.gupta |
76 |
tbodyarr.append("<tr><td>" + xstr(row[0]) + "</td><td>" + xstr(row[1]) +"</td><td>" + xstr(row[-1]) + "</td><td>" + xstr(row[2]) +"</td><td><a target='_blank' href='/rawhtml/" + xstr(row[0]) + "'>" + xstr(row[3])[:130] +"</a></td><td>" + xstr(row[4]) + "</td><td>" + row[5]+ "</td><td>" + row[6].strftime("%Y-%m-%d %H:%M:%S") + "</td></tr>")
|
| 14439 |
amit.gupta |
77 |
|
| 14440 |
amit.gupta |
78 |
return template%(total_count, "".join(tbodyarr))
|
|
|
79 |
except:
|
|
|
80 |
traceback.print_exc()
|
| 14439 |
amit.gupta |
81 |
finally:
|
|
|
82 |
conn.close()
|
| 14427 |
amit.gupta |
83 |
def getOrderHtml(orderId):
|
| 14439 |
amit.gupta |
84 |
try:
|
|
|
85 |
conn = getDbConnection()
|
|
|
86 |
cursor = conn.cursor()
|
|
|
87 |
# Execute the SQL command
|
|
|
88 |
# Fetch source id.
|
|
|
89 |
cursor.execute(ORDER_HTML_QUERY%(orderId))
|
|
|
90 |
result = cursor.fetchall()
|
|
|
91 |
return result[0][0]
|
| 14440 |
amit.gupta |
92 |
except:
|
|
|
93 |
traceback.print_exc()
|
| 14439 |
amit.gupta |
94 |
finally:
|
|
|
95 |
conn.close()
|
| 14615 |
amit.gupta |
96 |
|
| 14451 |
amit.gupta |
97 |
def getOrdersAfterDate(afterDate, storeId):
|
| 14460 |
amit.gupta |
98 |
try:
|
|
|
99 |
conn = getDbConnection()
|
|
|
100 |
cursor = conn.cursor()
|
| 14602 |
amit.gupta |
101 |
afterDate = datetime.strftime(afterDate,"%Y-%m-%d")
|
|
|
102 |
beforeDate = datetime.strftime(datetime.now(),"%Y-%m-%d %H")
|
| 14513 |
amit.gupta |
103 |
cursor.execute(ORDERS_DATE_QUERY%(afterDate, beforeDate, storeId))
|
| 14460 |
amit.gupta |
104 |
result = cursor.fetchall()
|
|
|
105 |
return result
|
|
|
106 |
finally:
|
|
|
107 |
conn.close()
|
| 14451 |
amit.gupta |
108 |
|
| 14468 |
amit.gupta |
109 |
def getOrdersAfterDate1(afterDate, storeId):
|
|
|
110 |
try:
|
|
|
111 |
conn = getDbConnection()
|
|
|
112 |
cursor = conn.cursor()
|
|
|
113 |
afterDate = datetime.strftime(afterDate,"\'%Y-%m-%d\'")
|
|
|
114 |
cursor.execute(ORDERS_DATE_QUERY1%(afterDate, storeId))
|
|
|
115 |
result = cursor.fetchall()
|
|
|
116 |
return result
|
|
|
117 |
finally:
|
|
|
118 |
conn.close()
|
|
|
119 |
|
| 14526 |
amit.gupta |
120 |
def getOrdersByTag(tagName, storeId):
|
|
|
121 |
try:
|
|
|
122 |
conn = getDbConnection()
|
|
|
123 |
cursor = conn.cursor()
|
|
|
124 |
query = "select o.* from users u join order_view o on u.id = o.user_id join clicks c on c.user_id = u.id where c.tag = '%s' and o.status='ORDER_CREATED' and o.store_id=%s"
|
|
|
125 |
cursor.execute(query%(tagName, storeId))
|
|
|
126 |
result = cursor.fetchall()
|
|
|
127 |
return result
|
|
|
128 |
finally:
|
| 14615 |
amit.gupta |
129 |
conn.close()
|
| 14526 |
amit.gupta |
130 |
|
| 14427 |
amit.gupta |
131 |
def main():
|
| 14460 |
amit.gupta |
132 |
print getOrdersAfterDate(datetime.now() - timedelta(days=10), 3)
|
| 14427 |
amit.gupta |
133 |
|
| 14615 |
amit.gupta |
134 |
def fetchResult(query, *params):
|
|
|
135 |
try:
|
|
|
136 |
conn = getDbConnection()
|
|
|
137 |
cursor = conn.cursor()
|
|
|
138 |
cursor.execute(query.format(params))
|
|
|
139 |
result = cursor.fetchall()
|
|
|
140 |
return result
|
|
|
141 |
finally:
|
|
|
142 |
conn.close()
|
| 14427 |
amit.gupta |
143 |
|
|
|
144 |
if __name__ == '__main__':
|
|
|
145 |
main()
|