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?
10210411-bookstore/bookmanager.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
179 lines (154 sloc)
7.99 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
import os | |
from flask import Flask | |
from flask import render_template | |
from flask import request | |
from flask import redirect | |
import pickle | |
import sqlite3 | |
from flask_sqlalchemy import SQLAlchemy | |
from flask import flash, session, url_for | |
app = Flask(__name__) | |
app.secret_key = "secret key" | |
@app.route('/') | |
def main(): #main function with our default url | |
return render_template("login.html") #display the login page | |
database={'admin':'p455w0rd','customer1':'p455w0rd','customer2':'p455w0rd'} #database with usernames and passwords | |
@app.route('/form_login',methods=['POST','GET']) | |
def login(): #function that checks login information | |
name1=request.form['username'] #extracting the username and putting in a variable | |
pwd=request.form['password'] #extracting the password and putting in a variable | |
if name1 not in database: | |
return render_template('login.html',info='Invalid User') #if username wrong return the login page and say wrong username | |
else: | |
if database[name1]!=pwd: | |
return render_template('login.html',info='Invalid Password') #if password wrong return the login page and say wrong password | |
elif name1 == 'admin': | |
return render_template('admin.html', name=name1) #check if it's admin and return the admin page | |
else: | |
return render_template('profile.html',name=name1) #check if it's customer and return the customer page | |
@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('books0.db') | |
cur = con.cursor(); | |
cur.execute("SELECT * FROM books0 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('/products') | |
def products(): | |
try: | |
con = sqlite3.connect('books0.db') | |
cur = con.cursor(); | |
cur.execute("SELECT * FROM books0") | |
rows = cur.fetchall() | |
return render_template('products.html', products=rows) | |
except Exception as e: | |
print(e) | |
finally: | |
cur.close() | |
con.close() | |
@app.route('/cart') | |
def cart(): | |
return render_template("cart.html") | |
@app.route('/empty') | |
def empty_cart(): | |
try: | |
session.clear() | |
return redirect(url_for('.cart')) | |
except Exception as e: | |
print(e) | |
@app.route('/delete/<string:code>') #mapping url with associated fucntion | |
def delete_product(code): | |
try: | |
all_total_price = 0 #sum of product prices | |
all_total_quantity = 0 #sum of product quantites in the cart | |
session.modified = True #using sessions to keep track of the user activity | |
for item in session['cart_item'].items(): | |
if item[0] == code: | |
session['cart_item'].pop(item[0], None) #pop() removes items from list hence deleting it | |
if 'cart_item' in session: | |
for key, value in session['cart_item'].items(): | |
individual_quantity = int(session['cart_item'][key]['quantity']) #defining the variable for quantity of one item | |
individual_price = float(session['cart_item'][key]['total_price']) #defining the variable for the price of one product | |
all_total_quantity = all_total_quantity + individual_quantity #calculating quantity of the cart | |
all_total_price = all_total_price + individual_price #calculating the total price of all products in the cart | |
break | |
if all_total_quantity == 0: #if there is nothing in the cart do nothing | |
session.clear() | |
else: | |
session['all_total_quantity'] = all_total_quantity #otherwise put the total quantity in session | |
session['all_total_price'] = all_total_price #otherwise put the total price in session | |
return redirect(url_for('.cart')) #redirect to cart and show the products and their price there | |
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 | |
@app.route('/stock') | |
def stock(): | |
try: | |
con = sqlite3.connect('books0.db') | |
cur = con.cursor(); | |
cur.execute("SELECT * FROM books0") | |
rows = cur.fetchall() | |
return render_template('stock.html', products=rows) | |
except Exception as e: | |
print(e) | |
finally: | |
cur.close() | |
con.close() | |
@app.route('/add_stock') | |
def add_stock(): | |
return render_template("add_stock.html") | |
""" | |
@app.route('/admin') | |
def back_admin(): | |
return render_template("admin.html") | |
@app.route('/customer') | |
def back_customer(): | |
return render_template("profile.html")""" | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', debug=True) |