def add_products(): connect_string = "mysql+pymysql://root:[email protected]/Tuckshop" sql_engine = sql.create_engine(connect_string) df = pd.read_sql_table('products', sql_engine) if request.method == 'POST': if len(df.loc[(df.product_name == request.form['product_name']) & (df.product_brand == request.form['brand'])]) == 0: new_product_name = request.form['product_name'] new_product_brand = request.form['brand'] new_product_quantity = request.form['quantity'] new_product_itemcost = request.form['itemcost'] new_product_price = request.form['price'] new_product = Products(product_name=new_product_name, product_brand=new_product_brand, quantity_in_stock=new_product_quantity, cost_per_item=new_product_itemcost, price=new_product_price) db.session.add(new_product) db.session.commit() return redirect(url_for('read_products')) else: return ( "<h4><br>" + "It looks like " + str(request.form['brand']) + " " + str(request.form['product_name']) + " already exists in the system." + "</h4>" + '<a href="/products/add" type="button">Try again?</a> </br>' + ('<br><br> <a href="/products/update2" type="button">Update products records</a> </br>' ) + ('<br> <a href="/products" type="button">Return to Products home</a> </br>' ) + ('<br> <br><a href="/" type="button">Return to Home</a> </br>') )
def setUp(self): db.create_all() customer = Customers(first_name="Testfirstname", last_name="Testlastname") product = Products(product_name="Pepsitest", product_quantity="1") purchase = Purchases(customer_id=1, product_id=1) db.session.add(customer) db.session.commit() db.session.add(product) db.session.commit() db.session.add(purchase) db.session.commit()
def test_addProducts(self): """ Test if the products are added to the database correctly """ #create a test product testProduct = Products() #set the attributes testProduct.productName = 'nameTEST' testProduct.productInfo = 'This is a test product' testProduct.productIMG = 'test.jpg' testProduct.productPrice = '10' #save it db.session.add(testProduct) #check if it's in the database all_products = Products.query.all() #self.assertEqual(len(all_products), 1) only_product = all_products[1] self.assertEqual(only_product, testProduct) #check attributes self.assertEqual(only_product.productName, 'nameTEST') self.assertEqual(only_product.productInfo, 'This is a test product') self.assertEqual(only_product.productIMG, 'test.jpg') self.assertEqual(only_product.productPrice, '10')
def store(): form = UpdateStoreForm() products = Products.query.all() if form.validate_on_submit(): product = Products(productname=form.productname.data, productdescription=form.productdescription.data, price=form.price.data, productvendor=form.productvendor.data) db.session.add(product) db.session.commit() return redirect(url_for('store')) else: print(form.errors) return render_template('store.html', title='store', form=form, products=products)
def setUp(self): """ Will be called before every test """ # ensure there is no data in the test database when the test starts db.session.commit() db.drop_all() db.create_all() # create test product testProduct = Products(productName="nameTEST", productInfo="This is a test product", productIMG="test.jpg", productPrice="10") # create test category testCategory = Categories(categoryName="cattest") # save the product and the category to database db.session.add(testProduct) db.session.add(testCategory) db.session.commit()
def admin(): categoryForm=CategoryForm() if categoryForm.validate_on_submit(): category=Categories( categoryName=categoryForm.categoryName.data ) db.session.add(category) db.session.commit() return redirect(url_for('products')) productForm=ProductForm() if productForm.validate_on_submit(): if productForm.productIMG.data: FinalIMG=save_img(productForm.productIMG.data) Products.productIMG = FinalIMG product=Products( productName=productForm.productName.data, productInfo=productForm.productInfo.data, productIMG=FinalIMG, productPrice=productForm.productPrice.data ) db.session.add(product) db.session.commit() return redirect(url_for('products')) updateProduct=UpdateProducts() if updateProduct.validate_on_submit(): newPrice=Products.query.filter_by(productName=updateProduct.productName.data).update(dict(productPrice=updateProduct.productPrice.data)) db.session.commit() return redirect(url_for('products')) deleteProduct=DeleteProduct() if deleteProduct.validate_on_submit(): Products.query.filter_by(productName=deleteProduct.productName.data).delete() db.session.commit() return redirect(url_for('products')) return render_template('admin.html', form=categoryForm, form1=productForm, form2=updateProduct, form3=deleteProduct)
# drop and then create tables from scratch. All previous data is wiped, so this file should be run only to setup or restart the entire system db.drop_all() db.create_all() # add test customer record customer_add = Customers(first_name="Test_First_Name", last_name="Test_Last_Name", customer_address='Test_Address', customer_dob='2002-07-05') db.session.add(customer_add) # add test product db.session.add( Products(product_name="Test_Product", product_brand="Test_Brand", quantity_in_stock=100, cost_per_item=1, price=1)) # add test order order_add = Orders(purchase_date='1500-01-01', price=1, cash_payment=1, quantity_ordered=1, fk_customer_id=1, fk_product_id=1) db.session.add(order_add) db.session.commit()