Subversion Repositories SmartDukaan

Rev

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

#!/usr/bin/python 

'''
If a order has been marked as out of stock for more than
2 days, it is moved back to the pending queue.

@author: Chandranshu
'''

import datetime
import optparse
import sys

if __name__ == '__main__' and __package__ is None:
    import os
    sys.path.insert(0, os.getcwd())

from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus
from shop2020.model.v1.order.impl import DataService
from shop2020.model.v1.order.impl.DataService import Order
from elixir import session

def move_out_of_stock_orders_to_pending_queue(days, db_hostname):
    DataService.initialize(db_hostname=db_hostname, echoOn=True)
    current_time = datetime.datetime.now()
    to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
    to_datetime = to_datetime - datetime.timedelta(days-1)
    from_datetime = to_datetime - datetime.timedelta(1)
    orders = Order.query.filter_by(status=OrderStatus.INVENTORY_LOW).filter(Order.outofstock_timestamp >= from_datetime).filter(Order.outofstock_timestamp <= to_datetime).all()
    for order in orders:
        order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
        order.statusDescription = 'Moved back to pending queue'
    session.commit()

def main():
    parser = optparse.OptionParser()
    parser.add_option("-d", "--days", dest="days",
                   default=2, type="int",
                   help="move orders marked as out of stock in last NUM days",
                   metavar="NUM")
    parser.add_option("-H", "--host", dest="hostname",
                      default="localhost",
                      type="string", help="The HOST where the DB server is running",
                      metavar="HOST")
    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
    move_out_of_stock_orders_to_pending_queue(options.days, options.hostname)

if __name__ == '__main__':
    main()