def mutate(self, info, productName, productPrice, productInventory): errorMsg = [] #fail if any field is invalid if productInventory < 0: errorMsg.append( "Unable to set productInventory to value less than zero") if len(productName) == 0: errorMsg.append("Unable to set name to empty-string") if productPrice < 0: errorMsg.append("Unable to change price to negative value") if (abs(productPrice) * 100) - int(abs(productPrice) * 100) > 0: errorMsg.append("Price cannot have more than two decimal places") if len(errorMsg) == 0: errorMsg = None product = Product(productName=productName, productPrice=productPrice, productInventory=productInventory) product.save() return CreateProduct(productId=product.productId, productName=product.productName, productPrice=product.productPrice, productInventory=product.productInventory, errors=errorMsg) else: #product creation fails return CreateProduct(productId=None, productName=None, productPrice=None, productInventory=None, errors=errorMsg)
def create(self, validated_data): ingredient_used = validated_data.pop('ingredient_used') product = Product(**validated_data) product.save() ingredient_used_objs = [ IngredientUsed( ingredient=item['ingredient'], quantity_used=item['quantity_used'], product=product, ) for item in ingredient_used ] IngredientUsed.objects.bulk_create(ingredient_used_objs, ignore_conflicts=True) return product
def newproductadd(request): """ new product addition into database """ if request.method == "POST": products = cleaner(request.POST.getlist('product')) price = cleaner(request.POST.getlist('price')) for i in range(len(products)): p = Product(p_name=products[i], p_price=price[i], stocks=0) p.save() else: return HttpResponse("failed") return render(request, 'addconfirmation.html')
def add_product(request, barcode_id): # ipdb.set_trace() name = request.POST['product_name'] description = request.POST['description'] notes = request.POST['notes'] p = Product( barcode_id=barcode_id, name=name, description=description, notes=notes, ) p.save() return redirect('view_product', barcode_id=barcode_id)
from inventory.models import Item, Product import random number = 10 for i in range(int(number)): p = Product() p.barcode_id = i * 100 p.name = 'Product #{i}00'.format(i=i) p.description = 'foo' p.notes = 'foooooo' p.save() for j in range(int(number)): item = Item() item.product = p item.cost = random.randrange(2, 100) item.save()