| 626 |
chandransh |
1 |
#!/usr/bin/python
|
| 5440 |
phani.kuma |
2 |
# coding: ascii
|
| 4090 |
chandransh |
3 |
'''
|
|
|
4 |
This script is used to load item details in the catalog database.
|
|
|
5 |
It's now mostly used for Accessories since they come in huge numbers.
|
| 1080 |
chandransh |
6 |
|
| 4090 |
chandransh |
7 |
@author: Chandranshu
|
|
|
8 |
'''
|
| 5971 |
mandeep.dh |
9 |
from elixir import *
|
| 9431 |
rajveer |
10 |
from shop2020.thriftpy.model.v1.catalog.ttypes import Item
|
| 5971 |
mandeep.dh |
11 |
import csv
|
|
|
12 |
import datetime
|
| 9431 |
rajveer |
13 |
from datetime import timedelta
|
| 626 |
chandransh |
14 |
import optparse
|
|
|
15 |
import xlrd
|
| 9431 |
rajveer |
16 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
17 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
18 |
from shop2020.thriftpy.model.v1.inventory.ttypes import VendorItemPricing
|
|
|
19 |
from shop2020.utils.Utils import to_java_date
|
|
|
20 |
from shop2020.utils.EmailAttachmentSender import mail
|
| 626 |
chandransh |
21 |
|
|
|
22 |
if __name__ == '__main__' and __package__ is None:
|
|
|
23 |
import sys
|
|
|
24 |
import os
|
|
|
25 |
sys.path.insert(0, os.getcwd())
|
|
|
26 |
|
|
|
27 |
|
| 9431 |
rajveer |
28 |
def load_item_data(filename):
|
| 626 |
chandransh |
29 |
workbook = xlrd.open_workbook(filename)
|
|
|
30 |
sheet = workbook.sheet_by_index(0)
|
|
|
31 |
num_rows = sheet.nrows
|
| 9431 |
rajveer |
32 |
updatedOn = to_java_date(datetime.datetime.now())
|
|
|
33 |
cclient = CatalogClient().get_client()
|
| 12650 |
amit.gupta |
34 |
tcategories = cclient.getAllCategories()
|
|
|
35 |
parent_category_ids = []
|
|
|
36 |
categoryMap = {}
|
|
|
37 |
for tcategory in tcategories:
|
|
|
38 |
if tcategory.parent_category_id not in parent_category_ids:
|
|
|
39 |
parent_category_ids.append(tcategory.parent_category_id)
|
|
|
40 |
|
|
|
41 |
for tcategory in tcategories:
|
|
|
42 |
if tcategory.id not in parent_category_ids:
|
|
|
43 |
categoryMap[tcategory.label] = tcategory.id
|
|
|
44 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
| 9431 |
rajveer |
45 |
iclient = InventoryClient().get_client()
|
|
|
46 |
|
| 626 |
chandransh |
47 |
for rownum in range(1, num_rows):
|
| 13040 |
amit.gupta |
48 |
#print sheet.row_values(rownum)
|
| 9431 |
rajveer |
49 |
message = ""
|
|
|
50 |
try:
|
| 12650 |
amit.gupta |
51 |
product_group, category_name, brand, model_number, model_name, color, mrp, sp, mop, dp, tp, nlc, start_date, preferred_vendor, risky, weight, item_type = sheet.row_values(rownum)[0:17]
|
| 9431 |
rajveer |
52 |
if isinstance(model_number, float):
|
|
|
53 |
model_number = str(int(model_number))
|
| 5440 |
phani.kuma |
54 |
|
| 9486 |
rajveer |
55 |
|
| 9431 |
rajveer |
56 |
item = Item()
|
|
|
57 |
item.productGroup = product_group
|
|
|
58 |
item.brand = brand
|
|
|
59 |
item.modelNumber = model_number
|
|
|
60 |
item.modelName = model_name
|
|
|
61 |
item.color = color
|
|
|
62 |
item.mrp = round(mrp)
|
| 12650 |
amit.gupta |
63 |
#if item is risky
|
| 13044 |
amit.gupta |
64 |
if categoryMap.has_key(category_name.strip()):
|
| 12650 |
amit.gupta |
65 |
item.category = categoryMap[category_name.strip()]
|
|
|
66 |
item.itemStatus = 8
|
| 13044 |
amit.gupta |
67 |
else:
|
|
|
68 |
print "can't find {0} at row {1}".format(category_name, rownum)
|
|
|
69 |
break
|
| 9431 |
rajveer |
70 |
item.sellingPrice = round(sp)
|
|
|
71 |
item.startDate = long(to_java_date(datetime.datetime(1899, 12, 30) + timedelta(days=int(start_date))))
|
| 13041 |
amit.gupta |
72 |
item.preferredVendor = preferred_vendor
|
| 12650 |
amit.gupta |
73 |
item.risky = int(risky)
|
| 9431 |
rajveer |
74 |
item.weight = weight
|
|
|
75 |
item.updatedOn = updatedOn
|
|
|
76 |
item.type = int(item_type)
|
| 9805 |
rajveer |
77 |
for c in (1,2,3,4,5):
|
|
|
78 |
try:
|
|
|
79 |
item_id = cclient.addItem(item)
|
| 13042 |
amit.gupta |
80 |
print "for row {1} item added\t{0}".format(item_id, rownum)
|
| 12650 |
amit.gupta |
81 |
item.id = item_id
|
|
|
82 |
for c1 in (5,5,5,5,5):
|
|
|
83 |
try:
|
|
|
84 |
prodClient.addItem(item)
|
|
|
85 |
break
|
|
|
86 |
except:
|
|
|
87 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
| 9805 |
rajveer |
88 |
break
|
|
|
89 |
except Exception as e:
|
|
|
90 |
cclient = CatalogClient().get_client()
|
| 1080 |
chandransh |
91 |
|
| 9431 |
rajveer |
92 |
vip = VendorItemPricing()
|
|
|
93 |
vip.itemId = item_id
|
|
|
94 |
vip.dealerPrice = round(dp)
|
|
|
95 |
vip.mop = round(mop)
|
|
|
96 |
vip.nlc = round(nlc)
|
|
|
97 |
vip.transferPrice = round(tp)
|
|
|
98 |
vip.vendorId = preferred_vendor
|
| 9805 |
rajveer |
99 |
for c in (1,2,3,4,5):
|
|
|
100 |
try:
|
|
|
101 |
iclient.addVendorItemPricing(vip)
|
|
|
102 |
break
|
|
|
103 |
except Exception as e:
|
|
|
104 |
iclient = InventoryClient().get_client()
|
|
|
105 |
|
| 9486 |
rajveer |
106 |
except Exception as e:
|
|
|
107 |
message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + str(e.message) + "\n"
|
| 12654 |
amit.gupta |
108 |
print message
|
| 11539 |
amit.gupta |
109 |
mail("build@shop2020.in", "cafe@nes", ["amit.gupta@shop2020.in", "chandan.kumar@shop2020.in"], "Problem while adding items", message, [], [], [])
|
| 626 |
chandransh |
110 |
|
| 4725 |
phani.kuma |
111 |
|
| 626 |
chandransh |
112 |
def main():
|
|
|
113 |
parser = optparse.OptionParser()
|
|
|
114 |
parser.add_option("-f", "--file", dest="filename",
|
|
|
115 |
default="ItemList.xls", type="string",
|
|
|
116 |
help="Read the item list from FILE",
|
|
|
117 |
metavar="FILE")
|
|
|
118 |
(options, args) = parser.parse_args()
|
|
|
119 |
if len(args) != 0:
|
|
|
120 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
121 |
filename = options.filename
|
| 9431 |
rajveer |
122 |
load_item_data(filename)
|
| 626 |
chandransh |
123 |
|
|
|
124 |
if __name__ == '__main__':
|
| 13041 |
amit.gupta |
125 |
main()
|