Subversion Repositories SmartDukaan

Rev

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

#!/usr/bin/python
'''
It loads data into the ServiceableLocationDetails and DeliveryEstinate
tables.

@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 Provider,\
    ServiceableLocationDetails, DeliveryEstimate
from shop2020.thriftpy.logistics.ttypes import StationType, WarehouseLocation

def load_service_details(filename, provider_name, db_hostname):
    DataService.initialize(dbname='logistics', db_hostname=db_hostname)
    
    #provider_name = provider_name.lower()
    provider = Provider.get_by(name=provider_name)
    if provider is None:
        provider = Provider()
        provider.name = provider_name
        session.commit()
    
    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(0)
    num_rows = sheet.nrows
    delivery_times = {}
    for rownum in range(1, num_rows):
        dest_pincode, dest_code, exp, cod, station_type,\
        delivery_times[WarehouseLocation.Delhi] = sheet.row_values(rownum)[0:6]
        #delivery_times[WarehouseLocation.Delhi], delivery_times[WarehouseLocation.Mumbai],\
        # delivery_times[WarehouseLocation.Bangalore], delivery_times[WarehouseLocation.Kolkata] = sheet.row_values(rownum)[0:10]
        dest_pincode = str(int(dest_pincode))
        dest_code = str(dest_code)
        sld = ServiceableLocationDetails()
        sld.provider = provider
        sld.dest_pincode = dest_pincode
        sld.dest_code = dest_code
        sld.exp = True if exp in [1, 'Yes'] else False
        sld.cod = True if cod in [1, 'Yes'] else False
        if station_type in ["Aramex Own Station", "A"]:
            sld.station_type = StationType.OWN_STATION
        if station_type in ["Associate Station", "B"]:
            sld.station_type = StationType.ASSOCIATE_STATION
        
        for warehouse_loc, delivery_time in delivery_times.iteritems():
            if delivery_time == '' or delivery_time == None:
                continue
            delivery_estimate = DeliveryEstimate()
            delivery_estimate.destination_pin = str(int(dest_pincode))
            delivery_estimate.provider = provider
            delivery_estimate.warehouse_location = warehouse_loc
            delivery_estimate.delivery_time = int(delivery_time)
    session.commit()

def main():
    parser = optparse.OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                   default="ServiceableLocations.xls", type="string",
                   help="Read the serviceable location list from FILE",
                   metavar="FILE")
    parser.add_option("-P", "--provider", dest="provider_name",
                      type="string", help="Create these entries for PROVIDER",
                      metavar="PROVIDER")
    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
    provider_name = options.provider_name
    if filename is None or provider_name is None:
        parser.error("A provider's name and a filename must be provided. Use -h for more details.")
    load_service_details(filename, provider_name, options.hostname)

if __name__ == '__main__':
    main()