Subversion Repositories SmartDukaan

Rev

Rev 1080 | Rev 1250 | 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
1080 chandransh 2
 
626 chandransh 3
import optparse
1080 chandransh 4
import csv
626 chandransh 5
import xlrd
6
import datetime
7
 
8
if __name__ == '__main__' and __package__ is None:
9
    import sys
10
    import os
11
    sys.path.insert(0, os.getcwd())
12
 
13
from shop2020.thriftpy.model.v1.catalog.ttypes import status
14
from shop2020.model.v1.catalog.impl import DataService
724 chandransh 15
from shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGenerator,\
16
    ItemChangeLog
626 chandransh 17
from elixir import *
18
 
1080 chandransh 19
def load_item_data(filename, category, full_update, dry_run):
626 chandransh 20
    DataService.initialize()
21
 
22
    workbook = xlrd.open_workbook(filename)
23
    sheet = workbook.sheet_by_index(0)
24
    num_rows = sheet.nrows
25
    updatedOn = datetime.datetime.now()
1080 chandransh 26
    new_items = []
27
    old_items = []
626 chandransh 28
    for rownum in range(1, num_rows):
1080 chandransh 29
        #print sheet.row_values(rownum)
30
        catalog_item_id, category_id, product_group = [None, None, None]
873 rajveer 31
        #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]
1080 chandransh 32
        brand, model_number, model_name, color, dp, mrp, mop, comments, hotspot_category, unused_feature, xfer_price, weight, start_date, deal_text, deal_value = sheet.row_values(rownum)[0:15]
33
        #product_group, brand, model_number, color, xfer_price, mop, mrp, catalog_item_id, category_id = sheet.row_values(rownum)[0:9]
626 chandransh 34
        if isinstance(model_number, float):
35
            model_number = str(int(model_number))
1080 chandransh 36
        item = Item.get_by(brand=brand, model_number=model_number, color=color, hotspotCategory=category)
37
        #item = Item.get_by(product_group=product_group, brand=brand, model_number=model_number, color=color)
38
 
626 chandransh 39
        if item is None:
1080 chandransh 40
            #print "[ADDING:]{0} {1} {2} {3} to our catalogue.".format(product_group, brand, model_number, color)
41
            new_items.append(rownum)
626 chandransh 42
            item = Item()
1080 chandransh 43
            #item.product_group = product_group
44
            item.brand = brand
626 chandransh 45
            item.model_number = model_number
46
            item.color = color
47
            item.status = status.IN_PROCESS
48
            item.addedOn = updatedOn
1089 chandransh 49
            item.hotspotCategory = category
626 chandransh 50
            session.add(item)
1080 chandransh 51
 
52
            if catalog_item_id !=None and catalog_item_id != "":
53
                #Add category and entity id
54
                item.catalog_item_id = int(catalog_item_id)
55
                item.category = int(category_id)
56
                item.status = status.ACTIVE
57
            else:
58
                '''
59
                This is to be used for accessories.
60
                '''
61
                # Check if a similar item already exists in our database
62
                #similar_item = Item.query.filter_by(product_group=product_group, brand = brand, model_number=model_number).first()
63
                #print "[SIMILAR ITEM FOUND:] FOR {0} {1} {2} {3}".format(product_group, brand, model_number, color)
64
 
65
                similar_item = Item.query.filter_by(brand = brand, model_number=model_number, hotspotCategory=category).first()
66
                print "[SIMILAR ITEM FOUND:] FOR {0} {1} {2} {3}".format(category, brand, model_number, color)
67
 
68
                if similar_item is None or similar_item.catalog_item_id is None:
69
                    # If there is no similar item in the database from before,
70
                    # use the entity_id_generator
71
                    entity_id = EntityIDGenerator.query.first()
72
                    item.catalog_item_id = entity_id.id + 1
73
                    entity_id.id = entity_id.id  + 1
74
                else:
75
                    #If a similar item already exists for a product group, brand and model_number, set it as same.
76
                    item.catalog_item_id = similar_item.catalog_item_id
77
                    item.category = similar_item.category
78
                    #item.status = status.ACTIVE
626 chandransh 79
        else:
1080 chandransh 80
            old_items.append(rownum)
81
 
82
        if category_id !=None and category_id != "":
83
            item.category = int(category_id)
84
 
85
        if dp != "":
86
            item.dealerPrice = dp
87
            item.sellingPrice = dp
629 rajveer 88
 
1080 chandransh 89
        if mrp != "" and mop != "" and mrp <  mop:
90
            print "[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}".format(product_group, brand, model_number, color, mrp, mop)
91
 
92
        if mop != "" and xfer_price != "" and xfer_price > mop:
93
            print "[BAD MOP and TP:] for {0} {1} {2} {3}. TP={4}. MOP={5}".format(product_group, brand, model_number, color, xfer_price, mop)
94
        if mrp != "":
95
            if item.mrp == None or item.mrp >= mrp:
96
                item.mrp = mrp
97
            else:
98
                print "[NEW MRP MORE THAN old MRP:] for {0} {1} {2} {3}. Old mrp={4}. New MRP={5}".format(product_group, brand, model_number, color, item.mrp, mrp)
873 rajveer 99
 
629 rajveer 100
        if mop != "":
101
            item.mop = mop
1080 chandransh 102
            item.sellingPrice = mop
103
 
724 chandransh 104
        if xfer_price !="":
105
            item.transfer_price = xfer_price
1080 chandransh 106
 
107
        item.startDate = datetime.datetime.now()
108
 
109
        item.model_name = model_name
110
 
629 rajveer 111
        if weight != "":    
112
            item.weight = weight
631 chandransh 113
        if start_date != "":
114
            item.startDate = datetime.datetime(*xlrd.xldate_as_tuple(start_date, workbook.datemode))
1080 chandransh 115
 
116
#        item.bestDealText = deal_text
117
#        if deal_value != "":
118
#            item.bestDealValue = deal_value
119
#        item.comments = comments
120
 
626 chandransh 121
        item.updatedOn = updatedOn
724 chandransh 122
 
123
        item_change_log = ItemChangeLog()
124
        item_change_log.new_status = item.status
125
        item_change_log.timestamp = updatedOn
126
        item_change_log.item = item
1080 chandransh 127
 
128
        if not dry_run:
129
            session.commit()
130
 
131
    write_report("new_items.csv", new_items, sheet)
132
    write_report("updated_items.csv", old_items, sheet)
133
 
134
    if (not dry_run) and full_update:
135
        query_string =  "UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", updatedOn='"+ str(updatedOn) +"'"+\
136
                        " WHERE updatedOn <> '" + str(updatedOn) + "' AND hotspotCategory='" + category +"'";
137
        session.execute(query_string, mapper=Item)
626 chandransh 138
        session.commit()
139
 
140
    print "Successfully updated the item list information."
141
 
1080 chandransh 142
def write_report(filename, items, sheet):
143
    items_writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_ALL)
144
    items_writer.writerow(sheet.row_values(0))
145
    for i in items:
146
        #print sheet.row_values(i)
147
        items_writer.writerow([str(value) for value in sheet.row_values(i)])
148
 
626 chandransh 149
def main():
150
    parser = optparse.OptionParser()
151
    parser.add_option("-f", "--file", dest="filename",
152
                   default="ItemList.xls", type="string",
153
                   help="Read the item list from FILE",
154
                   metavar="FILE")
1080 chandransh 155
    parser.add_option("-c", "--category", dest="category",
156
                      type="string",
157
                      help="Update the list only for the products belonging to CATEGORY",
158
                      metavar="CATEGORY")
159
    parser.add_option("-d", "--dry-run", dest="dry_run",
160
                      action="store_true",
161
                      help="Dry run only reporting on pending changes.Please note that some of the items can be reported twice.")
162
    parser.add_option("-a", "--full", dest="full_update",
163
                      action="store_true",
164
                      help="In a full update, all older items are marked as PHASED_OUT. Also see -pm.")
165
    parser.add_option("-p", "--partial", dest="full_update",
166
                      action="store_false",
167
                      help="In a partial update, older items are left as is. Also see -fm.")
168
    parser.set_defaults(full_update=True, dry_run=False, category="Handsets")
626 chandransh 169
    (options, args) = parser.parse_args()
170
    if len(args) != 0:
171
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
172
    filename = options.filename
1080 chandransh 173
    load_item_data(filename, options.category, options.full_update, options.dry_run)
626 chandransh 174
 
175
if __name__ == '__main__':
176
    main()