Subversion Repositories SmartDukaan

Rev

Rev 1504 | Rev 4090 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python

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 Provider,\
    DestinationProviderAllocation
from shop2020.thriftpy.logistics.ttypes import WarehouseLocation,\
    LogisticsServiceException

def load_providers(filename, sheet_no, db_hostname):
    DataService.initialize(dbname='logistics', db_hostname=db_hostname)

    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(sheet_no)
    num_rows = sheet.nrows
    providers = Provider.query.all()
    provider_map = dict([(p.name, p) for p in providers])
    provider_for_warehouse_location = {}

    for rownum in range(1, num_rows):
        dest_pincode, provider_for_warehouse_location[WarehouseLocation.Delhi], provider_for_warehouse_location[WarehouseLocation.Mumbai],\
         provider_for_warehouse_location[WarehouseLocation.Bangalore], provider_for_warehouse_location[WarehouseLocation.Kolkata] = sheet.row_values(rownum)[0:5]
        dest_pincode = str(int(dest_pincode))
        
        for warehouse_loc, provider_name in provider_for_warehouse_location.iteritems():
            if provider_name == '' or provider_name == None:
                continue
            try:
                query = DestinationProviderAllocation.query.filter_by(destination_pin = dest_pincode)
                query = query.filter_by(warehouse_location = warehouse_loc)
                dpa = query.one()
            except:
                dpa = DestinationProviderAllocation()
                dpa.destination_pin = dest_pincode
                dpa.warehouse_location = warehouse_loc
            provider = provider_map[provider_name]
            if provider is None:
                raise LogisticsServiceException(103, "Invalid provider --> " + provider_name)
            if sheet_no == 0:
                dpa.provider_less_amount = provider
            else:
                dpa.provider_more_amount = provider
    session.commit()

def main():
    parser = optparse.OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                   default="DestinationProviderAllocation.xls", type="string",
                   help="Read the providers list for different destinations and warehouse locations 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_providers(filename, 0, options.hostname)
    load_providers(filename, 1, options.hostname)

if __name__ == '__main__':
    main()