Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
from flask import Flask, flash,url_for, request, redirect,make_response
from flask import session, Blueprint, render_template, g, abort
from markupsafe import escape
from flask_wtf import Form
from pathlib import Path
import functools
import sqlite3
shopping = Blueprint("shopping", __name__)
@shopping.route('/add', methods=['POST'])
def add_to_cart():
if 'username' in session:
try:
#request the quantity and isbn number of the book being added to cart
_quantity = int(request.form['quantity'])
_isbn = request.form['isbn']
if _quantity and _isbn and request.method == 'POST':
# selects the record of the book being added from the book_stock database using isbn as an identfier
con = sqlite3.connect('book_stock.db')
cur = con.cursor();
cur.execute("SELECT * FROM books WHERE isbn=?;", [_isbn])
row = cur.fetchone()
#add the details of the book being added along with the quantity into the item array
itemArray = { _isbn: {'name' : row[1],'isbn':row[0], 'image' : row[5], 'price' : row[7], 'quantity' : _quantity, 'total_price' : _quantity * row[7]}}
print('itemArray is', itemArray)
all_total_price = 0
all_total_quantity = 0
session.modified = True
if 'cart_item' in session:
print('in session')
if _isbn in session['cart_item']:
#checks if book is already in the cart, if it is, update the quantity, total_price and total_quantity
for key, value in session['cart_item'].items():
if _isbn == key:
total_quantity = session['cart_item'][key]['quantity'] + _quantity
session['cart_item'][key]['quantity'] = total_quantity
session['cart_item'][key]['total_price'] = total_quantity * row[7]
else:
#if it is not, added the book details to session[cart_item ]
session['cart_item'].update(itemArray)
# loop through session[cart_item ] and add the total amount and price of the books
for key, value in session['cart_item'].items():
individual_quantity = int(session['cart_item'][key]['quantity'])
individual_price = int(session['cart_item'][key]['total_price'])
all_total_quantity = all_total_quantity +individual_quantity
all_total_price = all_total_price + individual_price
else:
#when the cart_item is not in session
#set the item being added into session and calculate all_total_price and all_total_quantity
session['cart_item'] = itemArray
all_total_quantity = all_total_quantity + _quantity
all_total_price = all_total_price + (_quantity*row[7])
session['all_total_price'] = all_total_price
session['all_total_quantity'] = all_total_quantity
return redirect(url_for('index'))
else:
return 'Error while adding item to cart'
except Exception as e:
print(e)
finally:
cur.close()
con.close()
else:
return redirect(url_for('register_login.login'));
@shopping.route("/cart")
def cart():
#checks if user logged in first
if 'username' in session:
return render_template("cart.html")
else:
return redirect(url_for('register_login.login'));
@shopping.route('/clear_cart')
def clear_cart():
if 'username' in session:
try:
all_total_price = 0
all_total_quantity = 0
session.modified = True;
# iterates through items in cart and removes from the cart_item session
for key in list(session['cart_item'].keys()):
session['cart_item'].pop(key)
#set all_total_quantity and all_total_price to 0, then redirects to index
session['all_total_quantity'] = all_total_quantity
session['all_total_price'] = all_total_price
return redirect(url_for('index'))
except Exception as e:
print(e)
else:
return redirect(url_for('register_login.login'));
@shopping.route('/delete/<string:isbn>')
def delete_book(isbn):
if 'username' in session:
try:
all_total_price = 0
all_total_quantity = 0
session.modified = True;
# iterates through the cart and if the isbn is equal to the one supplied, check if the quantity in cart is 1
for item in session['cart_item'].items():
if item[0] == isbn:
if int(session['cart_item'][item[0]]['quantity']) == 1:
#if it is, remove item from cart
session['cart_item'].pop(item[0],None)
else:
#if it is not, subtract 1 from item quantity and calculate new total_price
session['cart_item'][item[0]]['quantity'] = int(session['cart_item'][item[0]]['quantity']) - 1
session['cart_item'][item[0]]['total_price'] = int(session['cart_item'][item[0]]['price'])*int(session['cart_item'][item[0]]['quantity'])
if 'cart_item' in session:
for key, value in session['cart_item'].items():
#calculate the new all_total_quantity and all_total_price after deleting book
individual_quantity = int(session['cart_item'][key]['quantity'])
individual_price = int(session['cart_item'][key]['total_price'])
all_total_quantity = all_total_quantity + individual_quantity
all_total_price = all_total_price + individual_price
break
# set new all_total_price and all_total_quantity into session
session['all_total_quantity'] = all_total_quantity
session['all_total_price'] = all_total_price
return redirect(url_for('index'))
except Exception as e:
print(e)
else:
return redirect(url_for('register_login.login'));