| Line 19... |
Line 19... |
| 19 |
from shop2020.model.v1.catalog.impl import DataService
|
19 |
from shop2020.model.v1.catalog.impl import DataService
|
| 20 |
from shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGenerator,\
|
20 |
from shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGenerator,\
|
| 21 |
ItemChangeLog, Vendor, VendorItemPricing, VendorItemMapping
|
21 |
ItemChangeLog, Vendor, VendorItemPricing, VendorItemMapping
|
| 22 |
from elixir import *
|
22 |
from elixir import *
|
| 23 |
|
23 |
|
| 24 |
def load_item_data(filename, vendorId, category, full_update, dry_run, supplied_product_group):
|
24 |
def load_item_data(filename, vendorId, full_update, dry_run, supplied_product_group):
|
| 25 |
DataService.initialize('catalog')
|
25 |
DataService.initialize('catalog')
|
| 26 |
|
26 |
|
| 27 |
vendor = Vendor.get_by(id=vendorId)
|
27 |
vendor = Vendor.get_by(id=vendorId)
|
| 28 |
if vendor is None:
|
28 |
if vendor is None:
|
| 29 |
raise Exception("No vendor found for the id: " + str(vendorId))
|
29 |
raise Exception("No vendor found for the id: " + str(vendorId))
|
| Line 34... |
Line 34... |
| 34 |
updatedOn = datetime.datetime.now()
|
34 |
updatedOn = datetime.datetime.now()
|
| 35 |
new_items = []
|
35 |
new_items = []
|
| 36 |
updated_items = []
|
36 |
updated_items = []
|
| 37 |
not_created_items = []
|
37 |
not_created_items = []
|
| 38 |
|
38 |
|
| 39 |
existing_vendor_item_mappings = VendorItemMapping.query.filter_by(vendor=vendor, vendor_category=category).all()
|
39 |
existing_vendor_item_mappings = VendorItemMapping.query.filter_by(vendor=vendor).all()
|
| 40 |
existing_vendor_item_mappings_set = set([mapping.item_key for mapping in existing_vendor_item_mappings])
|
40 |
existing_vendor_item_mappings_set = set([mapping.item_key for mapping in existing_vendor_item_mappings])
|
| 41 |
|
41 |
|
| 42 |
for rownum in range(1, num_rows):
|
42 |
for rownum in range(1, num_rows):
|
| 43 |
print sheet.row_values(rownum)
|
43 |
print sheet.row_values(rownum)
|
| 44 |
|
44 |
|
| 45 |
if supplied_product_group != None and supplied_product_group != '':
|
45 |
if supplied_product_group != None and supplied_product_group != '':
|
| 46 |
our_brand, our_model_number, our_model_name, our_color,\
|
46 |
our_brand, our_model_number, our_model_name, our_color,\
|
| 47 |
brand, model_number, model_name, color,\
|
47 |
brand, model_number, model_name, color,\
|
| 48 |
dp, mrp, mop, sp, xfer_price,\
|
48 |
dp, mrp, mop, sp, xfer_price,\
|
| 49 |
comments, weight, start_date, deal_text, deal_value,\
|
49 |
comments, weight, start_date, deal_text, deal_value,\
|
| 50 |
warranty_period, defaultWarehouse, preferredVendor = sheet.row_values(rownum)[0:21]
|
50 |
warranty_period, defaultWarehouse, preferredWarehouse, preferredVendor = sheet.row_values(rownum)[0:22]
|
| 51 |
product_group = supplied_product_group
|
51 |
product_group = supplied_product_group
|
| 52 |
else:
|
52 |
else:
|
| 53 |
our_brand, our_model_number, our_model_name, our_color,\
|
53 |
our_brand, our_model_number, our_model_name, our_color,\
|
| 54 |
product_group, brand, model_number, model_name, color,\
|
54 |
product_group, brand, model_number, model_name, color,\
|
| 55 |
dp, mrp, mop, sp, xfer_price,\
|
55 |
dp, mrp, mop, sp, xfer_price,\
|
| 56 |
comments, weight, start_date, deal_text, deal_value,\
|
56 |
comments, weight, start_date, deal_text, deal_value,\
|
| 57 |
warranty_period, defaultWarehouse, preferredVendor = sheet.row_values(rownum)[0:22]
|
57 |
warranty_period, defaultWarehouse, preferredWarehouse, preferredVendor = sheet.row_values(rownum)[0:23]
|
| 58 |
|
58 |
|
| 59 |
print product_group
|
59 |
print product_group
|
| 60 |
|
60 |
|
| 61 |
if isinstance(model_number, float):
|
61 |
if isinstance(model_number, float):
|
| 62 |
model_number = str(int(model_number))
|
62 |
model_number = str(int(model_number))
|
| Line 122... |
Line 122... |
| 122 |
|
122 |
|
| 123 |
if item is None:
|
123 |
if item is None:
|
| 124 |
#print "[ADDING:]{0} {1} {2} {3} to our catalogue.".format(brand, model_number, model_name, color)
|
124 |
#print "[ADDING:]{0} {1} {2} {3} to our catalogue.".format(brand, model_number, model_name, color)
|
| 125 |
new_items.append(rownum)
|
125 |
new_items.append(rownum)
|
| 126 |
item = Item()
|
126 |
item = Item()
|
| 127 |
item.product_group = product_group.strip()
|
- |
|
| 128 |
item.brand = our_brand.strip()
|
127 |
item.brand = our_brand.strip()
|
| - |
|
128 |
item.product_group = product_group.strip()
|
| 129 |
item.model_number = our_model_number.strip()
|
129 |
item.model_number = our_model_number.strip()
|
| 130 |
item.model_name = our_model_name.strip()
|
130 |
item.model_name = our_model_name.strip()
|
| 131 |
item.color = our_color.strip()
|
131 |
item.color = our_color.strip()
|
| 132 |
item.status = status.IN_PROCESS
|
132 |
item.status = status.IN_PROCESS
|
| 133 |
item.status_description = "This item is in process"
|
133 |
item.status_description = "This item is in process"
|
| 134 |
item.addedOn = updatedOn
|
134 |
item.addedOn = updatedOn
|
| 135 |
item.hotspotCategory = category
|
- |
|
| 136 |
|
- |
|
| 137 |
if category == 'Handsets':
|
- |
|
| 138 |
item.preferredWarehouse = 1
|
- |
|
| 139 |
else:
|
- |
|
| 140 |
item.preferredWarehouse = 2
|
- |
|
| 141 |
|
135 |
|
| 142 |
vendor_item_mapping = VendorItemMapping(vendor=vendor, item=item, item_key=key, vendor_category=category)
|
136 |
vendor_item_mapping = VendorItemMapping(vendor=vendor, item=item, item_key=key)
|
| 143 |
vendor_item_pricing = VendorItemPricing(vendor=vendor, item=item)
|
137 |
vendor_item_pricing = VendorItemPricing(vendor=vendor, item=item)
|
| 144 |
|
138 |
|
| 145 |
session.add(item)
|
139 |
session.add(item)
|
| 146 |
session.add(vendor_item_mapping)
|
140 |
session.add(vendor_item_mapping)
|
| 147 |
session.add(vendor_item_pricing)
|
141 |
session.add(vendor_item_pricing)
|
| Line 156... |
Line 150... |
| 156 |
similar_item.catalog_item_id = entity_id.id
|
150 |
similar_item.catalog_item_id = entity_id.id
|
| 157 |
else:
|
151 |
else:
|
| 158 |
#If a similar item already exists for a product group, brand and model_number, set it as same.
|
152 |
#If a similar item already exists for a product group, brand and model_number, set it as same.
|
| 159 |
item.catalog_item_id = similar_item.catalog_item_id
|
153 |
item.catalog_item_id = similar_item.catalog_item_id
|
| 160 |
item.category = similar_item.category
|
154 |
item.category = similar_item.category
|
| - |
|
155 |
item.product_group = similar_item.product_group
|
| 161 |
item.status = similar_item.status
|
156 |
item.status = similar_item.status
|
| 162 |
item.status_description = similar_item.status_description
|
157 |
item.status_description = similar_item.status_description
|
| 163 |
#Use the same brand, model name and model number as in similar item in database
|
158 |
#Use the same brand, model name and model number as in similar item in database
|
| 164 |
item.brand = similar_item.brand
|
159 |
item.brand = similar_item.brand
|
| 165 |
item.model_name = similar_item.model_name
|
160 |
item.model_name = similar_item.model_name
|
| Line 174... |
Line 169... |
| 174 |
if check_color_valid(our_color) and (item.color == None or item.color.strip().lower() != our_color.strip().lower()):
|
169 |
if check_color_valid(our_color) and (item.color == None or item.color.strip().lower() != our_color.strip().lower()):
|
| 175 |
item.color = our_color.strip()
|
170 |
item.color = our_color.strip()
|
| 176 |
|
171 |
|
| 177 |
vendor_item_mapping = VendorItemMapping.get_by(vendor=vendor, item=item)
|
172 |
vendor_item_mapping = VendorItemMapping.get_by(vendor=vendor, item=item)
|
| 178 |
if vendor_item_mapping is None:
|
173 |
if vendor_item_mapping is None:
|
| 179 |
vendor_item_mapping = VendorItemMapping(vendor=vendor, item=item, item_key=key, vendor_category=category)
|
174 |
vendor_item_mapping = VendorItemMapping(vendor=vendor, item=item, item_key=key)
|
| 180 |
session.add(vendor_item_mapping)
|
175 |
session.add(vendor_item_mapping)
|
| 181 |
else:
|
176 |
else:
|
| 182 |
vendor_item_mapping.item_key = key
|
177 |
vendor_item_mapping.item_key = key
|
| 183 |
vendor_item_mapping.vendor_category = category
|
- |
|
| 184 |
|
178 |
|
| 185 |
vendor_item_pricing = VendorItemPricing.get_by(vendor=vendor, item=item)
|
179 |
vendor_item_pricing = VendorItemPricing.get_by(vendor=vendor, item=item)
|
| 186 |
if vendor_item_pricing is None:
|
180 |
if vendor_item_pricing is None:
|
| 187 |
vendor_item_pricing = VendorItemPricing(vendor=vendor, item=item)
|
181 |
vendor_item_pricing = VendorItemPricing(vendor=vendor, item=item)
|
| 188 |
session.add(vendor_item_pricing)
|
182 |
session.add(vendor_item_pricing)
|
| 189 |
|
183 |
|
| 190 |
if item.mrp != mrp or vendor_item_pricing.dealerPrice != dp or vendor_item_pricing.transfer_price != xfer_price or item.sellingPrice != sp:
|
184 |
if item.mrp != mrp or vendor_item_pricing.dealerPrice != dp or vendor_item_pricing.transfer_price != xfer_price or item.sellingPrice != sp:
|
| 191 |
updated_items.append(sheet.row_values(rownum)[0:19] + [item.mrp, item.dealerPrice, item.transfer_price])
|
185 |
updated_items.append(sheet.row_values(rownum)[0:19] + [item.mrp, vendor_item_pricing.dealerPrice, vendor_item_pricing.transfer_price])
|
| 192 |
|
186 |
|
| 193 |
if dp != "":
|
187 |
if dp != "":
|
| 194 |
item.dealerPrice = dp
|
- |
|
| 195 |
item.sellingPrice = dp
|
188 |
item.sellingPrice = dp
|
| 196 |
vendor_item_pricing.dealerPrice = dp
|
189 |
vendor_item_pricing.dealerPrice = dp
|
| 197 |
|
190 |
|
| 198 |
if mrp != "" and mop != "" and mrp < mop:
|
191 |
if mrp != "" and mop != "" and mrp < mop:
|
| 199 |
raise Exception("[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}".format(brand, model_number, model_name, color, mrp, mop))
|
192 |
raise Exception("[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}".format(brand, model_number, model_name, color, mrp, mop))
|
| Line 227... |
Line 220... |
| 227 |
|
220 |
|
| 228 |
if warranty_period != "":
|
221 |
if warranty_period != "":
|
| 229 |
try:
|
222 |
try:
|
| 230 |
item.warranty_period = int(warranty_period)
|
223 |
item.warranty_period = int(warranty_period)
|
| 231 |
except:
|
224 |
except:
|
| 232 |
pass
|
225 |
pass
|
| - |
|
226 |
|
| - |
|
227 |
if preferredWarehouse != "":
|
| - |
|
228 |
try:
|
| - |
|
229 |
item.preferredWarehouse = int(preferredWarehouse)
|
| - |
|
230 |
except:
|
| - |
|
231 |
pass
|
| 233 |
|
232 |
|
| 234 |
if mrp != "":
|
233 |
if mrp != "":
|
| 235 |
item.mrp = mrp
|
234 |
item.mrp = mrp
|
| 236 |
|
235 |
|
| 237 |
if mop != "":
|
236 |
if mop != "":
|
| 238 |
item.mop = mop
|
- |
|
| 239 |
vendor_item_pricing.mop = mop
|
237 |
vendor_item_pricing.mop = mop
|
| 240 |
|
238 |
|
| 241 |
if xfer_price !="":
|
239 |
if xfer_price !="":
|
| 242 |
item.transfer_price = xfer_price
|
- |
|
| 243 |
vendor_item_pricing.transfer_price = xfer_price
|
240 |
vendor_item_pricing.transfer_price = xfer_price
|
| 244 |
|
241 |
|
| 245 |
if start_date is not None and start_date != '':
|
242 |
if start_date is not None and start_date != '':
|
| 246 |
#If a start date has been specified, it takes precedence.
|
243 |
#If a start date has been specified, it takes precedence.
|
| 247 |
item.startDate = datetime.datetime(*xlrd.xldate_as_tuple(start_date, workbook.datemode))
|
244 |
item.startDate = datetime.datetime(*xlrd.xldate_as_tuple(start_date, workbook.datemode))
|
| Line 275... |
Line 272... |
| 275 |
phased_out_items = [key.split('|') for key in list(existing_vendor_item_mappings_set)]
|
272 |
phased_out_items = [key.split('|') for key in list(existing_vendor_item_mappings_set)]
|
| 276 |
write_report("phased_out_items.csv", phased_out_items, sheet, True)
|
273 |
write_report("phased_out_items.csv", phased_out_items, sheet, True)
|
| 277 |
|
274 |
|
| 278 |
if (not dry_run) and full_update:
|
275 |
if (not dry_run) and full_update:
|
| 279 |
query_string = "UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", status_description='This item has been phased out'" + ", updatedOn='"+ str(updatedOn) +"'"+\
|
276 |
query_string = "UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", status_description='This item has been phased out'" + ", updatedOn='"+ str(updatedOn) +"'"+\
|
| 280 |
" WHERE updatedOn <> '" + str(updatedOn) + "' AND hotspotCategory='" + category +"'";
|
277 |
" WHERE updatedOn <> '" + str(updatedOn) + "'";
|
| 281 |
session.execute(query_string, mapper=Item)
|
278 |
session.execute(query_string, mapper=Item)
|
| 282 |
session.commit()
|
279 |
session.commit()
|
| 283 |
|
280 |
|
| 284 |
print "Successfully updated the item list information."
|
281 |
print "Successfully updated the item list information."
|
| 285 |
|
282 |
|
| Line 313... |
Line 310... |
| 313 |
parser = optparse.OptionParser()
|
310 |
parser = optparse.OptionParser()
|
| 314 |
parser.add_option("-f", "--file", dest="filename",
|
311 |
parser.add_option("-f", "--file", dest="filename",
|
| 315 |
default="ItemList.xls", type="string",
|
312 |
default="ItemList.xls", type="string",
|
| 316 |
help="Read the item list from FILE",
|
313 |
help="Read the item list from FILE",
|
| 317 |
metavar="FILE")
|
314 |
metavar="FILE")
|
| 318 |
parser.add_option("-c", "--category", dest="category",
|
- |
|
| 319 |
type="string",
|
- |
|
| 320 |
help="Update the list only for the products belonging to CATEGORY",
|
- |
|
| 321 |
metavar="CATEGORY")
|
- |
|
| 322 |
parser.add_option("-v", "--vendor", dest="vendor",
|
315 |
parser.add_option("-v", "--vendor", dest="vendor",
|
| 323 |
type="int",
|
316 |
type="int",
|
| 324 |
help="Update the pricing information for VENDOR",
|
317 |
help="Update the pricing information for VENDOR",
|
| 325 |
metavar="VENDOR")
|
318 |
metavar="VENDOR")
|
| 326 |
parser.add_option("-d", "--dry-run", dest="dry_run",
|
319 |
parser.add_option("-d", "--dry-run", dest="dry_run",
|
| Line 334... |
Line 327... |
| 334 |
help="In a partial update, older items are left as is. Also see -fm.")
|
327 |
help="In a partial update, older items are left as is. Also see -fm.")
|
| 335 |
parser.add_option("-g", "--product-group", dest="product_group",
|
328 |
parser.add_option("-g", "--product-group", dest="product_group",
|
| 336 |
type="string",
|
329 |
type="string",
|
| 337 |
help="Set GROUP as the product group for all items added/updated during this run.",
|
330 |
help="Set GROUP as the product group for all items added/updated during this run.",
|
| 338 |
metavar="GROUP")
|
331 |
metavar="GROUP")
|
| 339 |
parser.set_defaults(full_update=True, dry_run=False, category="Handsets", vendor=1)
|
332 |
parser.set_defaults(full_update=True, dry_run=False, vendor=1)
|
| 340 |
(options, args) = parser.parse_args()
|
333 |
(options, args) = parser.parse_args()
|
| 341 |
if len(args) != 0:
|
334 |
if len(args) != 0:
|
| 342 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
335 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
| 343 |
filename = options.filename
|
336 |
filename = options.filename
|
| 344 |
load_item_data(filename, options.vendor, options.category, options.full_update, options.dry_run, options.product_group)
|
337 |
load_item_data(filename, options.vendor, options.full_update, options.dry_run, options.product_group)
|
| 345 |
|
338 |
|
| 346 |
if __name__ == '__main__':
|
339 |
if __name__ == '__main__':
|
| 347 |
main()
|
340 |
main()
|