Subversion Repositories SmartDukaan

Rev

Rev 4016 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2879 chandransh 1
#!/usr/bin/python
2
 
3
'''
4090 chandransh 4
If a order has been in the payment pending state for more
5
than 2 days, it marks it as failed.
6
 
2879 chandransh 7
Created on 09-Aug-2011
8
 
9
@author: chandranshu
10
'''
11
 
12
import datetime
13
import optparse
14
import sys
15
 
16
if __name__ == '__main__' and __package__ is None:
17
    import os
18
    sys.path.insert(0, os.getcwd())
19
 
20
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus
21
from shop2020.model.v1.order.impl import DataService
22
from shop2020.model.v1.order.impl.DataService import Order
23
from elixir import session
24
 
4016 chandransh 25
def move_payment_pending_orders_to_failed_state(days, db_hostname):
26
    DataService.initialize(db_hostname=db_hostname, echoOn=True)
2879 chandransh 27
    current_time = datetime.datetime.now()
28
    to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
29
    to_datetime = to_datetime - datetime.timedelta(days)
30
    orders = Order.query.filter_by(status=OrderStatus.PAYMENT_PENDING).filter(Order.created_timestamp <= to_datetime).all()
31
    for order in orders:
32
        order.status = OrderStatus.PAYMENT_FAILED
33
    session.commit()
34
 
35
def main():
36
    parser = optparse.OptionParser()
37
    parser.add_option("-d", "--days", dest="days",
38
                   default=2, type="int",
39
                   help="move orders in the PAYMENT_PENDING state for more than NUM days to PAYMENT_FAILED state",
40
                   metavar="NUM")
4016 chandransh 41
    parser.add_option("-H", "--host", dest="hostname",
42
                      default="localhost",
43
                      type="string", help="The HOST where the DB server is running",
44
                      metavar="HOST")
2879 chandransh 45
    (options, args) = parser.parse_args()
46
    if len(args) != 0:
47
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
4016 chandransh 48
    move_payment_pending_orders_to_failed_state(options.days, options.hostname)
2879 chandransh 49
 
50
if __name__ == '__main__':
51
    main()