Subversion Repositories SmartDukaan

Rev

Rev 14439 | Go to most recent revision | Details | 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
 
7
from datetime import date
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 datetime
14
import smtplib
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)
24
def getDbConnection():
25
    return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
26
 
27
 
28
ORDERS_QUERY='''
29
select * from order_view order by id desc limit %s offset %s
30
'''
31
 
32
ORDERS_COUNT_QUERY='''
33
select count(*) from order_view
34
'''
35
 
36
ORDER_HTML_QUERY = '''
37
select rawhtml from orders where id = %s
38
'''
39
conn = getDbConnection()
40
 
41
 
42
def getOrders(page=1, window=50):
43
    if page==None:
44
        page = 1
45
    if window==None:
46
        window = 50
47
    skip = (page-1)*window
48
    # prepare a cursor object using cursor() method
49
    cursor = conn.cursor()
50
    # Execute the SQL command
51
    # Fetch source id.
52
    cursor.execute(ORDERS_COUNT_QUERY)
53
    result = cursor.fetchall()
54
    total_count = result[0][0]
55
    pages = total_count/window + (0 if total_count%window==0 else 1)
56
 
57
    query = ORDERS_QUERY%(window,skip)
58
    cursor.execute(query)
59
    result = cursor.fetchall()
60
    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>"
61
    tbodyarr = []
62
    colhead="<tr><th>Id</th><th>User Id</th><th>Store Id</th><th>Order Url</th><th>Subtag</th><th>Statu</th><th>Created on</th></tr>"
63
    tbodyarr.append(colhead)
64
    for row in result:
65
        tbodyarr.append("<tr><td>" + xstr(row[0]) + "</td><td>" + xstr(row[1]) +"</td><td>" + xstr(row[2]) +"</td><td><a target='_blank' href='/rawhtml/" + xstr(row[0]) + "'>" + xstr(row[3])[:90] +"</a></td><td>" + xstr(row[4]) + "</td><td>" + row[5][:15]+ "</td><td>" + row[6].strftime("%Y-%m-%d %H:%M:%S") + "</td></tr>")
66
 
67
    return template%(total_count, "".join(tbodyarr))    
68
    cursor.close()
69
 
70
def getOrderHtml(orderId):
71
    cursor = conn.cursor()
72
    # Execute the SQL command
73
    # Fetch source id.
74
    cursor.execute(ORDER_HTML_QUERY%(orderId))
75
    result = cursor.fetchall()
76
    return result[0][0]
77
 
78
def main():
79
    print getOrders()
80
 
81
 
82
if __name__ == '__main__':
83
    main()