| 18363 |
amit.gupta |
1 |
#!/usr/bin/python
|
|
|
2 |
# coding: ascii
|
|
|
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.
|
|
|
6 |
|
|
|
7 |
@author: Chandranshu
|
|
|
8 |
'''
|
|
|
9 |
from datetime import timedelta
|
|
|
10 |
from elixir import *
|
|
|
11 |
from multiprocessing import Process, Queue
|
|
|
12 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
13 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
14 |
from shop2020.thriftpy.model.v1.catalog.ttypes import Item
|
|
|
15 |
from shop2020.thriftpy.model.v1.inventory.ttypes import VendorItemPricing
|
|
|
16 |
from shop2020.utils.EmailAttachmentSender import mail
|
|
|
17 |
from shop2020.utils.Utils import to_java_date
|
|
|
18 |
import csv
|
|
|
19 |
import datetime
|
|
|
20 |
import optparse
|
|
|
21 |
import time
|
|
|
22 |
import traceback
|
|
|
23 |
import xlrd
|
|
|
24 |
|
|
|
25 |
if __name__ == '__main__' and __package__ is None:
|
|
|
26 |
import sys
|
|
|
27 |
import os
|
|
|
28 |
sys.path.insert(0, os.getcwd())
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
def load_item_data(filename, lineNumber=None):
|
|
|
32 |
message="Done successfully"
|
|
|
33 |
workbook = xlrd.open_workbook(filename)
|
|
|
34 |
sheet = workbook.sheet_by_index(0)
|
|
|
35 |
num_rows = sheet.nrows
|
|
|
36 |
updatedOn = to_java_date(datetime.datetime.now())
|
|
|
37 |
cclient = CatalogClient().get_client()
|
|
|
38 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
|
|
39 |
iclient = InventoryClient().get_client()
|
|
|
40 |
|
|
|
41 |
tcategories = cclient.getAllCategories()
|
|
|
42 |
parent_category_ids = []
|
|
|
43 |
categoryMap = {}
|
|
|
44 |
#0-Delhi 1-Maha 2-Blr 3-Ggn 4-Rajasthan 5-Telangana 6-Gujarat
|
|
|
45 |
|
|
|
46 |
for tcategory in tcategories:
|
|
|
47 |
if tcategory.parent_category_id not in parent_category_ids:
|
|
|
48 |
parent_category_ids.append(tcategory.parent_category_id)
|
|
|
49 |
|
|
|
50 |
for tcategory in tcategories:
|
|
|
51 |
if tcategory.id not in parent_category_ids:
|
|
|
52 |
categoryMap[tcategory.label] = tcategory.id
|
|
|
53 |
|
|
|
54 |
message = ""
|
|
|
55 |
|
|
|
56 |
if lineNumber is None:
|
|
|
57 |
start = 1
|
|
|
58 |
end = num_rows
|
|
|
59 |
else:
|
|
|
60 |
start = lineNumber
|
|
|
61 |
end = min(lineNumber + 3, num_rows)
|
|
|
62 |
for rownum in range(start, end):
|
|
|
63 |
states = {}
|
|
|
64 |
try:
|
|
|
65 |
itemID, product_group, category_name, brand, model_number, model_name, color, mrp, sp, mop, dp, tp, nlc, start_date, preferred_vendor, risky, weight, item_type, states[0], states[3], states[1], states[2] = sheet.row_values(rownum)[0:22]
|
|
|
66 |
if isinstance(model_number, float):
|
|
|
67 |
model_number = str(int(model_number))
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
item = Item()
|
|
|
71 |
item.productGroup = product_group
|
|
|
72 |
item.brand = brand
|
|
|
73 |
item.modelNumber = model_number
|
|
|
74 |
item.modelName = model_name
|
|
|
75 |
item.color = color
|
|
|
76 |
try:
|
|
|
77 |
item.mrp = round(mrp)
|
|
|
78 |
except:
|
|
|
79 |
pass
|
|
|
80 |
#if item is risky
|
|
|
81 |
if categoryMap.has_key(category_name.strip()):
|
|
|
82 |
item.category = categoryMap[category_name.strip()]
|
|
|
83 |
item.itemStatus = 8
|
|
|
84 |
else:
|
|
|
85 |
print "can't find {0} at row {1}".format(category_name, rownum)
|
|
|
86 |
continue
|
|
|
87 |
item.sellingPrice = round(sp)
|
|
|
88 |
item.startDate = long(to_java_date(datetime.datetime(1899, 12, 30) + timedelta(days=int(start_date))))
|
|
|
89 |
item.preferredVendor = preferred_vendor
|
|
|
90 |
item.risky = int(risky)
|
|
|
91 |
item.weight = weight
|
|
|
92 |
item.updatedOn = updatedOn
|
|
|
93 |
item.type = int(item_type)
|
|
|
94 |
for key in states.keys():
|
|
|
95 |
val = states[key]
|
|
|
96 |
if val !='':
|
|
|
97 |
states[key] = val*100
|
|
|
98 |
else:
|
|
|
99 |
del states[key]
|
|
|
100 |
|
|
|
101 |
for c in (1,2,3,4,5):
|
|
|
102 |
print "newitemid:", item.id
|
|
|
103 |
try:
|
|
|
104 |
item_id = cclient.addItem(item)
|
|
|
105 |
print "for row {1} item added\t{0}".format(item_id, rownum)
|
|
|
106 |
print "newitemid:", item_id, "olditemid:", itemID
|
|
|
107 |
item.id = item_id
|
|
|
108 |
if len(states)>0:
|
|
|
109 |
for c2 in (1,2,3,4,5):
|
|
|
110 |
try:
|
|
|
111 |
cclient.updateItemStateVat(item_id, states)
|
|
|
112 |
print "break 0"
|
|
|
113 |
break
|
|
|
114 |
except Exception as e:
|
|
|
115 |
cclient = CatalogClient().get_client()
|
|
|
116 |
for c1 in (5,5,5,5,5):
|
|
|
117 |
try:
|
|
|
118 |
prodClient.addItem(item)
|
|
|
119 |
if len(states) > 0:
|
|
|
120 |
for c3 in (1,2,3,4,5):
|
|
|
121 |
try:
|
|
|
122 |
prodClient.updateItemStateVat(item_id, states)
|
|
|
123 |
print "break 1"
|
|
|
124 |
break
|
|
|
125 |
except Exception as e:
|
|
|
126 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
|
|
127 |
print "break 2"
|
|
|
128 |
break
|
|
|
129 |
except:
|
|
|
130 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
|
|
131 |
print "break 3"
|
|
|
132 |
break
|
|
|
133 |
except Exception as e:
|
|
|
134 |
cclient = CatalogClient().get_client()
|
|
|
135 |
vip = VendorItemPricing()
|
|
|
136 |
vip.itemId = item_id
|
|
|
137 |
vip.dealerPrice = round(dp)
|
|
|
138 |
vip.mop = round(mop)
|
|
|
139 |
vip.nlc = round(nlc)
|
|
|
140 |
vip.transferPrice = round(tp)
|
|
|
141 |
vip.vendorId = preferred_vendor
|
|
|
142 |
for c in (1,2,3,4,5):
|
|
|
143 |
try:
|
|
|
144 |
iclient.addVendorItemPricing(vip)
|
|
|
145 |
break
|
|
|
146 |
except Exception as e:
|
|
|
147 |
iclient = InventoryClient().get_client()
|
|
|
148 |
|
|
|
149 |
except:
|
|
|
150 |
traceback.print_exc()
|
|
|
151 |
message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + "\n"
|
|
|
152 |
print message
|
|
|
153 |
mail("build@shop2020.in", "cafe@nes", ["amit.gupta@shop2020.in", "chandan.kumar@shop2020.in"], "Problem while adding items", message, [], [], [])
|
|
|
154 |
print "Successfully updated the item list information."
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
def main():
|
|
|
158 |
parser = optparse.OptionParser()
|
|
|
159 |
parser.add_option("-f", "--file", dest="filename",
|
|
|
160 |
default="ItemList.xls", type="string",
|
|
|
161 |
help="Read the item list from FILE",
|
|
|
162 |
metavar="FILE")
|
|
|
163 |
parser.add_option("-l", "--line", dest="linenumber",
|
|
|
164 |
default="ItemList.xls", type="string",
|
|
|
165 |
help="Line to start from",
|
|
|
166 |
metavar="FILE")
|
|
|
167 |
(options, args) = parser.parse_args()
|
|
|
168 |
if len(args) != 0:
|
|
|
169 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
170 |
filename = options.filename
|
|
|
171 |
linenumber = options.linenumber
|
|
|
172 |
load_item_data(filename, linenumber)
|
|
|
173 |
|
|
|
174 |
if __name__ == '__main__':
|
|
|
175 |
main()
|