Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
7047CEM-Group-22-Github-Coventry-version/AddStock.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
72 lines (61 sloc)
3.76 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/add', methods=['POST']) | |
def add_product_to_cart(): | |
""" | |
Takes no paramethis inputs, will take inputs from form which adds products to the cart and will then use data from the products database to add costs and book codes | |
will then also link to the simple payments project to create a checkout when the user checkouts the products. | |
""" | |
cursor = None | |
try: | |
_quantity = int(request.form['quantity']) | |
_code = request.form['code'] | |
# forms which take input for the quanityt of books being added and adding the books to the cart | |
if _quantity and _code and request.method == 'POST': | |
con = sqlite3.connect('products.db') | |
cur = con.cursor(); | |
cur.execute("SELECT * FROM products WHERE code=?;", [_code]) | |
row = cur.fetchone() | |
#connects to the product database and look for the product details and then add them to an array | |
itemArray = { row[2] : {'name' : row[1], 'code' : row[2], 'quantity' : _quantity, 'price' : row[4], 'image' : row[3], 'total_price': _quantity * row[4]}} | |
print('itemArray is', itemArray) | |
all_total_price = 0 | |
all_total_quantity = 0 | |
session.modified = True | |
if 'cart_item' in session: | |
print('in session') | |
#This will check if the code for the book selected is within the cart and make sure to adjust the quantity depending on how many are added to the cart | |
if row[2] in session['cart_item']: | |
for key, value in session['cart_item'].items(): | |
if row[2] == key: | |
old_quantity = session['cart_item'][key]['quantity'] | |
total_quantity = old_quantity + _quantity | |
session['cart_item'][key]['quantity'] = total_quantity | |
session['cart_item'][key]['total_price'] = total_quantity * row[4] | |
else: | |
session['cart_item'] = array_merge(session['cart_item'], itemArray) | |
for key, value in session['cart_item'].items(): | |
individual_quantity = int(session['cart_item'][key]['quantity']) | |
individual_price = float(session['cart_item'][key]['total_price']) | |
all_total_quantity = all_total_quantity + individual_quantity | |
all_total_price = all_total_price + individual_price | |
#this will check for the price and adjust the price within the cart | |
else: | |
session['cart_item'] = itemArray | |
all_total_quantity = all_total_quantity + _quantity | |
all_total_price = all_total_price + _quantity * row[4] | |
session['all_total_quantity'] = all_total_quantity | |
session['all_total_price'] = all_total_price | |
checksumstr = f"pid={pid:s}&sid={sid:s}&amount={all_total_price:.1f}&token={secret:s}" #this will use the variables sid, pid and the secret token to form the checksum which will form a secure connection to the payment system allowing payments to go through | |
#print('checksumstr is', checksumstr) | |
checksum = md5(checksumstr.encode('utf-8')).hexdigest() | |
session['checksum'] = checksum | |
#print('checksum is', checksum) | |
session['sid'] = sid | |
session['pid'] = pid | |
return redirect(url_for('.home')) | |
else: | |
return 'Error while adding item to cart' | |
except Exception as e: | |
print(e) | |
finally: | |
cur.close() | |
con.close() |