Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
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='''
14725 amit.gupta 30
select o.*, u.username from order_view o join users u on u.id=o.user_id where %s 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='''
14725 amit.gupta 41
select count(*) from order_view o join users u on u.id=o.user_id where %s 
14427 amit.gupta 42
'''
43
 
44
ORDER_HTML_QUERY = '''
45
select rawhtml from orders where id = %s
46
'''
47
 
48
 
14615 amit.gupta 49
 
50
 
14725 amit.gupta 51
def getOrders(page=1, window=50, status=None, user=None):
14439 amit.gupta 52
    try:
14725 amit.gupta 53
        search = "1=1"
54
        if status is not None:
55
            search="o.status='%s'"%(status)
56
        if user is not  None:
57
            search = "o.user_id=%s"%(user)
14439 amit.gupta 58
        conn = getDbConnection()
59
        if page==None:
60
            page = 1
61
        if window==None:
62
            window = 50
63
        skip = (page-1)*window
64
        # prepare a cursor object using cursor() method
65
        cursor = conn.cursor()
66
        # Execute the SQL command
67
        # Fetch source id.
14725 amit.gupta 68
        cursor.execute(ORDERS_COUNT_QUERY%(search))
14439 amit.gupta 69
        result = cursor.fetchall()
70
        total_count = result[0][0]
71
        pages = total_count/window + (0 if total_count%window==0 else 1)
72
 
14725 amit.gupta 73
        query = ORDERS_QUERY%(search,window,skip)
14439 amit.gupta 74
        cursor.execute(query)
75
        result = cursor.fetchall()
76
        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>"
77
        tbodyarr = []
14470 amit.gupta 78
        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 79
        tbodyarr.append(colhead)
80
        for row in result:
14733 amit.gupta 81
            tbodyarr.append("<tr><td>" + xstr(row[0]) + "</td><td>" + xstr(row[1]) +"</td><td><a href=\"/transactions?user=" + xstr(row[1]) + "\">" + xstr(row[-1]) + "</a></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><a href=\"/transactions?status=" + xstr(row[5]) + "\">" + xstr(row[5]) + "</a></td><td>" + row[6].strftime("%Y-%m-%d %H:%M:%S") + "</td></tr>")
14439 amit.gupta 82
 
14440 amit.gupta 83
        return template%(total_count, "".join(tbodyarr))
84
    except:
85
        traceback.print_exc()
14439 amit.gupta 86
    finally:
87
        conn.close()
14427 amit.gupta 88
def getOrderHtml(orderId):
14439 amit.gupta 89
    try:
90
        conn = getDbConnection()
91
        cursor = conn.cursor()
92
        # Execute the SQL command
93
        # Fetch source id.
94
        cursor.execute(ORDER_HTML_QUERY%(orderId))
95
        result = cursor.fetchall()
96
        return result[0][0]
14440 amit.gupta 97
    except:
98
        traceback.print_exc()
14439 amit.gupta 99
    finally:
100
        conn.close()
14615 amit.gupta 101
 
14451 amit.gupta 102
def getOrdersAfterDate(afterDate, storeId):
14460 amit.gupta 103
    try:
104
        conn = getDbConnection()
105
        cursor = conn.cursor()
14602 amit.gupta 106
        afterDate = datetime.strftime(afterDate,"%Y-%m-%d")
107
        beforeDate = datetime.strftime(datetime.now(),"%Y-%m-%d %H")
14513 amit.gupta 108
        cursor.execute(ORDERS_DATE_QUERY%(afterDate, beforeDate, storeId))
14460 amit.gupta 109
        result = cursor.fetchall()
110
        return result
111
    finally:
112
        conn.close() 
14451 amit.gupta 113
 
14468 amit.gupta 114
def getOrdersAfterDate1(afterDate, storeId):
115
    try:
116
        conn = getDbConnection()
117
        cursor = conn.cursor()
118
        afterDate = datetime.strftime(afterDate,"\'%Y-%m-%d\'")
119
        cursor.execute(ORDERS_DATE_QUERY1%(afterDate, storeId))
120
        result = cursor.fetchall()
121
        return result
122
    finally:
123
        conn.close() 
124
 
14526 amit.gupta 125
def getOrdersByTag(tagName, storeId):
126
    try:
127
        conn = getDbConnection()
128
        cursor = conn.cursor()
129
        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"
130
        cursor.execute(query%(tagName, storeId))
131
        result = cursor.fetchall()
132
        return result
133
    finally:
14615 amit.gupta 134
        conn.close()
14526 amit.gupta 135
 
14427 amit.gupta 136
def main():
14460 amit.gupta 137
    print getOrdersAfterDate(datetime.now() - timedelta(days=10), 3)
14427 amit.gupta 138
 
14615 amit.gupta 139
def fetchResult(query, *params):
140
    try:
141
        conn = getDbConnection()
142
        cursor = conn.cursor()
143
        cursor.execute(query.format(params))
144
        result = cursor.fetchall()
145
        return result
146
    finally:
147
        conn.close() 
14427 amit.gupta 148
 
149
if __name__ == '__main__':
150
    main()