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 handle(self, *args, **options): if Category.objects.exists() or Category.objects.exists(): print('Product already loaded...existing.') print(ALREADY_LOADED_ERROR_MESSAGE) return print('Creating category data') for name in CATEGORY_IMAGE_NAMES.keys(): category = Category(categoryName=name) category.categoryImgPath = CATEGORY_IMAGE_NAMES[name] category.save() print('Loading product data from csv file') for line in DictReader(open('./product_data.csv')): product = Product() product.productName = line['productName'] product.productPrice = line['productPrice'] product.productDescription = line['productDescription'] product.productImgPath = line['productImgPath'] product.category = Category.objects.get( categoryName=line['productCategory']) product.save()
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''')
def create_category(self, id, parent, name): self.stdout.write('Creating category: id=%s, parent_id=%s, name=%s' % (id, parent, name)) parent_id = None if parent == "0" else int(parent) category = Category(id=int(id), parent_id=parent_id, name=name) category.save()
def save_category(category): data = category.cleaned_data name = data['name'] category_to_save = Category(name=name) category_to_save.save()
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')