Rev 17697 | 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 datetime import timedeltafrom elixir import *from shop2020.clients.CatalogClient import CatalogClientfrom shop2020.clients.InventoryClient import InventoryClientfrom shop2020.thriftpy.model.v1.catalog.ttypes import Itemfrom shop2020.thriftpy.model.v1.inventory.ttypes import VendorItemPricingfrom shop2020.utils.EmailAttachmentSender import mailfrom shop2020.utils.Utils import to_java_dateimport csvimport datetimeimport optparseimport timeimport tracebackimport xlrdif __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()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):states = {}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 = colortry:item.mrp = round(mrp)except:pass#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 in states.keys():val = states[key]if val !='':states[key] = val*100else:del states[key]for 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:traceback.print_exc()message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + "\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()