Subversion Repositories SmartDukaan

Rev

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