def update_stock(stock_by_store): product_sync_ids = list( map( lambda x: x.product_or_variant_sync_id, filter(lambda x: x.type_enum == StockTypeEnum.PRODUCT, stock_by_store))) product_syncs = list( ProductSync.objects.filter(sync_id__in=product_sync_ids)) product_map = { product_sync.sync_id: product_sync.product for product_sync in product_syncs } variant_sync_ids = list( map( lambda x: x.product_or_variant_sync_id, filter(lambda x: x.type_enum == StockTypeEnum.VARIANT, stock_by_store))) variant_syncs = list( VariantSync.objects.filter(sync_id__in=variant_sync_ids)) variant_map = { product_sync.sync_id: product_sync.product for product_sync in variant_syncs } product_map.update(variant_map) products = product_map.values() stock_records = list( StockRecord.objects.select_related( 'product', 'partner').filter(product__in=products)) product_stock_map = {(stock_record.product.id, stock_record.partner.id): stock_record for stock_record in stock_records} updated_stock_records = [] added_stock_records = [] for stock in stock_by_store: product = product_map.get(stock.product_or_variant_sync_id) partner = stock.store.partner if product is not None and partner is not None: stock_record = product_stock_map.get((product.id, partner.id)) if stock_record: stock_record.num_in_stock = stock.stock if stock.stock > 0 else 0 updated_stock_records.append(stock_record) else: stock_record = StockRecord(product=product, partner=partner) stock_record.partner_sku = partner_sku_func(partner, product) stock_record.num_in_stock = stock.stock if stock.stock > 0 else 0 added_stock_records.append(stock_record) StockRecord.objects.update(num_in_stock=0) update_result = bulk_update(updated_stock_records) insert_result = StockRecord.objects.bulk_create(added_stock_records)
def update_price_product_from_sync_obj(product_syncs): products = list(map(lambda x: x.product, product_syncs)) partners = list(Partner.objects.all()) stock_records = list( StockRecord.objects.filter(product__in=products, partner__in=partners)) product_stock_map = { str(stock_record.product.id) + '_' + str(stock_record.partner.id): stock_record for stock_record in stock_records } updated_stock_records = [] added_stock_records = [] for product_sync in product_syncs: for partner in partners: product = product_sync.product stock_record = product_stock_map.get( str(product.id) + '_' + str(partner.id)) if stock_record: stock_record.price_excl_tax = product_sync.price if product_sync.price else 0 stock_record.price_retail = product_sync.price # stock_record.save() updated_stock_records.append(stock_record) else: stock_record = StockRecord(product=product, partner=partner) stock_record.partner_sku = partner_sku_func(partner, product) stock_record.price_excl_tax = product_sync.price if product_sync.price else 0 stock_record.price_retail = product_sync.price if product_sync.price else 0 stock_record.num_in_stock = 0 added_stock_records.append(stock_record) update_result = bulk_update(updated_stock_records) insert_result = StockRecord.objects.bulk_create(added_stock_records)
def populate(): title = 'Petzo Nourish Box' slug = 'petzo-nourish-box' description = {} product_class = ProductClass.objects.get(name='Food') product_category = Category.objects.get(name='Food') structure = Product.STANDALONE partner = Partner.objects.get(name='Petzo India Pvt Ltd') product = Product() product.structure = Product.STANDALONE product.title = title product.slug = slug product.product_class = product_class stockRecord = StockRecord() stockRecord.partner = partner stockRecord.price_currency = 'INR' stockRecord.num_in_stock = 100 with open('list.csv', 'rb') as csvFile: reader = csv.reader(csvFile, delimiter=',') reader.next() for row in reader: sku = row[0] stockRecord.partner_sku = sku product.upc = sku product.description = row[5] + '\n' + row[6] product.save() pc = ProductCategory.objects.create(product=product, category=product_category) pc.save()
def test_backorder_purchase_is_permitted(self): record = StockRecord(num_in_stock=None, product=self.product) with patch.object(self.wrapper, 'max_purchase_quantity') as m: m.return_value = None result, reason = self.wrapper.is_purchase_permitted(record) self.assertTrue(result)
def test_availability_message_for_out_of_stock(self): record = StockRecord(num_in_stock=0, product=self.product) self.assertEqual(u'Not available', unicode(self.wrapper.availability(record)))
def test_availability_message_for_in_stock(self): record = StockRecord(num_in_stock=4, product=self.product) self.assertEqual(u'In stock (4 available)', unicode(self.wrapper.availability(record)))
def test_availability_code_for_null_stock_but_available(self): record = StockRecord(num_in_stock=None, product=self.product) self.assertEqual('available', self.wrapper.availability_code(record))
def test_availability_code_for_zero_stock(self): record = StockRecord(num_in_stock=0, product=self.product) self.assertEqual('outofstock', self.wrapper.availability_code(record))
def test_max_purchase_quantity(self): record = StockRecord(num_in_stock=4, product=self.product) self.assertEqual(record.num_in_stock, self.wrapper.max_purchase_quantity(record))
def test_smaller_purchase_is_permitted(self): record = StockRecord(num_in_stock=4, product=self.product) result, reason = self.wrapper.is_purchase_permitted(record, quantity=3) self.assertTrue(result)
def test_nonzero_stock_is_available_to_buy(self): record = StockRecord(num_in_stock=10) self.assertTrue(self.wrapper.is_available_to_buy(record))
def test_zero_stock_is_not_available_to_buy(self): record = StockRecord(num_in_stock=0) self.assertFalse(self.wrapper.is_available_to_buy(record))
def setUp(self): self.wrapper = DefaultWrapper() self.product = Product() self.product.product_class = ProductClass() self.record = StockRecord(num_in_stock=0, product=self.product)
p = Product.objects.create(structure=structure,upc=upc,title=title,slug=slug, description=description,product_class=product_class) c = Category.objects.all()[1] pc = ProductCategory.objects.create(product=p,category=c) from oscar.apps.catalogue.models import Category c = Category.objects.all()[1] # for food len(Product.objects.filter(categories=c)) # generate sku from oscar.apps.partner.models import StockRecord sr = StockRecord() In [19]: sr.product = p In [20]: p Out[20]: <Product: test> In [21]: sr.partner = partner In [22]: partner Out[22]: <Partner: Petzo India Pvt. Ltd.> In [23]: sr.partner_sku = 'fd4546' In [24]: sr.price_currency = 'INR' In [25]: sr.price_retail = 450
def test_dispatch_date_for_in_stock(self): tomorrow = datetime.date.today() + datetime.timedelta(days=1) record = StockRecord(product=self.product, num_in_stock=4) self.assertEquals(tomorrow, self.wrapper.dispatch_date(record))
def test_too_large_purchase_is_not_permitted(self): record = StockRecord(num_in_stock=4, product=self.product) result, reason = self.wrapper.is_purchase_permitted(record, quantity=5) self.assertFalse(result)
def test_dispatch_date_for_out_of_stock(self): date = datetime.date.today() + datetime.timedelta(days=7) record = StockRecord(product=self.product) self.assertEquals(date, self.wrapper.dispatch_date(record))
def createProduct(request): title = "Petzo Pet Fodd" slug = "Petzo-pet-food" # add request part here for plan plan = 1 price = 450 # code which manipulates description acc to plan description = 'nutrients and all' product_class = ProductClass.objects.get(name='Food') product_category = Category.objects.get(name='Food') length = len(Product.objects.filter(categories=c)) partner = Partner.objects.get(name='Petzo India Pvt Ltd') product_upc = "FD0000" + length + 1 structure = Product.STANDALONE p = Product.objects.create(structure=structure,upc=upc,title=title,slug=slug, description=description,product_class=product_class) p.save() pc = ProductCategory.objects.create(product=p,category=product_category) pc.save() sr = StockRecord() sr.product = p sr.partner = partner sr.partner_sku = product_upc sr.price_currency = 'INR' sr.price_retail = price sr.price_excl_tax = price sr.num_in_stock = 1 sr.save()