Subversion Repositories SmartDukaan

Rev

Rev 724 | Rev 873 | 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)
766 rajveer 39
	'''            
626 chandransh 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
766 rajveer 51
        '''
52
        #added category and entity id
629 rajveer 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
724 chandransh 57
        item.hotspotCategory = hotspot_category
766 rajveer 58
 
626 chandransh 59
        item.model_name = model_name
629 rajveer 60
        if mrp != "":
61
            item.mrp = mrp
766 rajveer 62
	else:
63
	    item.mrp = dp + dp/5
629 rajveer 64
        if mop != "":
65
            item.mop = mop
66
        if dp != "":    
67
            item.dealerPrice = dp
635 rajveer 68
            item.sellingPrice = dp
766 rajveer 69
        else:
70
	    item.dealerPrice = mrp - mrp/5
71
	    item.sellingPrice= mrp - mrp/5
724 chandransh 72
        if xfer_price !="":
73
            item.transfer_price = xfer_price
629 rajveer 74
        if weight != "":    
75
            item.weight = weight
631 chandransh 76
        if start_date != "":
77
            item.startDate = datetime.datetime(*xlrd.xldate_as_tuple(start_date, workbook.datemode))
626 chandransh 78
        item.bestDealText = deal_text
631 chandransh 79
        if deal_value != "":
80
            item.bestDealValue = deal_value
626 chandransh 81
        item.comments = comments
82
        item.updatedOn = updatedOn
724 chandransh 83
 
84
        item_change_log = ItemChangeLog()
85
        item_change_log.new_status = item.status
86
        item_change_log.timestamp = updatedOn
87
        item_change_log.item = item
626 chandransh 88
        session.commit()
89
 
90
    session.execute("UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", updatedOn='"+ str(updatedOn) +"' WHERE updatedOn <> '" + str(updatedOn) + "'", mapper=Item)
91
    session.commit()
92
    print "Successfully updated the item list information."
93
 
94
def main():
95
    parser = optparse.OptionParser()
96
    parser.add_option("-f", "--file", dest="filename",
97
                   default="ItemList.xls", type="string",
98
                   help="Read the item list from FILE",
99
                   metavar="FILE")
100
    (options, args) = parser.parse_args()
101
    if len(args) != 0:
102
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
103
    filename = options.filename
104
    load_item_data(filename)
105
 
106
if __name__ == '__main__':
107
    main()