| 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):
|
| 15729 |
amit.gupta |
29 |
message="Done successfully"
|
| 626 |
chandransh |
30 |
workbook = xlrd.open_workbook(filename)
|
|
|
31 |
sheet = workbook.sheet_by_index(0)
|
|
|
32 |
num_rows = sheet.nrows
|
| 9431 |
rajveer |
33 |
updatedOn = to_java_date(datetime.datetime.now())
|
|
|
34 |
cclient = CatalogClient().get_client()
|
| 12650 |
amit.gupta |
35 |
tcategories = cclient.getAllCategories()
|
|
|
36 |
parent_category_ids = []
|
|
|
37 |
categoryMap = {}
|
| 15728 |
amit.gupta |
38 |
#0-Delhi 1-Maha 2-Blr 3-Ggn 4-Rajasthan 5-Telangana 6-Gujarat
|
|
|
39 |
|
| 12650 |
amit.gupta |
40 |
for tcategory in tcategories:
|
|
|
41 |
if tcategory.parent_category_id not in parent_category_ids:
|
|
|
42 |
parent_category_ids.append(tcategory.parent_category_id)
|
|
|
43 |
|
|
|
44 |
for tcategory in tcategories:
|
|
|
45 |
if tcategory.id not in parent_category_ids:
|
|
|
46 |
categoryMap[tcategory.label] = tcategory.id
|
|
|
47 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
| 9431 |
rajveer |
48 |
iclient = InventoryClient().get_client()
|
|
|
49 |
|
| 626 |
chandransh |
50 |
for rownum in range(1, num_rows):
|
| 13040 |
amit.gupta |
51 |
#print sheet.row_values(rownum)
|
| 9431 |
rajveer |
52 |
message = ""
|
| 15728 |
amit.gupta |
53 |
states = {}
|
| 9431 |
rajveer |
54 |
try:
|
| 15728 |
amit.gupta |
55 |
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:21]
|
| 9431 |
rajveer |
56 |
if isinstance(model_number, float):
|
|
|
57 |
model_number = str(int(model_number))
|
| 5440 |
phani.kuma |
58 |
|
| 9486 |
rajveer |
59 |
|
| 9431 |
rajveer |
60 |
item = Item()
|
|
|
61 |
item.productGroup = product_group
|
|
|
62 |
item.brand = brand
|
|
|
63 |
item.modelNumber = model_number
|
|
|
64 |
item.modelName = model_name
|
|
|
65 |
item.color = color
|
|
|
66 |
item.mrp = round(mrp)
|
| 12650 |
amit.gupta |
67 |
#if item is risky
|
| 13044 |
amit.gupta |
68 |
if categoryMap.has_key(category_name.strip()):
|
| 12650 |
amit.gupta |
69 |
item.category = categoryMap[category_name.strip()]
|
|
|
70 |
item.itemStatus = 8
|
| 13044 |
amit.gupta |
71 |
else:
|
|
|
72 |
print "can't find {0} at row {1}".format(category_name, rownum)
|
| 13045 |
amit.gupta |
73 |
continue
|
| 9431 |
rajveer |
74 |
item.sellingPrice = round(sp)
|
|
|
75 |
item.startDate = long(to_java_date(datetime.datetime(1899, 12, 30) + timedelta(days=int(start_date))))
|
| 13041 |
amit.gupta |
76 |
item.preferredVendor = preferred_vendor
|
| 12650 |
amit.gupta |
77 |
item.risky = int(risky)
|
| 9431 |
rajveer |
78 |
item.weight = weight
|
|
|
79 |
item.updatedOn = updatedOn
|
|
|
80 |
item.type = int(item_type)
|
| 15728 |
amit.gupta |
81 |
for key,val in states.iteritems():
|
|
|
82 |
states[key] = val*100
|
| 9805 |
rajveer |
83 |
for c in (1,2,3,4,5):
|
|
|
84 |
try:
|
|
|
85 |
item_id = cclient.addItem(item)
|
| 13042 |
amit.gupta |
86 |
print "for row {1} item added\t{0}".format(item_id, rownum)
|
| 12650 |
amit.gupta |
87 |
item.id = item_id
|
| 15728 |
amit.gupta |
88 |
if len(states)>0:
|
|
|
89 |
for c2 in (1,2,3,4,5):
|
|
|
90 |
try:
|
|
|
91 |
cclient.updateItemStateVat(item_id, states)
|
|
|
92 |
break
|
|
|
93 |
except Exception as e:
|
|
|
94 |
cclient = CatalogClient().get_client()
|
| 12650 |
amit.gupta |
95 |
for c1 in (5,5,5,5,5):
|
|
|
96 |
try:
|
|
|
97 |
prodClient.addItem(item)
|
| 15728 |
amit.gupta |
98 |
if len(states) > 0:
|
|
|
99 |
for c3 in (1,2,3,4,5):
|
|
|
100 |
try:
|
|
|
101 |
prodClient.updateItemStateVat(item_id, states)
|
|
|
102 |
break
|
|
|
103 |
except Exception as e:
|
|
|
104 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
| 12650 |
amit.gupta |
105 |
break
|
|
|
106 |
except:
|
|
|
107 |
prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
|
| 9805 |
rajveer |
108 |
break
|
|
|
109 |
except Exception as e:
|
|
|
110 |
cclient = CatalogClient().get_client()
|
| 1080 |
chandransh |
111 |
|
| 9431 |
rajveer |
112 |
vip = VendorItemPricing()
|
|
|
113 |
vip.itemId = item_id
|
|
|
114 |
vip.dealerPrice = round(dp)
|
|
|
115 |
vip.mop = round(mop)
|
|
|
116 |
vip.nlc = round(nlc)
|
|
|
117 |
vip.transferPrice = round(tp)
|
|
|
118 |
vip.vendorId = preferred_vendor
|
| 9805 |
rajveer |
119 |
for c in (1,2,3,4,5):
|
|
|
120 |
try:
|
|
|
121 |
iclient.addVendorItemPricing(vip)
|
|
|
122 |
break
|
|
|
123 |
except Exception as e:
|
|
|
124 |
iclient = InventoryClient().get_client()
|
|
|
125 |
|
| 9486 |
rajveer |
126 |
except Exception as e:
|
|
|
127 |
message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + str(e.message) + "\n"
|
| 12654 |
amit.gupta |
128 |
print message
|
| 11539 |
amit.gupta |
129 |
mail("build@shop2020.in", "cafe@nes", ["amit.gupta@shop2020.in", "chandan.kumar@shop2020.in"], "Problem while adding items", message, [], [], [])
|
| 626 |
chandransh |
130 |
|
| 4725 |
phani.kuma |
131 |
|
| 626 |
chandransh |
132 |
def main():
|
|
|
133 |
parser = optparse.OptionParser()
|
|
|
134 |
parser.add_option("-f", "--file", dest="filename",
|
|
|
135 |
default="ItemList.xls", type="string",
|
|
|
136 |
help="Read the item list from FILE",
|
|
|
137 |
metavar="FILE")
|
|
|
138 |
(options, args) = parser.parse_args()
|
|
|
139 |
if len(args) != 0:
|
|
|
140 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
141 |
filename = options.filename
|
| 9431 |
rajveer |
142 |
load_item_data(filename)
|
| 626 |
chandransh |
143 |
|
|
|
144 |
if __name__ == '__main__':
|
| 13041 |
amit.gupta |
145 |
main()
|