| 759 |
chandransh |
1 |
#!/usr/bin/python
|
|
|
2 |
|
| 4090 |
chandransh |
3 |
'''
|
|
|
4 |
This script loads AWB numebers of a given type for a given
|
|
|
5 |
provider into the database.
|
|
|
6 |
|
|
|
7 |
It expects the filename of a XLS file having just one column
|
|
|
8 |
containing AWB numbers. The header row is ignored.
|
|
|
9 |
|
|
|
10 |
@author: Chandranshu
|
|
|
11 |
'''
|
| 759 |
chandransh |
12 |
import optparse
|
|
|
13 |
import xlrd
|
|
|
14 |
from elixir import *
|
|
|
15 |
|
|
|
16 |
if __name__ == '__main__' and __package__ is None:
|
|
|
17 |
import sys
|
|
|
18 |
import os
|
|
|
19 |
sys.path.insert(0, os.getcwd())
|
|
|
20 |
|
|
|
21 |
from shop2020.logistics.service.impl import DataService
|
|
|
22 |
from shop2020.logistics.service.impl.DataService import Awb, Provider
|
|
|
23 |
|
| 4013 |
chandransh |
24 |
def load_awb_numbers(filename, provider_id, type, db_hostname):
|
|
|
25 |
DataService.initialize(dbname='logistics', db_hostname=db_hostname)
|
| 759 |
chandransh |
26 |
|
| 3044 |
chandransh |
27 |
provider = Provider.get_by(id=provider_id)
|
| 759 |
chandransh |
28 |
if provider is None:
|
| 3044 |
chandransh |
29 |
exit()
|
| 759 |
chandransh |
30 |
|
|
|
31 |
workbook = xlrd.open_workbook(filename)
|
|
|
32 |
sheet = workbook.sheet_by_index(0)
|
|
|
33 |
num_rows = sheet.nrows
|
|
|
34 |
for rownum in range(1, num_rows):
|
| 873 |
rajveer |
35 |
awb_number = sheet.row_values(rownum)[0]
|
| 759 |
chandransh |
36 |
awb = Awb()
|
| 873 |
rajveer |
37 |
awb.awb_number = int(awb_number)
|
| 759 |
chandransh |
38 |
awb.is_available = True
|
|
|
39 |
awb.provider = provider
|
| 3044 |
chandransh |
40 |
awb.type = type
|
| 759 |
chandransh |
41 |
session.commit()
|
|
|
42 |
|
|
|
43 |
def main():
|
|
|
44 |
parser = optparse.OptionParser()
|
|
|
45 |
parser.add_option("-f", "--file", dest="filename",
|
|
|
46 |
default="AwbNumbers.xls", type="string",
|
|
|
47 |
help="Read the AWB numbers from FILE",
|
|
|
48 |
metavar="FILE")
|
| 3044 |
chandransh |
49 |
parser.add_option("-P", "--provider", dest="provider_id",
|
|
|
50 |
type="int", help="Load these entries for PROVIDER",
|
| 759 |
chandransh |
51 |
metavar="PROVIDER")
|
| 3044 |
chandransh |
52 |
parser.add_option("-t", "--type", dest="type",
|
|
|
53 |
type="string", help="The TYPE of orders these AWB numbers are for",
|
|
|
54 |
metavar="TYPE")
|
| 4013 |
chandransh |
55 |
parser.add_option("-H", "--host", dest="hostname",
|
|
|
56 |
default="localhost",
|
|
|
57 |
type="string", help="The HOST where the DB server is running",
|
|
|
58 |
metavar="HOST")
|
| 759 |
chandransh |
59 |
(options, args) = parser.parse_args()
|
|
|
60 |
if len(args) != 0:
|
|
|
61 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
62 |
filename = options.filename
|
| 3044 |
chandransh |
63 |
provider_id = options.provider_id
|
|
|
64 |
type = options.type
|
|
|
65 |
if filename is None or provider_id is None or type is None:
|
| 759 |
chandransh |
66 |
parser.error("A provider's name and a filename must be provided. Use -h for usage details.")
|
| 4013 |
chandransh |
67 |
load_awb_numbers(filename, provider_id, type, options.hostname)
|
| 759 |
chandransh |
68 |
|
|
|
69 |
if __name__ == '__main__':
|
|
|
70 |
main()
|