def test_product(self):
        product = Product()
        product.name = "A solution book"
        product.price = 200
        product.digital = True
        product.save()

        record = Product.objects.get(pk=1)
        self.assertEqual(record, product)
示例#2
0
def edit_product(request):
    if request.method == 'POST' and request.FILES['image']:
        image = request.FILES['image']

        title = request.POST['title']
        price = request.POST['price']

        description = request.POST['description']
        obj = Product()
        obj.title = title
        obj.description = description
        obj.price = price
        obj.image = image
        obj.save()
        return redirect('/custom-admin')
    return render(request, template_name='store/add_product.html')
示例#3
0
def add_product(request):
    if request.method == 'POST':
        form = ProductForm(request.POST, request.FILES)
        product = Product()
        if form.is_valid():
            # product.sub_category = 'ENGINE OIL DIESEL'
            product.name = form.cleaned_data['name']
            product.price = form.cleaned_data['price']
            product.image = form.cleaned_data['image']
            product.author = request.user
            product.save()
            print(request)
            return redirect('products_list')
        else:
            messages.error(request, 'Post error')
            return redirect('add_product')
    form = ProductForm()
    context = {'form': form}
    return render(request, 'staff/product-add.html', context)
示例#4
0
    def write_product(self, url, category):
        page = html.parse(url)
        name = unicode(page.xpath('.//div[@class="product_title"]/h1')[0].text_content())
        print 'Writing product : ' + name

        try:
            product = Product.objects.get(categories=category, name=name, store=self.store)
        except Product.DoesNotExist:
            product = Product(name=name, store=self.store)

        # try:
        #     product_info = ProductInfo.objects.get(product=product, store=self.store)
        # except ProductInfo.DoesNotExist:
        #     product_info = ProductInfo(product=product, store=self.store)

        price_els = page.xpath('.//div[@class="product-info"]//div[@class="price"]')
        if len(price_els):
            price_el = price_els[0]
        else:
            return
        old_price_els = price_el.xpath('./span[@class="price-old"]')
        if len(old_price_els):
            old_price_el = old_price_els[0]
            old_price_content = old_price_el.text_content()
            old_price = float(re.search('Rs\.([\d,\.]+)', old_price_content).group(1).replace(',', ''))
            product.original_price = old_price
            new_price_els = price_el.xpath('./span[@class="price-new"]')
            new_price_el = new_price_els[0]
            new_price_content = new_price_el.text_content()
            new_price = float(re.search('Rs\.([\d,\.]+)', new_price_content).group(1).replace(',', ''))
            product.price = new_price
        else:
            price_with_comma = re.search('Price:\s+Rs\.([\d,\.]+)', price_el.text_content()).group(1)
            product.price = float(price_with_comma.replace(',', ''))

        product.currency = self.currency
        description_text = page.xpath('.//div[@class="product-info"]//div[@class="description"]')[0].text_content()
        product.code = re.search('Product Code:\s(.+)\\n', description_text).group(1)
        availability_text = re.search('Availability:\s(.+)', description_text).group(1)

        # TODO: get vendor
        if availability_text == 'In Stock':
            product.availability = 0
        description_el = page.xpath('//div[@id="tab-description"]')[0]
        product.description = tostring(description_el)
        product.purchase_url = url
        product.save()
        product.categories.add(category)
        # TODO: write only if images are new
        # delete existing images first
        images = product.images.all()
        # product.images.clear()
        for image in images:
            image.delete()

        primary_image_url = page.xpath('//div[@class="product-info"]//div[@class="image"]/a')[0].get('href')
        image = Image(image_url=primary_image_url)
        image.save()
        product.images.add(image)
        additional_image_links = page.xpath('//div[@class="image-additional"]/a')
        for image in additional_image_links:
            image_url = image.get('href')
            image = Image(image_url=image_url)
            image.save()
            product.images.add(image)
            # product.save()
from store.models import Category, Product
import random
import string

pics = ['cpu_amd.jpeg', 'cpu_intel.jpeg', 'GPU.jpeg', 'gpu_amd.jpg', 'gpu_nvidia.jpeg', 'index.jpeg', 'keyboard.jpg', 'keyboard_hp.jpg', 'keyboard_razor.png']

IsInShoppingCart = [True, False]
iteration = 0

for i in range(10000):
	for j in pics:
		a = Category()
		a.name = ''.join(random.choice(string.ascii_uppercase) for _ in range(6))
		a.category_logo = random.choice(pics)
		a.save()

		for k in range(10):
			b = Product()
			b.maker = ''.join(random.choice(string.ascii_uppercase) for _ in range(6))
			b.model = ''.join(random.choice(string.digits) for _ in range(6))
			b.description = 'descr' + str(i) + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(25))
			b.price = ''.join(random.choice(string.digits) for _ in range(3))
			b.category = a
			b.product_logo = random.choice(pics)
			b.is_in_shopCart = random.choice(IsInShoppingCart)
			b.save()
			iteration += 1
			print "iteration: {}".format(iteration)