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 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()
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)