Rev 635 | Rev 724 | Go to most recent revision | Blame | Compare with Previous | 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.thriftpy.model.v1.catalog.ttypes import statusfrom shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGeneratorfrom elixir import *def load_item_data(filename):DataService.initialize()workbook = xlrd.open_workbook(filename)sheet = workbook.sheet_by_index(0)num_rows = sheet.nrowsupdatedOn = datetime.datetime.now()for rownum in range(1, num_rows):print sheet.row_values(rownum)brand, model_number, color, model_name, mrp, mop, dp, weight, start_date, deal_text, deal_value, comments, catalog_item_id, category_id = sheet.row_values(rownum)[0:14]if isinstance(model_number, float):model_number = str(int(model_number))item = Item.get_by(manufacturer_name=brand, model_number=model_number, color=color)if item is None:print "[ADDING:]{0} {1} {2} to our catalogue.".format(brand, model_number, color)item = Item()item.manufacturer_name = branditem.model_number = model_numberitem.color = coloritem.status = status.IN_PROCESSitem.addedOn = updatedOnsession.add(item)#Set the catalog_item_id.similar_item = Item.query.filter_by(manufacturer_name = brand, model_number=model_number).first()print similar_itemif similar_item is None or similar_item.catalog_item_id is None:#Use the entity_id_generatorentity_id = EntityIDGenerator.query.first()item.catalog_item_id = entity_id.id + 1entity_id.id = entity_id.id + 1else:#If it already exists for a brand and model_number, set it as same.item.catalog_item_id = similar_item.catalog_item_id#added category and entity id'''item.catalog_item_id = int(catalog_item_id)item.category = int(category_id)item.status = status.ACTIVE'''item.model_name = model_nameif mrp != "":item.mrp = mrpif mop != "":item.mop = mopif dp != "":item.dealerPrice = dpitem.sellingPrice = dpif weight != "":item.weight = weightif start_date != "":item.startDate = datetime.datetime(*xlrd.xldate_as_tuple(start_date, workbook.datemode))item.bestDealText = deal_textif deal_value != "":item.bestDealValue = deal_valueitem.comments = commentsitem.updatedOn = updatedOnsession.commit()session.execute("UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", updatedOn='"+ str(updatedOn) +"' WHERE updatedOn <> '" + str(updatedOn) + "'", mapper=Item)session.commit()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()