def add_category(request): if request.method == 'POST': form = categoryForm(request.POST or None) category = Category() if form.is_valid(): category.name = form.cleaned_data.get('name') category.author = request.user category.save() return redirect('Product_category') form = categoryForm() context = {'form': form} return render(request, 'staff/add-category.html', context)
def test_fetch_products_data_api(self, mock_get): ''' Test the recovery of products data from api. ''' category = Category() category.name = 'Category test name' category.save() mock_response = mock.Mock() # Mock products data in api expected_api_products = { 'products': [{ 'product_name': 'aliment', 'ingredients_test_fr': 'ingredient', 'url': "https://url_prod", 'image_url': 'https://image_url_product', }] } mock_response.json.return_value = expected_api_products mock_get.return_value = mock_response response = OpenFoodFactsApi().fetch_products_data_api(category) self.assertEqual(response, expected_api_products['products']) # Making sure the right url was called mock_get.assert_called_once_with( f'''https://fr.openfoodfacts.org/categorie/{category}.json''')
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)
def add(request): if request.session.has_key('seller'): id = request.session['seller'] if request.method == 'POST': MyLoginForm = ProductEntryForm(request.POST) print(MyLoginForm.errors) if MyLoginForm.is_valid(): name = MyLoginForm.cleaned_data['name'] company = MyLoginForm.cleaned_data['company'] price = MyLoginForm.cleaned_data['price'] availability = MyLoginForm.cleaned_data['availability'] cat = MyLoginForm.cleaned_data['category'] other = MyLoginForm.cleaned_data['other'] dbuser = Sellers.objects.filter(id=id) if cat == "Other": categ = Category() categ.name = other categ.save() if not cat == "Other": category = Category.objects.filter(name=cat) else: category = Category.objects.filter(name=other) if Products.objects.filter(name=name, company=company, seller=dbuser): x = Products.objects.filter(name=name, company=company)[0] if Products.objects.filter(name=name, company=company, category=category, seller=dbuser[0]): x = Products.objects.filter(name=name, company=company, category=category)[0] x.price = price x.availability = availability x.save(update_fields=['price', 'availability']) else: product = Products() product.productID = "GBDP{0}{1}".format( id, Products.objects.count()) product.name = name product.company = company product.price = price product.availability = availability product.category = category[0] product.seller = dbuser[0] product.save() categories = Category.objects.all() products = Products.objects.filter(seller=dbuser[0]) context = { 'dbuser': dbuser, 'products': products, 'categories': categories, } return render(request, 'seller/dashboard.html', context) else: dbuser = Sellers.objects.filter(id=id) mysearch = ProductSearchForm(request.GET) if mysearch.is_valid(): search = mysearch.cleaned_data['searchfield'] productlist = list( Products.objects.filter(name__icontains=search, seller=id)) productlist = list( Products.objects.filter(name__icontains=search, seller=id)) productlist.extend( list( Products.objects.filter(company__icontains=search, seller=id))) categories = Category.objects.all() context = { 'dbuser': dbuser, 'products': productlist, 'categories': categories, } return render(request, 'seller/dashboard.html', context) else: products = Products.objects.filter(seller=dbuser[0]) categories = Category.objects.all() context = { 'dbuser': dbuser, 'products': products, 'categories': categories, } return render(request, 'seller/dashboard.html', context) else: return render(request, 'seller/index.html')