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 post(self, request): form1 = MultiImages(request.POST, request.FILES) form = ProductForm(request.POST) if (form.is_valid() and form1.is_valid()): category = request.POST.get("category") brand = request.POST.get("brand") name = request.POST.get("name") price = request.POST.get("price") stock = request.POST.get("stock") form = Product(category=category, brand=brand, name=name, price=price, stock=stock) form.save() for file in request.FILES.getlist("image"): form1 = MultiImage(product_id=form, image=file) form1.save() return redirect('main:home_view') else: print(form.errors) return HttpResponse('not valid')
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)
def new_product(): form = ProductForm() if form.validate_on_submit(): product = Product(product_name=form.product_name.data) db.session.add(product) db.session.commit() flash('Your product is successfully added in the product list!', 'success') return redirect(url_for('main.product')) return render_template('create.html', form=form, legend='New Product')
def add_product(): form = ProductForm() if request.method == "POST" and form.validate(): product_id = Product(name=form.name.data) db.session.add(product_id) db.session.commit() flash('Entered Product ID added!', 'success') return redirect(url_for('products')) return render_template('add_product.html', form=form)
def set_global_context(self): # Get the valid prices self.products = Product.get_all_products() self.pricelist = self.campaign.get_product_prices() # Translate to json. self.catalog_json = dumps({ 'products': {code: p.as_jsonable() for code, p in self.products.iteritems()}, 'prices': {code: pp.as_jsonable() for code, pp in self.pricelist.iteritems()} }) #self.products_json = dumps({code: p.as_jsonable() for code, p in self.products.iteritems()}) self.pricelist_json = dumps({code: pp.as_jsonable() for code, pp in self.pricelist.iteritems()})
def process_product(form, request, manufacturer): pspec_file = request.files.getlist('pspec')[0] pimage_file = request.files.getlist('pimage')[0] pspec_path = pspec.save(pspec_file) pimage_path = pimage.save(pimage_file) product = Product( name=form.product.data, manufacturer_id=manufacturer.id, total_samples=form.total_samples.data, sample_counter=0, spec_file_name=pspec_path, spec_image_name=pimage_path, ) db.session.add(product) db.session.flush() return product
def readExcelProducts(): loc = ("simple_inv.xlsx") wb = xlrd.open_workbook(loc) sheet = wb.sheet_by_index(0) values_to_insert = [None for _ in range(10)] for i in range(sheet.nrows): for j in range(sheet.ncols): if j == 7 : values_to_insert[j-1] += sheet.cell_value(i, j) else: values_to_insert[j] = sheet.cell_value(i, j) p = Product.create(values_to_insert[0],values_to_insert[1],values_to_insert[2],values_to_insert[3], values_to_insert[4],values_to_insert[5],values_to_insert[6],values_to_insert[8],values_to_insert[9]) p.save()
def __init__(self, agreement, blob): self.agreement = agreement self.errors = [] self.messages = [] self.restrictions = [] self.blob = blob self.products = Product.get_all_products() self.prices = agreement.campaign.get_product_prices(agreement.pricedate) self.product_contents = agreement.campaign.get_product_contents(agreement.pricedate) #self.product_contents = defaultdict(list) #for pc in pcs: # self.product_contents[pc.included_in_id].append(pc) self.apply_restrictions() self.available_install_methods = agreement.available_install_methods() self.final_children = []
def product(): form = addproduct() eform = editproduct() details = Product.query.order_by(Product.prod_name).all() exists = bool(Product.query.order_by(Product.prod_name).all()) if exists == False and request.method == 'GET': flash(f'Add products to view', 'info') elif eform.validate_on_submit() and request.method == 'POST': pname = request.form.get("productname", "") details = Product.query.order_by(Product.prod_name).all() prod = Product.query.filter_by(prod_name=pname).first() prod.prod_name = eform.editname.data prod.prod_qty = eform.editqty.data prod.prod_price = eform.editprice.data try: db.session.commit() flash(f'Your product has been updated!', 'success') return redirect('/Product') except IntegrityError: db.session.rollback() flash(f'This product already exists', 'danger') return redirect('/Product') return render_template('product.html', details=details, eform=eform) elif form.validate_on_submit(): product = Product(prod_name=form.prodname.data, prod_qty=form.prodqty.data, prod_price=form.prodprice.data) db.session.add(product) try: db.session.commit() flash(f'Your product {form.prodname.data} has been added!', 'success') return redirect(url_for('product')) except IntegrityError: db.session.rollback() flash(f'This product already exists', 'danger') return redirect('/Product') return render_template('product.html', eform=eform, form=form, details=details)
def __init__(self, agreement, blob): self.agreement = agreement self.errors = [] self.messages = [] self.restrictions = [] self.blob = blob self.products = Product.get_all_products() self.prices = agreement.campaign.get_product_prices( agreement.pricedate) self.product_contents = agreement.campaign.get_product_contents( agreement.pricedate) #self.product_contents = defaultdict(list) #for pc in pcs: # self.product_contents[pc.included_in_id].append(pc) self.apply_restrictions() self.available_install_methods = agreement.available_install_methods() self.final_children = []
def set_global_context(self): # Get the valid prices self.products = Product.get_all_products() self.pricelist = self.campaign.get_product_prices() # Translate to json. self.catalog_json = dumps({ 'products': {code: p.as_jsonable() for code, p in self.products.iteritems()}, 'prices': { code: pp.as_jsonable() for code, pp in self.pricelist.iteritems() } }) #self.products_json = dumps({code: p.as_jsonable() for code, p in self.products.iteritems()}) self.pricelist_json = dumps({ code: pp.as_jsonable() for code, pp in self.pricelist.iteritems() })
from django.test import TestCase from django.urls import reverse from users.models import CustomUser as User from inventory.models import Partner, Product, Transaction from inventory.views import transact_api # Create your tests here. # custom entries into the database product_insert = [ Product( name=prod_name_list[i], description=prod_des[i], code=code_list[i], quantity=qty_list[i], cost=cost_list[i], price=price_list[i], responsible=User.objects.get(pk=1), ) for i in range(len(prod_name_list[i])) ] Product.objects.bulk_create(product_insert) class TransactionViewTestCase(TestCase): partner = 1 prod_id_list = [2, 1, 3] trade_list_qty = [11, 2, 4] unit_price_list = [100, 120, 310] def test_perfect_receiving(self):
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()