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