Rev 1250 | Blame | Last modification | View Log | RSS feed
#!/usr/bin/pythonimport optparseimport xlrdimport datetimeif __name__ == '__main__' and __package__ is None:import sysimport ossys.path.insert(0, os.getcwd())from shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import Itemfrom elixir import *def read_best_seller_data(filename, strict):DataService.initialize('catalog')item_table = str(Item.table)session.execute("UPDATE " + item_table + " SET bestSellingRank=null", mapper=Item)print("Cleared the best selling rank of items in the Item table itself")#The txn is not yet committed. If something goes wrong, we'll still have our best sellers.#raise Exception("Testing that the old ranks are not cleared if something fails.")workbook = xlrd.open_workbook(filename)sheet = workbook.sheet_by_index(0)num_rows = sheet.nrowsupdatedOn = datetime.datetime.now()missing_items = []for rownum in range(1, num_rows):print sheet.row_values(rownum)[0:3]rank, brand, model_number = sheet.row_values(rownum)[0:3]#product_group, brand, model_number, color, rank = sheet.row_values(rownum)[0:5]if isinstance(model_number, float):model_number = str(int(model_number))item = Item.query.filter_by(brand=brand, model_number=model_number).first()#item = Item.get_by(product_group=product_group, brand=brand, model_number=model_number, color=color)if item is None:if strict:raise Exception("[MISSING:]We don't have {0} {1} in our catalogue.".format(brand, model_number))else:missing_items.append([brand, model_number, rank])continueitem.bestSellingRank = int(rank)item.updatedOn = updatedOnsession.commit()print "Successfully updated the best sellers information."if missing_items:print "Some items are not present in our catalog"print missing_itemsdef main():parser = optparse.OptionParser()parser.add_option("-f", "--file", dest="filename",default="BestSellers.xls", type="string",help="Read the best sellers list from FILE",metavar="FILE")parser.add_option("-s", "--strict", dest="strict",default=False, action="store_true")(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.filenameread_best_seller_data(filename, options.strict)if __name__ == '__main__':main()