Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1504 ankur.sing 1
#!/usr/bin/python
2
 
3
import optparse
4
import xlrd
5
from elixir import *
6
 
7
if __name__ == '__main__' and __package__ is None:
8
    import sys
9
    import os
10
    sys.path.insert(0, os.getcwd())
11
 
12
from shop2020.logistics.service.impl import DataService
13
from shop2020.logistics.service.impl.DataService import Provider,\
14
    DestinationProviderAllocation
15
from shop2020.thriftpy.logistics.ttypes import WarehouseLocation,\
16
    LogisticsServiceException
17
 
4014 chandransh 18
def load_providers(filename, sheet_no, db_hostname):
19
    DataService.initialize(dbname='logistics', db_hostname=db_hostname)
1504 ankur.sing 20
 
21
    workbook = xlrd.open_workbook(filename)
22
    sheet = workbook.sheet_by_index(sheet_no)
23
    num_rows = sheet.nrows
24
    providers = Provider.query.all()
25
    provider_map = dict([(p.name, p) for p in providers])
26
    provider_for_warehouse_location = {}
27
 
28
    for rownum in range(1, num_rows):
29
        dest_pincode, provider_for_warehouse_location[WarehouseLocation.Delhi], provider_for_warehouse_location[WarehouseLocation.Mumbai],\
30
         provider_for_warehouse_location[WarehouseLocation.Bangalore], provider_for_warehouse_location[WarehouseLocation.Kolkata] = sheet.row_values(rownum)[0:5]
31
        dest_pincode = str(int(dest_pincode))
32
 
33
        for warehouse_loc, provider_name in provider_for_warehouse_location.iteritems():
34
            if provider_name == '' or provider_name == None:
35
                continue
36
            try:
37
                query = DestinationProviderAllocation.query.filter_by(destination_pin = dest_pincode)
38
                query = query.filter_by(warehouse_location = warehouse_loc)
39
                dpa = query.one()
40
            except:
41
                dpa = DestinationProviderAllocation()
42
                dpa.destination_pin = dest_pincode
43
                dpa.warehouse_location = warehouse_loc
44
            provider = provider_map[provider_name]
45
            if provider is None:
46
                raise LogisticsServiceException(103, "Invalid provider --> " + provider_name)
47
            if sheet_no == 0:
48
                dpa.provider_less_amount = provider
49
            else:
50
                dpa.provider_more_amount = provider
51
    session.commit()
52
 
53
def main():
54
    parser = optparse.OptionParser()
55
    parser.add_option("-f", "--file", dest="filename",
56
                   default="DestinationProviderAllocation.xls", type="string",
57
                   help="Read the providers list for different destinations and warehouse locations from FILE",
58
                   metavar="FILE")
4014 chandransh 59
    parser.add_option("-H", "--host", dest="hostname",
60
                  default="localhost",
61
                  type="string", help="The HOST where the DB server is running",
62
                  metavar="HOST")
1504 ankur.sing 63
    (options, args) = parser.parse_args()
64
    if len(args) != 0:
65
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
66
    filename = options.filename
67
    if filename is None:
68
        parser.error("A filename must be provided. Use -h for more details.")
4014 chandransh 69
    load_providers(filename, 0, options.hostname)
70
    load_providers(filename, 1, options.hostname)
1504 ankur.sing 71
 
72
if __name__ == '__main__':
73
    main()