Subversion Repositories SmartDukaan

Rev

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