Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
626 chandransh 1
#!/usr/bin/python
2
import optparse
3
import xlrd
4
import datetime
5
 
6
if __name__ == '__main__' and __package__ is None:
7
    import sys
8
    import os
9
    sys.path.insert(0, os.getcwd())
10
 
11
from shop2020.thriftpy.model.v1.catalog.ttypes import status
12
from shop2020.model.v1.catalog.impl import DataService
724 chandransh 13
from shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGenerator,\
14
    ItemChangeLog
626 chandransh 15
from elixir import *
16
 
17
def load_item_data(filename):
18
    DataService.initialize()
19
 
20
    workbook = xlrd.open_workbook(filename)
21
    sheet = workbook.sheet_by_index(0)
22
    num_rows = sheet.nrows
23
    updatedOn = datetime.datetime.now()
24
    for rownum in range(1, num_rows):
25
        print sheet.row_values(rownum)
724 chandransh 26
        hotspot_category, brand, model_number, color, model_name, mrp, mop, dp, xfer_price, weight, start_date, deal_text, deal_value, comments, catalog_item_id, category_id = sheet.row_values(rownum)[0:17]
626 chandransh 27
        if isinstance(model_number, float):
28
            model_number = str(int(model_number))
29
        item = Item.get_by(manufacturer_name=brand, model_number=model_number, color=color)
30
        if item is None:
31
            print "[ADDING:]{0} {1} {2} to our catalogue.".format(brand, model_number, color)
32
            item = Item()
33
            item.manufacturer_name = brand
34
            item.model_number = model_number
35
            item.color = color
36
            item.status = status.IN_PROCESS
37
            item.addedOn = updatedOn
38
            session.add(item)
39
 
40
        #Set the catalog_item_id.
41
        similar_item = Item.query.filter_by(manufacturer_name = brand, model_number=model_number).first()
42
        print similar_item
43
        if similar_item is None or similar_item.catalog_item_id is None:
44
            #Use the entity_id_generator
45
            entity_id = EntityIDGenerator.query.first()
46
            item.catalog_item_id = entity_id.id + 1
47
            entity_id.id = entity_id.id  + 1
48
        else:
49
            #If it already exists for a brand and model_number, set it as same.
50
            item.catalog_item_id = similar_item.catalog_item_id
629 rajveer 51
 
52
        #added category and entity id
53
        '''
643 chandransh 54
        item.catalog_item_id = int(catalog_item_id)
629 rajveer 55
        item.category = int(category_id)
635 rajveer 56
        item.status = status.ACTIVE
629 rajveer 57
        '''
724 chandransh 58
        item.hotspotCategory = hotspot_category
626 chandransh 59
        item.model_name = model_name
629 rajveer 60
        if mrp != "":
61
            item.mrp = mrp
62
        if mop != "":
63
            item.mop = mop
64
        if dp != "":    
65
            item.dealerPrice = dp
635 rajveer 66
            item.sellingPrice = dp
724 chandransh 67
        if xfer_price !="":
68
            item.transfer_price = xfer_price
629 rajveer 69
        if weight != "":    
70
            item.weight = weight
631 chandransh 71
        if start_date != "":
72
            item.startDate = datetime.datetime(*xlrd.xldate_as_tuple(start_date, workbook.datemode))
626 chandransh 73
        item.bestDealText = deal_text
631 chandransh 74
        if deal_value != "":
75
            item.bestDealValue = deal_value
626 chandransh 76
        item.comments = comments
77
        item.updatedOn = updatedOn
724 chandransh 78
 
79
        item_change_log = ItemChangeLog()
80
        item_change_log.new_status = item.status
81
        item_change_log.timestamp = updatedOn
82
        item_change_log.item = item
626 chandransh 83
        session.commit()
84
 
85
    session.execute("UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", updatedOn='"+ str(updatedOn) +"' WHERE updatedOn <> '" + str(updatedOn) + "'", mapper=Item)
86
    session.commit()
87
    print "Successfully updated the item list information."
88
 
89
def main():
90
    parser = optparse.OptionParser()
91
    parser.add_option("-f", "--file", dest="filename",
92
                   default="ItemList.xls", type="string",
93
                   help="Read the item list from FILE",
94
                   metavar="FILE")
95
    (options, args) = parser.parse_args()
96
    if len(args) != 0:
97
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
98
    filename = options.filename
99
    load_item_data(filename)
100
 
101
if __name__ == '__main__':
102
    main()