def get_or_create_product_class(): #Product class products = ProductClass.objects.all() if len(products) > 0: product_class = products[0] else: pro_cls_name = u'Default' pro_cls_slug = slugify(pro_cls_name) product_class = ProductClass(name=pro_cls_name, slug=pro_cls_slug) product_class.save() return product_class
def save_product(request): product_form = ProductForm(request.POST or None) if product_form.is_valid(): #Get information for Product title = request.POST['title'] slug = slugify(title) description = request.POST['description'] image_url_from_web = urllib.unquote( request.POST['image']).decode('utf8') is_trending = int(request.POST['product_is_trending']) #Product class products = ProductClass.objects.all() if len(products) > 0: product_class = products[0] else: pro_cls_name = u'Default' pro_cls_slug = slugify(pro_cls_name) product_class = ProductClass(name=pro_cls_name, slug=pro_cls_slug) product_class.save() #Get image info io_file = cStringIO.StringIO(urllib.urlopen(image_url_from_web).read()) img_file = Image.open(io_file) image_extension = '.' + string.lower(img_file.format) image_name = hashlib.md5( b'%s' % time.time()).hexdigest() + image_extension #built image path to save host url_product_image = datetime.now().strftime( OSCAR_IMAGE_FOLDER) + image_name #check file exists on host dir, need recheck MEDIA_ROOT was configured in settings file path_product_image = MEDIA_ROOT + '/' + url_product_image exists = os.path.exists(path_product_image) if (exists): url_product_image = datetime.now().strftime( OSCAR_IMAGE_FOLDER) + hashlib.md5( b'%s' % time.time()).hexdigest() + image_extension #save file img_saved = download_photo(image_url_from_web, url_product_image) #Save product object product = Product(title=title, slug=slug, description=description, product_class=product_class, user_id=request.user.pk, is_trending=is_trending) product.save() #Check image saved to save Product Image object if img_saved: product_image = ProductImage(product=product, original=url_product_image, caption=title, display_order=0) product_image.save() #built product link to return front end pro_link = '/catalogue/%s_%d/' % (slug, product.id) return { 'code': 1, 'pro_link': u'<a href="%s" class="f-button close" target="_blank">%s</a>' % (pro_link, u'See the product'), 'back_text': u'Add more Products', 'mes_box': u'<h1>Image has been saved!</h1>', }
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()