Rev 15776 | Rev 15779 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/python# coding: ascii'''This script is used to load item details in the catalog database.It's now mostly used for Accessories since they come in huge numbers.@author: Chandranshu'''from elixir import *from shop2020.thriftpy.model.v1.catalog.ttypes import Itemimport csvimport datetimefrom datetime import timedeltaimport optparseimport xlrdfrom shop2020.clients.CatalogClient import CatalogClientfrom shop2020.clients.InventoryClient import InventoryClientfrom shop2020.thriftpy.model.v1.inventory.ttypes import VendorItemPricingfrom shop2020.utils.Utils import to_java_datefrom shop2020.utils.EmailAttachmentSender import mailimport timeif __name__ == '__main__' and __package__ is None:import sysimport ossys.path.insert(0, os.getcwd())def load_item_data(filename):message="Done successfully"workbook = xlrd.open_workbook(filename)sheet = workbook.sheet_by_index(0)num_rows = sheet.nrowsupdatedOn = to_java_date(datetime.datetime.now())cclient = CatalogClient().get_client()prodClient = CatalogClient("catalog_service_server_host_prod").get_client()iclient = InventoryClient().get_client()rowsleft = [750,789,795,805,811,817,823,829,835,841,847,853,859,865,871,877,883,889,895,901,907,913,919,925,931,937,943,949,955,961,967,973,979,985,991,997,1003,1009,1015,1021,1027,1033,1039,1045,1051,1057,1063,1069,1075,1081,1087,1093,1099,1105,1111,1117,1123,1129,1135,1141,1147,1153,1159,1165,1171,1177,1183,1189,1195,1201,1207,1213,1219,1225,1231,1237,1243,1249,1255,1261,1267,1273,1279,1285,1291,1297,1303,1309,1315,1321,1327,1333,1339,1345,1351,1357,1363,1369,1375,1381,1387,1393,1399,1405,1411,1417,1423,1429,1435,1441,1447,1453,1459,1465,1471,1477,1483,1489,1495,1501,1507,1513,1519,1525,1531,1537,1543,1549,1555,1561,1567,1573,1579,1585,1591,1597,1603,1609,1615,1621,1627,1633,1639,1645,1651,1657,1663,1669,1675,1681,1687,1693,1699,1705,1711,1717,1723,1729,1735,1741,1747,1753,1759,1765,1771,1777,1783,1789,1795,1801,1807,1813,1819,1825,1831,1837,1843,1849,1855,1861,1867,1873,1879,1885,1891,1897,1903,1909,1915,1921,1927,1933,1939,1945,1951,1957,1963,1969,1975,1981,1987,1993,1999,2005,2011,2017,2023,2029,2035,2041,2047,2053,2059,2065,2071,2077,2083,2089,2095,2101,2107,2113,2119,2125,2131,2137,2143,2149,2155,2161,2167,2173,2179,2185,2191,2197,2203,2209,2215,2221,2227,2233,2239]tcategories = cclient.getAllCategories()parent_category_ids = []categoryMap = {}#0-Delhi 1-Maha 2-Blr 3-Ggn 4-Rajasthan 5-Telangana 6-Gujaratfor tcategory in tcategories:if tcategory.parent_category_id not in parent_category_ids:parent_category_ids.append(tcategory.parent_category_id)for tcategory in tcategories:if tcategory.id not in parent_category_ids:categoryMap[tcategory.label] = tcategory.idmessage = ""for rownum in range(1, num_rows):if rownum not in rowsleft:continuestates = {}try: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]if isinstance(model_number, float):model_number = str(int(model_number))item = Item()item.productGroup = product_groupitem.brand = branditem.modelNumber = model_numberitem.modelName = model_nameitem.color = coloritem.mrp = round(mrp)#if item is riskyif categoryMap.has_key(category_name.strip()):item.category = categoryMap[category_name.strip()]item.itemStatus = 8else:print "can't find {0} at row {1}".format(category_name, rownum)continueitem.sellingPrice = round(sp)item.startDate = long(to_java_date(datetime.datetime(1899, 12, 30) + timedelta(days=int(start_date))))item.preferredVendor = preferred_vendoritem.risky = int(risky)item.weight = weightitem.updatedOn = updatedOnitem.type = int(item_type)for key,val in states.iteritems():states[key] = val*100for c in (1,2,3,4,5):print "itemid", item.idtry:item_id = cclient.addItem(item)print "for row {1} item added\t{0}".format(item_id, rownum)item.id = item_idif len(states)>0:for c2 in (1,2,3,4,5):try:cclient.updateItemStateVat(item_id, states)print "break 0"breakexcept Exception as e:cclient = CatalogClient().get_client()for c1 in (5,5,5,5,5):try:prodClient.addItem(item)if len(states) > 0:for c3 in (1,2,3,4,5):try:prodClient.updateItemStateVat(item_id, states)print "break 1"breakexcept Exception as e:prodClient = CatalogClient("catalog_service_server_host_prod").get_client()print "break 2"breakexcept:prodClient = CatalogClient("catalog_service_server_host_prod").get_client()print "break 3"breakexcept Exception as e:cclient = CatalogClient().get_client()vip = VendorItemPricing()vip.itemId = item_idvip.dealerPrice = round(dp)vip.mop = round(mop)vip.nlc = round(nlc)vip.transferPrice = round(tp)vip.vendorId = preferred_vendorfor c in (1,2,3,4,5):try:iclient.addVendorItemPricing(vip)breakexcept Exception as e:iclient = InventoryClient().get_client()except Exception as e:message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + str(e.message) + "\n"print messagemail("build@shop2020.in", "cafe@nes", ["amit.gupta@shop2020.in", "chandan.kumar@shop2020.in"], "Problem while adding items", message, [], [], [])print "Successfully updated the item list information."def main():parser = optparse.OptionParser()parser.add_option("-f", "--file", dest="filename",default="ItemList.xls", type="string",help="Read the item list from FILE",metavar="FILE")(options, args) = parser.parse_args()if len(args) != 0:parser.error("You've supplied extra arguments. Are you sure you want to run this program?")filename = options.filenameload_item_data(filename)if __name__ == '__main__':main()