Subversion Repositories SmartDukaan

Rev

Rev 631 | Rev 643 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
import optparse
import xlrd
import datetime

if __name__ == '__main__' and __package__ is None:
    import sys
    import os
    sys.path.insert(0, os.getcwd())

from shop2020.thriftpy.model.v1.catalog.ttypes import status
from shop2020.model.v1.catalog.impl import DataService
from shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGenerator
from elixir import *

def load_item_data(filename):
    DataService.initialize()

    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(0)
    num_rows = sheet.nrows
    updatedOn = 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, entity_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 = brand
            item.model_number = model_number
            item.color = color
            item.status = status.IN_PROCESS
            item.addedOn = updatedOn
            session.add(item)
            
        #Set the catalog_item_id.
        similar_item = Item.query.filter_by(manufacturer_name = brand, model_number=model_number).first()
        print similar_item
        if similar_item is None or similar_item.catalog_item_id is None:
            #Use the entity_id_generator
            entity_id = EntityIDGenerator.query.first()
            item.catalog_item_id = entity_id.id + 1
            entity_id.id = entity_id.id  + 1
        else:
            #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(entity_id)
        item.category = int(category_id)
        item.status = status.ACTIVE
        '''
        
        item.model_name = model_name
        if mrp != "":
            item.mrp = mrp
        if mop != "":
            item.mop = mop
        if dp != "":    
            item.dealerPrice = dp
            item.sellingPrice = dp
        if weight != "":    
            item.weight = weight
        if start_date != "":
            item.startDate = datetime.datetime(*xlrd.xldate_as_tuple(start_date, workbook.datemode))
        item.bestDealText = deal_text
        if deal_value != "":
            item.bestDealValue = deal_value
        item.comments = comments
        item.updatedOn = updatedOn
        session.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.filename
    load_item_data(filename)

if __name__ == '__main__':
    main()