Subversion Repositories SmartDukaan

Rev

Rev 14427 | Rev 14440 | 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
 
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)
14439 amit.gupta 24
connection = None
14427 amit.gupta 25
def getDbConnection():
14439 amit.gupta 26
    connection = MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
14427 amit.gupta 27
 
28
 
29
ORDERS_QUERY='''
30
select * from order_view order by id desc limit %s offset %s
31
'''
32
 
33
ORDERS_COUNT_QUERY='''
34
select count(*) from order_view
35
'''
36
 
37
ORDER_HTML_QUERY = '''
38
select rawhtml from orders where id = %s
39
'''
40
 
41
 
42
def getOrders(page=1, window=50):
14439 amit.gupta 43
    try:
44
        conn = getDbConnection()
45
        if page==None:
46
            page = 1
47
        if window==None:
48
            window = 50
49
        skip = (page-1)*window
50
        # prepare a cursor object using cursor() method
51
        cursor = conn.cursor()
52
        # Execute the SQL command
53
        # Fetch source id.
54
        cursor.execute(ORDERS_COUNT_QUERY)
55
        result = cursor.fetchall()
56
        total_count = result[0][0]
57
        pages = total_count/window + (0 if total_count%window==0 else 1)
58
 
59
        query = ORDERS_QUERY%(window,skip)
60
        cursor.execute(query)
61
        result = cursor.fetchall()
62
        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>"
63
        tbodyarr = []
64
        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>"
65
        tbodyarr.append(colhead)
66
        for row in result:
67
            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>")
68
 
69
        return template%(total_count, "".join(tbodyarr))    
70
    finally:
71
        conn.close()
14427 amit.gupta 72
def getOrderHtml(orderId):
14439 amit.gupta 73
    try:
74
        conn = getDbConnection()
75
        cursor = conn.cursor()
76
        # Execute the SQL command
77
        # Fetch source id.
78
        cursor.execute(ORDER_HTML_QUERY%(orderId))
79
        result = cursor.fetchall()
80
        return result[0][0]
81
    finally:
82
        conn.close()
14427 amit.gupta 83
 
84
def main():
85
    print getOrders()
86
 
87
 
88
if __name__ == '__main__':
89
    main()