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_unicode_slug(self): root_category = Category.add_root(name="Vins français") child_category = root_category.add_child(name="Château d'Yquem") self.assertEqual(root_category.slug, 'vins-français') self.assertEqual( root_category.get_absolute_url(), '/catalogue/category/vins-fran%C3%A7ais_{}/'.format(root_category.pk) ) self.assertEqual(child_category.slug, 'château-dyquem') self.assertEqual( child_category.get_absolute_url(), '/catalogue/category/vins-fran%C3%A7ais/ch%C3%A2teau-dyquem_{}/'.format(child_category.pk) )
def test_unicode_slug(self): root_category = Category.add_root(name="Vins français") child_category = root_category.add_child(name="Château d'Yquem") self.assertEqual(root_category.slug, 'vins-français') self.assertEqual( root_category.get_absolute_url(), '/catalogue/category/vins-fran%C3%A7ais_{}/'.format( root_category.pk)) self.assertEqual(child_category.slug, 'château-dyquem') self.assertEqual( child_category.get_absolute_url(), '/catalogue/category/vins-fran%C3%A7ais/ch%C3%A2teau-dyquem_{}/'. format(child_category.pk))
def setUp(self): self.category = Category.add_root(name="Products")
def test_enforces_slug_uniqueness(self): root = Category.add_root(name="Products") root.add_child(name="Books") with self.assertRaises(ValidationError): root.add_child(name="Books")
def test_supports_has_children_method(self): """Supports has_children method""" root = Category.add_root(name="Products") self.assertFalse(root.has_children()) root.add_child(name="Books") self.assertTrue(root.has_children())
def test_is_public_on(self): category = Category.add_root(name="Barfoos", is_public=True) response = self.app.get(category.get_absolute_url()) self.assertEqual(http_client.OK, response.status_code) return category
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_unicode_slug(self): with override_settings(OSCAR_SLUG_ALLOW_UNICODE=True): root_category = Category.add_root(name=u"Vins français") child_category = root_category.add_child(name=u"Château d'Yquem") self.assertEqual(root_category.slug, u'vins-français') self.assertEqual(child_category.slug, u'château-dyquem')
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_doesnt_fail_for_empty_image_field(self): category = Category.add_root(name='Test Category') from oscar.apps.catalogue.receivers import delete_image_files delete_image_files(Category, category)
def setUp(self): self.products = Category.add_root(name=u"Pröducts") self.books = self.products.add_child(name=u"Bücher")
def test_is_public_off(self): category = Category.add_root(name="Foobars", is_public=False) response = self.app.get(category.get_absolute_url(), expect_errors=True) self.assertEqual(http_client.NOT_FOUND, response.status_code) return category
def test_duplicate_slugs_allowed_for_non_siblings(self): more_books = Category.add_root(name=self.books.name) self.assertEqual(more_books.slug, self.books.slug)