| Line 11... |
Line 11... |
| 11 |
sys.path.insert(0, os.getcwd())
|
11 |
sys.path.insert(0, os.getcwd())
|
| 12 |
|
12 |
|
| 13 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status
|
13 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status
|
| 14 |
from shop2020.model.v1.catalog.impl import DataService
|
14 |
from shop2020.model.v1.catalog.impl import DataService
|
| 15 |
from shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGenerator,\
|
15 |
from shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGenerator,\
|
| 16 |
ItemChangeLog
|
16 |
ItemChangeLog, Vendor, VendorItemPricing, VendorItemMapping
|
| 17 |
from elixir import *
|
17 |
from elixir import *
|
| 18 |
|
18 |
|
| 19 |
def load_item_data(filename, category, full_update, dry_run):
|
19 |
def load_item_data(filename, vendorId, category, full_update, dry_run, product_group):
|
| 20 |
DataService.initialize('catalog')
|
20 |
DataService.initialize('catalog')
|
| - |
|
21 |
|
| - |
|
22 |
vendor = Vendor.get_by(id=vendorId)
|
| - |
|
23 |
if vendor is None:
|
| - |
|
24 |
raise Exception("No vendor found for the id: " + str(vendorId))
|
| 21 |
|
25 |
|
| 22 |
workbook = xlrd.open_workbook(filename)
|
26 |
workbook = xlrd.open_workbook(filename)
|
| 23 |
sheet = workbook.sheet_by_index(0)
|
27 |
sheet = workbook.sheet_by_index(0)
|
| 24 |
num_rows = sheet.nrows
|
28 |
num_rows = sheet.nrows
|
| 25 |
updatedOn = datetime.datetime.now()
|
29 |
updatedOn = datetime.datetime.now()
|
| 26 |
new_items = []
|
30 |
new_items = []
|
| 27 |
updated_items = []
|
31 |
updated_items = []
|
| - |
|
32 |
|
| 28 |
existing_items = Item.query.filter_by(status=status.ACTIVE, hotspotCategory=category).all()
|
33 |
existing_vendor_item_mappings = VendorItemMapping.query.filter_by(vendor=vendor, vendor_category=category).all()
|
| 29 |
existing_items_set = set([item.brand+';'+item.model_number+';'+item.color for item in existing_items])
|
34 |
existing_vendor_item_mappings_set = set([mapping.item_key for mapping in existing_vendor_item_mappings])
|
| - |
|
35 |
|
| 30 |
for rownum in range(1, num_rows):
|
36 |
for rownum in range(1, num_rows):
|
| 31 |
#print sheet.row_values(rownum)
|
37 |
#print sheet.row_values(rownum)
|
| 32 |
catalog_item_id, category_id, product_group = [None, None, None]
|
38 |
catalog_item_id, category_id = [None, None]
|
| 33 |
|
39 |
|
| 34 |
#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]
|
40 |
#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]
|
| 35 |
brand, model_number, model_name, color, dp, mrp, mop, comments, hotspot_category, unused_feature, xfer_price, weight, start_date, deal_text, deal_value, catalog_item_id, category_id = sheet.row_values(rownum)[0:17]
|
41 |
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]
|
| 36 |
#product_group, brand, model_number, color, xfer_price, mop, mrp, catalog_item_id, category_id = sheet.row_values(rownum)[0:9]
|
42 |
#product_group, brand, model_number, color, xfer_price, mop, mrp, catalog_item_id, category_id = sheet.row_values(rownum)[0:9]
|
| 37 |
if isinstance(model_number, float):
|
43 |
if isinstance(model_number, float):
|
| 38 |
model_number = str(int(model_number))
|
44 |
model_number = str(int(model_number))
|
| 39 |
|
45 |
|
| - |
|
46 |
item = None
|
| 40 |
key = brand + ';' + model_number + ';' + color
|
47 |
vendor_item_pricing = None
|
| - |
|
48 |
|
| - |
|
49 |
key = product_group.strip().lower() + '|' + brand.strip().lower() + '|' + model_number.strip().lower() + '|' + color.strip().lower()
|
| 41 |
if key in existing_items_set:
|
50 |
if key in existing_vendor_item_mappings_set:
|
| 42 |
existing_items_set.remove(key)
|
51 |
existing_vendor_item_mappings_set.remove(key)
|
| 43 |
item = Item.get_by(brand=brand, model_number=model_number, color=color, hotspotCategory=category)
|
52 |
item = VendorItemMapping.query.filter_by(vendor=vendor, item_key=key, vendor_category=category).one().item
|
| 44 |
#item = Item.get_by(product_group=product_group, brand=brand, model_number=model_number, color=color)
|
- |
|
| 45 |
|
53 |
|
| 46 |
if item is None:
|
54 |
if item is None:
|
| 47 |
#print "[ADDING:]{0} {1} {2} {3} to our catalogue.".format(product_group, brand, model_number, color)
|
55 |
#print "[ADDING:]{0} {1} {2} {3} to our catalogue.".format(product_group, brand, model_number, color)
|
| 48 |
new_items.append(rownum)
|
56 |
new_items.append(rownum)
|
| 49 |
item = Item()
|
57 |
item = Item()
|
| 50 |
#item.product_group = product_group
|
58 |
item.product_group = product_group
|
| 51 |
item.brand = brand
|
59 |
item.brand = brand
|
| 52 |
item.model_number = model_number
|
60 |
item.model_number = model_number
|
| 53 |
item.color = color
|
61 |
item.color = color
|
| 54 |
item.status = status.IN_PROCESS
|
62 |
item.status = status.IN_PROCESS
|
| 55 |
item.addedOn = updatedOn
|
63 |
item.addedOn = updatedOn
|
| 56 |
item.hotspotCategory = category
|
64 |
item.hotspotCategory = category
|
| - |
|
65 |
|
| - |
|
66 |
vendor_item_mapping = VendorItemMapping(vendor=vendor, item=item, item_key=key, vendor_category=category)
|
| - |
|
67 |
|
| 57 |
session.add(item)
|
68 |
session.add(item)
|
| - |
|
69 |
session.add(vendor_item_mapping)
|
| 58 |
|
70 |
|
| 59 |
if catalog_item_id !=None and catalog_item_id != "":
|
71 |
if catalog_item_id !=None and catalog_item_id != "":
|
| 60 |
#Add category and entity id
|
72 |
#Add category and entity id
|
| 61 |
item.catalog_item_id = int(catalog_item_id)
|
73 |
item.catalog_item_id = int(catalog_item_id)
|
| 62 |
item.category = int(category_id)
|
74 |
item.category = int(category_id)
|
| 63 |
item.status = status.ACTIVE
|
75 |
item.status = status.ACTIVE
|
| 64 |
else:
|
76 |
else:
|
| 65 |
'''
|
- |
|
| 66 |
This is to be used for accessories.
|
- |
|
| 67 |
'''
|
- |
|
| 68 |
# Check if a similar item already exists in our database
|
77 |
# Check if a similar item already exists in our database
|
| 69 |
#similar_item = Item.query.filter_by(product_group=product_group, brand = brand, model_number=model_number).first()
|
78 |
similar_item = Item.query.filter_by(product_group=product_group, brand = brand, model_number=model_number, hotspotCategory=category).first()
|
| 70 |
#print "[SIMILAR ITEM FOUND:] FOR {0} {1} {2} {3}".format(product_group, brand, model_number, color)
|
79 |
print "[SIMILAR ITEM FOUND:] FOR {0} {1} {2} {3}".format(product_group, brand, model_number, color)
|
| 71 |
|
80 |
|
| 72 |
similar_item = Item.query.filter_by(brand = brand, model_number=model_number, hotspotCategory=category).first()
|
- |
|
| 73 |
print "[SIMILAR ITEM FOUND:] FOR {0} {1} {2} {3}".format(category, brand, model_number, color)
|
- |
|
| 74 |
|
- |
|
| 75 |
if similar_item is None or similar_item.catalog_item_id is None:
|
81 |
if similar_item is None or similar_item.catalog_item_id is None:
|
| 76 |
# If there is no similar item in the database from before,
|
82 |
# If there is no similar item in the database from before,
|
| 77 |
# use the entity_id_generator
|
83 |
# use the entity_id_generator
|
| 78 |
entity_id = EntityIDGenerator.query.first()
|
84 |
entity_id = EntityIDGenerator.query.first()
|
| 79 |
item.catalog_item_id = entity_id.id + 1
|
85 |
item.catalog_item_id = entity_id.id + 1
|
| 80 |
entity_id.id = entity_id.id + 1
|
86 |
entity_id.id = entity_id.id + 1
|
| 81 |
else:
|
87 |
else:
|
| 82 |
#If a similar item already exists for a product group, brand and model_number, set it as same.
|
88 |
#If a similar item already exists for a product group, brand and model_number, set it as same.
|
| 83 |
item.catalog_item_id = similar_item.catalog_item_id
|
89 |
item.catalog_item_id = similar_item.catalog_item_id
|
| 84 |
item.category = similar_item.category
|
90 |
item.category = similar_item.category
|
| 85 |
#item.status = status.ACTIVE
|
91 |
item.status = similar_item.status
|
| 86 |
else:
|
92 |
else:
|
| 87 |
#Check if this item already existed and one of its price parameters has changed
|
93 |
# If this item already existed and one of its price parameters has changed
|
| 88 |
# in which case we add it to the list of updated items for reporting.
|
94 |
# in which case we add it to the list of updated items for reporting.
|
| - |
|
95 |
vendor_item_pricing = VendorItemPricing.get_by(vendor=vendor, item=item)
|
| - |
|
96 |
if vendor_item_pricing is None:
|
| - |
|
97 |
vendor_item_pricing = VendorItemPricing(vendor=vendor, item=item)
|
| - |
|
98 |
session.add(vendor_item_pricing)
|
| - |
|
99 |
|
| 89 |
if item.mrp != mrp or item.dealerPrice != dp or item.transfer_price != xfer_price:
|
100 |
if item.mrp != mrp or vendor_item_pricing.dealerPrice != dp or vendor_item_pricing.transfer_price != xfer_price:
|
| 90 |
updated_items.append(sheet.row_values(rownum)[0:15] + [item.mrp, item.dealerPrice, item.transfer_price])
|
101 |
updated_items.append(sheet.row_values(rownum)[0:15] + [item.mrp, item.dealerPrice, item.transfer_price])
|
| 91 |
|
102 |
|
| 92 |
if category_id !=None and category_id != "":
|
103 |
if category_id !=None and category_id != "":
|
| 93 |
item.category = int(category_id)
|
104 |
item.category = int(category_id)
|
| 94 |
|
105 |
|
| 95 |
if dp != "":
|
106 |
if dp != "":
|
| 96 |
item.dealerPrice = dp
|
107 |
item.dealerPrice = dp
|
| 97 |
item.sellingPrice = dp
|
108 |
item.sellingPrice = dp
|
| - |
|
109 |
vendor_item_pricing.dealerPrice = dp
|
| 98 |
|
110 |
|
| 99 |
if mrp != "" and mop != "" and mrp < mop:
|
111 |
if mrp != "" and mop != "" and mrp < mop:
|
| 100 |
raise Exception("[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}".format(product_group, brand, model_number, color, mrp, mop))
|
112 |
raise Exception("[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}".format(product_group, brand, model_number, color, mrp, mop))
|
| 101 |
|
113 |
|
| 102 |
if mop != "" and xfer_price != "" and xfer_price > mop:
|
114 |
if mop != "" and xfer_price != "" and xfer_price > mop:
|
| Line 107... |
Line 119... |
| 107 |
# else:
|
119 |
# else:
|
| 108 |
# raise Exception("[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))
|
120 |
# raise Exception("[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))
|
| 109 |
|
121 |
|
| 110 |
if mop != "":
|
122 |
if mop != "":
|
| 111 |
item.mop = mop
|
123 |
item.mop = mop
|
| - |
|
124 |
vendor_item_pricing.mop = mop
|
| 112 |
item.sellingPrice = mop
|
125 |
item.sellingPrice = mop
|
| 113 |
|
126 |
|
| 114 |
if xfer_price !="":
|
127 |
if xfer_price !="":
|
| 115 |
item.transfer_price = xfer_price
|
128 |
item.transfer_price = xfer_price
|
| - |
|
129 |
vendor_item_pricing.transfer_price = xfer_price
|
| 116 |
|
130 |
|
| 117 |
item.startDate = datetime.datetime.now()
|
131 |
item.startDate = datetime.datetime.now()
|
| 118 |
|
132 |
|
| 119 |
item.model_name = model_name
|
133 |
item.model_name = model_name
|
| 120 |
|
134 |
|
| Line 138... |
Line 152... |
| 138 |
if not dry_run:
|
152 |
if not dry_run:
|
| 139 |
session.commit()
|
153 |
session.commit()
|
| 140 |
|
154 |
|
| 141 |
write_report("new_items.csv", new_items, sheet, False)
|
155 |
write_report("new_items.csv", new_items, sheet, False)
|
| 142 |
write_report("updated_items.csv", updated_items, sheet, True)
|
156 |
write_report("updated_items.csv", updated_items, sheet, True)
|
| 143 |
phased_out_items = [key.split(';') for key in list(existing_items_set)]
|
157 |
phased_out_items = [key.split('|') for key in list(existing_vendor_item_mappings_set)]
|
| 144 |
write_report("phased_out_items.csv", phased_out_items, sheet, True)
|
158 |
write_report("phased_out_items.csv", phased_out_items, sheet, True)
|
| 145 |
|
159 |
|
| 146 |
if (not dry_run) and full_update:
|
160 |
if (not dry_run) and full_update:
|
| 147 |
query_string = "UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", updatedOn='"+ str(updatedOn) +"'"+\
|
161 |
query_string = "UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", updatedOn='"+ str(updatedOn) +"'"+\
|
| 148 |
" WHERE updatedOn <> '" + str(updatedOn) + "' AND hotspotCategory='" + category +"'";
|
162 |
" WHERE updatedOn <> '" + str(updatedOn) + "' AND hotspotCategory='" + category +"'";
|
| Line 171... |
Line 185... |
| 171 |
metavar="FILE")
|
185 |
metavar="FILE")
|
| 172 |
parser.add_option("-c", "--category", dest="category",
|
186 |
parser.add_option("-c", "--category", dest="category",
|
| 173 |
type="string",
|
187 |
type="string",
|
| 174 |
help="Update the list only for the products belonging to CATEGORY",
|
188 |
help="Update the list only for the products belonging to CATEGORY",
|
| 175 |
metavar="CATEGORY")
|
189 |
metavar="CATEGORY")
|
| - |
|
190 |
parser.add_option("-v", "--vendor", dest="vendor",
|
| - |
|
191 |
type="int",
|
| - |
|
192 |
help="Update the pricing information for VENDOR",
|
| - |
|
193 |
metavar="VENDOR")
|
| 176 |
parser.add_option("-d", "--dry-run", dest="dry_run",
|
194 |
parser.add_option("-d", "--dry-run", dest="dry_run",
|
| 177 |
action="store_true",
|
195 |
action="store_true",
|
| 178 |
help="Dry run only reporting on pending changes.Please note that some of the items can be reported twice.")
|
196 |
help="Dry run only reporting on pending changes.Please note that some of the items can be reported twice.")
|
| 179 |
parser.add_option("-a", "--full", dest="full_update",
|
197 |
parser.add_option("-a", "--full", dest="full_update",
|
| 180 |
action="store_true",
|
198 |
action="store_true",
|
| 181 |
help="In a full update, all older items are marked as PHASED_OUT. Also see -pm.")
|
199 |
help="In a full update, all older items are marked as PHASED_OUT. Also see -pm.")
|
| 182 |
parser.add_option("-p", "--partial", dest="full_update",
|
200 |
parser.add_option("-p", "--partial", dest="full_update",
|
| 183 |
action="store_false",
|
201 |
action="store_false",
|
| 184 |
help="In a partial update, older items are left as is. Also see -fm.")
|
202 |
help="In a partial update, older items are left as is. Also see -fm.")
|
| - |
|
203 |
parser.add_option("-g", "--product-group", dest="product_group",
|
| - |
|
204 |
type="string",
|
| - |
|
205 |
help="Set GROUP as the product group for all items added/updated during this run.",
|
| - |
|
206 |
metavar="GROUP")
|
| 185 |
parser.set_defaults(full_update=True, dry_run=False, category="Handsets")
|
207 |
parser.set_defaults(full_update=True, dry_run=False, category="Handsets", vendor=1)
|
| 186 |
(options, args) = parser.parse_args()
|
208 |
(options, args) = parser.parse_args()
|
| 187 |
if len(args) != 0:
|
209 |
if len(args) != 0:
|
| 188 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
210 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
| 189 |
filename = options.filename
|
211 |
filename = options.filename
|
| 190 |
load_item_data(filename, options.category, options.full_update, options.dry_run)
|
212 |
load_item_data(filename, options.vendor, options.category, options.full_update, options.dry_run, options.product_group)
|
| 191 |
|
213 |
|
| 192 |
if __name__ == '__main__':
|
214 |
if __name__ == '__main__':
|
| 193 |
main()
|
215 |
main()
|