This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
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?
JH_Bookshop/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
122 lines (106 sloc)
4.43 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
from flask import Flask | |
import sqlite3 | |
from flask import flash, session, render_template, request, redirect, url_for | |
from werkzeug.security import generate_password_hash, check_password_hash | |
app = Flask(__name__) | |
app.secret_key = "secret key" | |
@app.route('/add', methods=['POST']) | |
def add_product_to_cart(): | |
cursor = None | |
try: | |
_quantity = int(request.form['quantity']) | |
_code = request.form['code'] | |
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() | |
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') | |
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 | |
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 | |
return redirect(url_for('.products')) | |
else: | |
return 'Error while adding item to cart' | |
except Exception as e: | |
print(e) | |
finally: | |
cur.close() | |
con.close() | |
@app.route('/') | |
def products(): | |
try: | |
con = sqlite3.connect('products.db') | |
cur = con.cursor(); | |
cur.execute("SELECT * FROM products") | |
rows = cur.fetchall() | |
return render_template('products.html', products=rows) | |
except Exception as e: | |
print(e) | |
finally: | |
cur.close() | |
con.close() | |
@app.route('/empty') | |
def empty_cart(): | |
try: | |
session.clear() | |
return redirect(url_for('.products')) | |
except Exception as e: | |
print(e) | |
@app.route('/delete/<string:code>') | |
def delete_product(code): | |
try: | |
all_total_price = 0 | |
all_total_quantity = 0 | |
session.modified = True | |
for item in session['cart_item'].items(): | |
if item[0] == code: | |
session['cart_item'].pop(item[0], None) | |
if 'cart_item' in session: | |
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 | |
break | |
if all_total_quantity == 0: | |
session.clear() | |
else: | |
session['all_total_quantity'] = all_total_quantity | |
session['all_total_price'] = all_total_price | |
return redirect(url_for('.products')) | |
except Exception as e: | |
print(e) | |
def array_merge( first_array , second_array ): | |
if isinstance( first_array , list ) and isinstance( second_array , list ): | |
return first_array + second_array | |
elif isinstance( first_array , dict ) and isinstance( second_array , dict ): | |
return dict( list( first_array.items() ) + list( second_array.items() ) ) | |
elif isinstance( first_array , set ) and isinstance( second_array , set ): | |
return first_array.union( second_array ) | |
return False | |
if __name__ == "__main__": | |
app.run() |