Subversion Repositories SmartDukaan

Rev

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

#!/usr/bin/python
'''
Loads data into WarehouseAllocation table which is used to determine
the logistics location of the warehouse which will be used to fulful
orders for this pincode.

@author: Chandranshu
'''
import optparse
import xlrd
from elixir import *

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

from shop2020.logistics.service.impl import DataService
from shop2020.logistics.service.impl.DataService import WarehouseAllocation
from shop2020.thriftpy.logistics.ttypes import WarehouseLocation

def load_warehouse_allocations(filename, db_hostname):
    DataService.initialize(dbname='logistics', db_hostname=db_hostname)
    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(0)
    num_rows = sheet.nrows
    for rownum in range(1, num_rows):
        pincode, warehouse_loc = sheet.row_values(rownum)[0:2]
        warehouse_allocation = None
        try:
            warehouse_allocation = WarehouseAllocation.query.filter_by(pincode = pincode).one()
        except:
            warehouse_allocation = WarehouseAllocation()
            warehouse_allocation.pincode = str(int(pincode))
            session.add(warehouse_allocation)
        warehouse_allocation.primary_warehouse_location = WarehouseLocation._NAMES_TO_VALUES[warehouse_loc]
    session.commit()

def main():
    parser = optparse.OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                   type="string", help="Read the serviceable location list from FILE",
                   metavar="FILE")
    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?")
    filename = options.filename
    if filename is None:
        parser.error("A filename must be provided. Use -h for more details.")
    load_warehouse_allocations(filename, options.hostname)

if __name__ == '__main__':
    main()