Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1216 chandransh 1
#!/usr/bin/python 
2
 
4090 chandransh 3
'''
4
If a order has been marked as out of stock for more than
5
2 days, it is moved back to the pending queue.
6
 
7
@author: Chandranshu
8
'''
9
 
1216 chandransh 10
import datetime
11
import optparse
12
import sys
13
 
14
if __name__ == '__main__' and __package__ is None:
15
    import os
16
    sys.path.insert(0, os.getcwd())
17
 
18
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus
19
from shop2020.model.v1.order.impl import DataService
20
from shop2020.model.v1.order.impl.DataService import Order
1940 chandransh 21
from elixir import session
1216 chandransh 22
 
4016 chandransh 23
def move_out_of_stock_orders_to_pending_queue(days, db_hostname):
24
    DataService.initialize(db_hostname=db_hostname, echoOn=True)
1217 chandransh 25
    current_time = datetime.datetime.now()
26
    to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
27
    to_datetime = to_datetime - datetime.timedelta(days-1)
28
    from_datetime = to_datetime - datetime.timedelta(1)
29
    orders = Order.query.filter_by(status=OrderStatus.INVENTORY_LOW).filter(Order.outofstock_timestamp >= from_datetime).filter(Order.outofstock_timestamp <= to_datetime).all()
1216 chandransh 30
    for order in orders:
31
        order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
4032 chandransh 32
        order.statusDescription = 'Moved back to pending queue'
1216 chandransh 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 marked as out of stock in last NUM days",
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")
1216 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_out_of_stock_orders_to_pending_queue(options.days, options.hostname)
1216 chandransh 49
 
50
if __name__ == '__main__':
51
    main()