| 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 |
|
| 18373 |
amit.gupta |
31 |
def load_item_data(filename, start, end):
|
|
|
32 |
print filename, start, end
|
|
|
33 |
#return
|
| 18363 |
amit.gupta |
34 |
workbook = xlrd.open_workbook(filename)
|
|
|
35 |
sheet = workbook.sheet_by_index(0)
|
|
|
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 |
for rownum in range(start, end):
|
|
|
57 |
states = {}
|
|
|
58 |
try:
|
|
|
59 |
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]
|
|
|
60 |
if isinstance(model_number, float):
|
|
|
61 |
model_number = str(int(model_number))
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
item = Item()
|
|
|
65 |
item.productGroup = product_group
|
|
|
66 |
item.brand = brand
|
|
|
67 |
item.modelNumber = model_number
|
|
|
68 |
item.modelName = model_name
|
|
|
69 |
item.color = color
|
|
|
70 |
try:
|
|
|
71 |
item.mrp = round(mrp)
|
|
|
72 |
except:
|
|
|
73 |
pass
|
|
|
74 |
#if item is risky
|
|
|
75 |
if categoryMap.has_key(category_name.strip()):
|
|
|
76 |
item.category = categoryMap[category_name.strip()]
|
|
|
77 |
item.itemStatus = 8
|
|
|
78 |
else:
|
|
|
79 |
print "can't find {0} at row {1}".format(category_name, rownum)
|
|
|
80 |
continue
|
|
|
81 |
item.sellingPrice = round(sp)
|
|
|
82 |
item.startDate = long(to_java_date(datetime.datetime(1899, 12, 30) + timedelta(days=int(start_date))))
|
|
|
83 |
item.preferredVendor = preferred_vendor
|
|
|
84 |
item.risky = int(risky)
|
|
|
85 |
item.weight = weight
|
|
|
86 |
item.updatedOn = updatedOn
|
|
|
87 |
item.type = int(item_type)
|
|
|
88 |
for key in states.keys():
|
|
|
89 |
val = states[key]
|
|
|
90 |
if val !='':
|
|
|
91 |
states[key] = val*100
|
|
|
92 |
else:
|
|
|
93 |
del states[key]
|
|
|
94 |
|
|
|
95 |
for c in (1,2,3,4,5):
|
|
|
96 |
print "newitemid:", item.id
|
|
|
97 |
try:
|
|
|
98 |
item_id = cclient.addItem(item)
|
|
|
99 |
print "for row {1} item added\t{0}".format(item_id, rownum)
|
|
|
100 |
print "newitemid:", item_id, "olditemid:", itemID
|
|
|
101 |
item.id = item_id
|
|
|
102 |
if len(states)>0:
|
|
|
103 |
for c2 in (1,2,3,4,5):
|
|
|
104 |
try:
|
|
|
105 |
cclient.updateItemStateVat(item_id, states)
|
|
|
106 |
print "break 0"
|
|
|
107 |
break
|
|
|
108 |
except Exception as e:
|
|
|
109 |
cclient = CatalogClient().get_client()
|
|
|
110 |
for c1 in (5,5,5,5,5):
|
|
|
111 |
try:
|
|
|
112 |
prodClient.addItem(item)
|
|
|
113 |
if len(states) > 0:
|
|
|
114 |
for c3 in (1,2,3,4,5):
|
|
|
115 |
try:
|
|
|
116 |
prodClient.updateItemStateVat(item_id, states)
|
|
|
117 |
print "break 1"
|
|
|
118 |
break
|
|
|
119 |
except Exception as e:
|
|
|
120 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
|
|
121 |
print "break 2"
|
|
|
122 |
break
|
|
|
123 |
except:
|
|
|
124 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
|
|
125 |
print "break 3"
|
|
|
126 |
break
|
|
|
127 |
except Exception as e:
|
|
|
128 |
cclient = CatalogClient().get_client()
|
|
|
129 |
vip = VendorItemPricing()
|
|
|
130 |
vip.itemId = item_id
|
| 18373 |
amit.gupta |
131 |
vip.dealerPrice = round(dp,2)
|
|
|
132 |
vip.mop = round(mop,2)
|
|
|
133 |
vip.nlc = round(nlc,2)
|
|
|
134 |
vip.transferPrice = round(tp,2)
|
| 18363 |
amit.gupta |
135 |
vip.vendorId = preferred_vendor
|
|
|
136 |
for c in (1,2,3,4,5):
|
|
|
137 |
try:
|
|
|
138 |
iclient.addVendorItemPricing(vip)
|
|
|
139 |
break
|
|
|
140 |
except Exception as e:
|
|
|
141 |
iclient = InventoryClient().get_client()
|
|
|
142 |
|
|
|
143 |
except:
|
|
|
144 |
traceback.print_exc()
|
|
|
145 |
message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + "\n"
|
|
|
146 |
print message
|
| 18373 |
amit.gupta |
147 |
#mail("build@shop2020.in", "cafe@nes", ["amit.gupta@shop2020.in", "chandan.kumar@shop2020.in"], "Problem while adding items", message, [], [], [])
|
| 18363 |
amit.gupta |
148 |
print "Successfully updated the item list information."
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
def main():
|
|
|
152 |
parser = optparse.OptionParser()
|
|
|
153 |
parser.add_option("-f", "--file", dest="filename",
|
|
|
154 |
default="ItemList.xls", type="string",
|
|
|
155 |
help="Read the item list from FILE",
|
|
|
156 |
metavar="FILE")
|
| 18373 |
amit.gupta |
157 |
parser.add_option("-l", "--line", dest="totallines",
|
| 18363 |
amit.gupta |
158 |
default="ItemList.xls", type="string",
|
|
|
159 |
help="Line to start from",
|
|
|
160 |
metavar="FILE")
|
|
|
161 |
(options, args) = parser.parse_args()
|
|
|
162 |
if len(args) != 0:
|
|
|
163 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
164 |
filename = options.filename
|
| 18373 |
amit.gupta |
165 |
totallines = int(options.totallines)
|
|
|
166 |
#totallines = 13
|
|
|
167 |
itemsLeft = True
|
|
|
168 |
lineNumbers=1
|
|
|
169 |
oneIter=4
|
| 18374 |
amit.gupta |
170 |
start = lineNumbers
|
| 18373 |
amit.gupta |
171 |
while itemsLeft:
|
| 18374 |
amit.gupta |
172 |
end = min(start + oneIter, totallines+1)#exclude last
|
|
|
173 |
#print start, end
|
| 18373 |
amit.gupta |
174 |
p=Process(target=load_item_data, args=(filename, start, end,))
|
|
|
175 |
p.start()
|
|
|
176 |
p.join()
|
|
|
177 |
time.sleep(5)
|
|
|
178 |
start=end
|
|
|
179 |
if start==totallines + 1:
|
|
|
180 |
itemsLeft = False
|
|
|
181 |
|
| 18363 |
amit.gupta |
182 |
if __name__ == '__main__':
|
|
|
183 |
main()
|