Subversion Repositories SmartDukaan

Rev

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