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 handle(self, **options): """ Check all products with active product alerts for availability and send out email alerts when a product is available to buy. """ Product.objects.all().delete() Category.objects.all().delete() for page in PRODUCTS: page = page.get('page') category = unidecode(page.get('category')).strip() category_slug = slugify(category) try: cat = Category.objects.get(slug=category_slug, depth=1) except Category.DoesNotExist: cat = Category(name=category, depth=1) cat.path = category_slug cat.save() except Exception as e: import pdb;pdb.set_trace() product_type = unidecode(page.get('sub_category')).strip() product_type_slug = slugify(product_type) pt, pt_is_new = ProductClass.objects.get_or_create(name=product_type) product = page.get('product')[0] pre_title = product.get('title').strip() match = re.match(r'^Art-Nr\: (\d+) \- (.*)$', pre_title) if match: sku, title = match.groups() else: sku, title = None, pre_title title = unidecode(title) slug = slugify(title) description = '\n'.join([unidecode(i.get('text').strip()) for i in product.get('description', [])]).strip() print product_type, sku, slug, title, description p = Product(slug=slug, title=title, product_class=pt, # upc=sku, description=description) p.save() p.productcategory_set.add(ProductCategory(category=cat, product=p)) p_attributes = [re.sub(r'[\:\.]', '', unidecode(i.get('text'))) for i in product.get('attr_headers', [])] p_codes = [slugify(re.sub(r'[\:\.]', '', unidecode(i.get('text')))) for i in product.get('attr_headers', [])] p_values = [unidecode(i.get('text')) for i in product.get('attr_values', [])] for name, code, value in zip(p_attributes, p_codes, p_values): pa, is_new = ProductAttribute.objects.get_or_create(name=name, code=code) pav, pav_is_new = ProductAttributeValue.objects.get_or_create(value_text=value, attribute=pa, product=p) pa.productattributevalue_set.add(pav)
def create_update_product(self, data): field_values = data[0:len(self.FIELDS)] values = [item.value for item in field_values] values = dict(zip(self.FIELDS, values)) try: product = Product.objects.get(id=values[self.ID]) self.statistics['updated'] += 1 except Product.DoesNotExist: product = Product() p_class = ProductClass.objects.get(name=values[self.PRODUCT_CLASS]) product.product_class = p_class self.statistics['created'] += 1 categories = Category.objects.filter( id__in=self._get_categories(values[self.CATEGORY])) ProductCategory.objects.filter(product=product).delete() for category in categories: product_category = ProductCategory() product_category.product = product product_category.category = category product_category.save() product.title = values[self.TITLE] product.description = values[self.DESCRIPTION] product.upc = values[self.UPC] product.save() self.save_product_attributes(product, data) return product
def setUp(self): super(TestConvertingAFulfillmentLineToItemDict, self).setUp() self.line = mwsm.FulfillmentOrderLine(order_item_id='123', quantity=5) self.line.line = Line() self.line.line.product = Product() self.line.line.product.amazon_profile = mwsm.AmazonProfile( sku='MY-SKU')
def test_none_upc_is_represented_as_empty_string(self): product = Product(product_class=self.product_class, title='testing', upc=None) product.save() product.refresh_from_db() self.assertEqual(product.upc, '')
def get_mocks(): product = Product(title='utTestProduct_1', slug='ut-Test-Product-1') image_1 = ProductImage( original='test/test_img.png', caption='test image 1', product=product, ) settings = Mock() settings.SRCSETS = { 'fullsizes': { 'fullscreen': 1080, 'tablet': 780, 'mobile_large': 520, 'moble_small': 280, }, 'thumbnails': { 'large': 100, 'small': 50, } } return product, image_1, settings
def update_product(variants): product_class = ProductClass.objects.get_or_create( name='moy_sklad_child')[0] sync_ids = list(map(lambda x: x.product_sync_id, variants)) product_syncs = list(ProductSync.objects.filter(sync_id__in=sync_ids)) delete_price(product_syncs) product_sync_map_by_sync = { product_sync.sync_id: product_sync for product_sync in product_syncs } parents = set() for variant in variants: product_sync = product_sync_map_by_sync.get(variant.product_sync_id) parent_product = product_sync.product if parent_product: product = variant.product if variant.product else Product() product.is_discountable = True # product.product_class = product_class product.product_class = None product.title = product_sync.name product.parent = parent_product product.structure = Product.CHILD product.save() obj, created = ProductInformation.objects.update_or_create( product=product, defaults={'hide': variant.archived}) variant.product = product parent_product.structure = Product.PARENT parents.add(parent_product) VariantSync.objects.bulk_update(variants) bulk_update(parents) update_product_attr_from_sync_obj(variants, lambda x: x.get_characteristics(), product_class) update_price_product_from_sync_obj(variants)
def test_create_products_with_attributes(self): product = Product(upc='1234', product_class=self.product_class, title='testing') product.attr.num_pages = 100 product.save()
def test_top_level_products_must_have_product_class(self): product = Product(title=u"Kopfhörer") self.assertRaises(ValidationError, product.clean)
def test_top_level_products_must_have_titles(self): product = Product(product_class=self.product_class) self.assertRaises(ValidationError, product.clean)
def setUp(self): self.wrapper = DefaultWrapper() self.product = Product() self.product.product_class = ProductClass() self.record = StockRecord(num_in_stock=0, product=self.product)
def update_product(product_syncs): if not os.path.exists(DIR_PATH): os.makedirs(DIR_PATH) product_class = ProductClass.objects.get_or_create(name='moy_sklad')[0] sync_ids = list(map(lambda x: x.folder_sync_id, product_syncs)) pf_syncs = list(ProductFolderSync.objects.filter(sync_id__in=sync_ids)) pf_sync_map_by_sync = {pf_sync.sync_id: pf_sync for pf_sync in pf_syncs} for product_sync in product_syncs: pf_sync = pf_sync_map_by_sync.get(product_sync.folder_sync_id) product = product_sync.product if product_sync.product else Product() product.is_discountable = True product.product_class = product_class product.title = product_sync.name product.description = product_sync.description if product_sync.description is not None else '' if Product.objects.exclude(id=product.id).filter( upc=product_sync.article).exists(): Product.objects.exclude(id=product.id).filter( upc=product_sync.article).update(upc=None) product.upc = product_sync.article product.save() product.children.update(title=product_sync.name) obj, created = ProductInformation.objects.update_or_create( product=product, defaults={'hide': product_sync.archived}) attributes = json.loads(product_sync.attributes) care_description = attributes.get('Уход', '') information, _ = ProductInformation.objects.get_or_create( product=product) information.care_description = care_description information.save() if product_sync.image_url is not None: img_name = product_sync.image_url.replace( 'https://online.moysklad.ru/api/remap/1.1/download/', '') img_path = os.path.join( settings.MEDIA_ROOT, settings.MOYSKLAD_MEDIA_URL + '/' + img_name + '.jpg') origin = settings.MOYSKLAD_MEDIA_URL + '/' + img_name + '.jpg' image = ProductImage.objects.filter(original=origin, product=product).first() if not image: p = requests.get(product_sync.image_url, headers={ 'Authorization': 'Basic ' + settings.MOYSKLAD_TOKEN }) out = open(img_path, "wb") out.write(p.content) out.close() ProductImage.objects.filter(product=product).delete() image = ProductImage(product=product, original=origin) image.save() for category in list(product.categories.all()): if category != pf_sync.category: ProductCategory.objects.filter(category=category, product=product).delete() ProductCategory.objects.get_or_create(category=pf_sync.category, product=product) product_sync.product = product ProductSync.objects.bulk_update(product_syncs) update_product_attr_from_sync_obj(product_syncs, lambda x: x.get_attributes(), product_class) update_price_product_from_sync_obj(product_syncs)
def dispatch(self, *args, **kwargs): partner = Partner.objects.filter(name='opt.my') if partner.exists(): partner = partner[0] else: partner = Partner() partner.name = 'opt.my' partner.save() url = "http://path/?format=json" response = urllib.urlopen(url) data = json.loads(response.read()) # set category for item in data: if item['parent'] != "null": category = Category( id=item['id'], path=str(item['id']), name=item['name'], depth=1) category.save() url = "http://path/?format=json" response = urllib.urlopen(url) data = json.loads(response.read()) # set class class_attr = ProductClass(id=1, name="product", slug="product") class_attr.save() # set products for item in data: product_category = ProductCategory(product_id=item['id'], category_id=item['category_id']) product_category.save() product = Product(id=item['id'], upc=item['id'], title=item['name'], slug=str(item['id']), product_class_id=1, date_created=datetime.datetime.today(), description=item['description']) product.save() # set attribute attribute_weight = ProductAttribute.objects.filter(name='height') if attribute_weight.exists(): attribute_weight = attribute_weight[0] else: attribute_weight = ProductAttribute() attribute_weight.name = 'height' attribute_weight.save() weight_attr = ProductAttributeValue() weight_attr.product = product weight_attr.attribute = attribute_weight weight_attr.value_text = str(item['height']) weight_attr.save() attribute_depth = ProductAttribute.objects.filter(name='depth') if attribute_depth.exists(): attribute_depth = attribute_depth[0] else: attribute_depth = ProductAttribute() attribute_depth.name = 'depth' attribute_depth.save() depth_attr = ProductAttributeValue() depth_attr.product = product depth_attr.attribute = attribute_depth depth_attr.value_text = str(item['depth']) depth_attr.save() attribute_length = ProductAttribute.objects.filter(name='length') if attribute_length.exists(): attribute_length = attribute_length[0] else: attribute_length = ProductAttribute() attribute_length.name = 'length' attribute_length.save() length_attr = ProductAttributeValue() length_attr.product = product length_attr.attribute = attribute_length length_attr.value_text = str(item['length']) length_attr.save() # set image try: img_url = 'http://path/media/{0}'.format( item['main_picture']) name = urlparse(img_url).path.split('/')[-1] image = ProductImage() image.product = product image.original = '' image.save() img_temp = NamedTemporaryFile(delete=True) img_temp.write(urllib2.urlopen(img_url).read()) img_temp.flush() image.original.save(name, File(img_temp)) except Exception, e: pass # set the price rec = StockRecord.objects.filter(product=product) if rec.exists(): rec = rec[0] else: rec = StockRecord() rec.product = product rec.partner = partner rec.num_in_stock = 10 rec.partner_sku = product.upc rec.product = product rec.partner = partner rec.num_in_stock = 10 rec.partner_sku = product.upc rec.price_excl_tax = Decimal(str(item['dollar'])) rec.save()
def test_none_upc_is_represented_as_empty_string(self): product = Product(product_class=self.product_class, title='testing', upc=None) self.assertEqual(product.upc, u'')
def dispatch(self, *args, **kwargs): partner = Partner.objects.filter(name='opt.my') if partner.exists(): partner = partner[0] else: partner = Partner() partner.name = 'opt.my' partner.save() url = "http://path/?format=json" response = urllib.urlopen(url) data = json.loads(response.read()) # set category for item in data: if item['parent'] != "null": category = Category(id=item['id'], path=str(item['id']), name=item['name'], depth=1) category.save() url = "http://path/?format=json" response = urllib.urlopen(url) data = json.loads(response.read()) # set class class_attr = ProductClass(id=1, name="product", slug="product") class_attr.save() # set products for item in data: product_category = ProductCategory(product_id=item['id'], category_id=item['category_id']) product_category.save() product = Product(id=item['id'], upc=item['id'], title=item['name'], slug=str(item['id']), product_class_id=1, date_created=datetime.datetime.today(), description=item['description']) product.save() # set attribute attribute_weight = ProductAttribute.objects.filter(name='height') if attribute_weight.exists(): attribute_weight = attribute_weight[0] else: attribute_weight = ProductAttribute() attribute_weight.name = 'height' attribute_weight.save() weight_attr = ProductAttributeValue() weight_attr.product = product weight_attr.attribute = attribute_weight weight_attr.value_text = str(item['height']) weight_attr.save() attribute_depth = ProductAttribute.objects.filter(name='depth') if attribute_depth.exists(): attribute_depth = attribute_depth[0] else: attribute_depth = ProductAttribute() attribute_depth.name = 'depth' attribute_depth.save() depth_attr = ProductAttributeValue() depth_attr.product = product depth_attr.attribute = attribute_depth depth_attr.value_text = str(item['depth']) depth_attr.save() attribute_length = ProductAttribute.objects.filter(name='length') if attribute_length.exists(): attribute_length = attribute_length[0] else: attribute_length = ProductAttribute() attribute_length.name = 'length' attribute_length.save() length_attr = ProductAttributeValue() length_attr.product = product length_attr.attribute = attribute_length length_attr.value_text = str(item['length']) length_attr.save() # set image try: img_url = 'http://path/media/{0}'.format(item['main_picture']) name = urlparse(img_url).path.split('/')[-1] image = ProductImage() image.product = product image.original = '' image.save() img_temp = NamedTemporaryFile(delete=True) img_temp.write(urllib2.urlopen(img_url).read()) img_temp.flush() image.original.save(name, File(img_temp)) except Exception, e: pass # set the price rec = StockRecord.objects.filter(product=product) if rec.exists(): rec = rec[0] else: rec = StockRecord() rec.product = product rec.partner = partner rec.num_in_stock = 10 rec.partner_sku = product.upc rec.product = product rec.partner = partner rec.num_in_stock = 10 rec.partner_sku = product.upc rec.price_excl_tax = Decimal(str(item['dollar'])) rec.save()