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?
website_project/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.
175 lines (127 sloc)
4.25 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 | |
from markupsafe import escape | |
from flask import url_for | |
from flask import render_template | |
from flask import request | |
from flask import redirect | |
from flask import abort | |
from flask import make_response | |
from flask import flash | |
from flask import session | |
import sqlite3 | |
app = Flask(__name__) | |
@app.route('/') | |
def hello_world(): | |
return redirect(url_for('register')) | |
# add stocks | |
@app.route('/add_stocks', methods=['GET','POST']) | |
def add_books(): | |
if request.method == 'POST': | |
return do_the_add(request.form['ID'], request.form['Name'],) | |
else: | |
return show_the_add_form(); | |
def show_the_add_form(): | |
return render_template('add_books.html',page=url_for('add_books')) | |
# books | |
@app.route('/books') | |
def books(): | |
con = sqlite3.connect("database.db") | |
con.row_factory = sqlite3.Row | |
cur = con.cursor() | |
cur.execute("SELECT * from books") | |
rows = cur.fetchall(); | |
return render_template("books.html",rows = rows) | |
# admin sees home page with stocks button | |
@app.route('/books_admin') | |
def books_admin(): | |
con = sqlite3.connect("database.db") | |
con.row_factory = sqlite3.Row | |
cur = con.cursor() | |
cur.execute("SELECT * from books") | |
rows = cur.fetchall(); | |
return render_template("books_admin.html",rows = rows) | |
# admin can view stocks page | |
@app.route('/books_admin_sees') | |
def books_admin_sees(): | |
con = sqlite3.connect("database.db") | |
con.row_factory = sqlite3.Row | |
cur = con.cursor() | |
cur.execute("SELECT * from books") | |
rows = cur.fetchall(); | |
return render_template("stocks.html",rows = rows) | |
# register | |
@app.route('/register', methods=['GET', 'POST']) | |
def register(): | |
if request.method == 'POST': | |
return do_the_registration(request.form['uname'], request.form['pwd']) | |
else: | |
return show_the_registration_form(); | |
def show_the_registration_form(): | |
return render_template('register.html',page=url_for('register')) | |
def do_the_registration(u,p): | |
con = sqlite3.connect('registered_users.db') | |
try: | |
con.execute('CREATE TABLE users (name TEXT, pwd INT)') | |
print ('Table created successfully'); | |
except: | |
pass | |
con.close() | |
con = sqlite3.connect('registered_users.db') | |
con.execute("INSERT INTO users values(?,?);", (u,p)) | |
con.commit() | |
con.close() | |
return show_the_login_form() | |
# login | |
@app.route('/login', methods=['GET', 'POST']) | |
def login(): | |
if request.method == 'POST': | |
return do_the_login(request.form['uname'], request.form['pwd']) | |
else: | |
return show_the_login_form() | |
def show_the_login_form(): | |
return render_template('login.html',page=url_for('login')) | |
def do_the_login(u,p): | |
con = sqlite3.connect('registered_users.db') | |
cur = con.cursor(); | |
cur.execute("SELECT count(*) FROM users WHERE name=? AND pwd=?;", (u,p)) | |
if(int(cur.fetchone()[0]))>0: | |
return f'<H1>Success!</H1>' | |
else: | |
abort(403) | |
@app.errorhandler(403) | |
def wrong_details(error): | |
return render_template('wrong_details.html'), 403 | |
def do_the_login(u,p): | |
if(p =='p455w0rd'): | |
return redirect(url_for('books')) | |
else: | |
return f'<H1>Failure!<H1>' | |
def do_the_registration(u,p): | |
if(u == 'admin'): | |
return redirect(url_for('books_admin')) | |
if(u == 'customer1'): | |
return redirect(url_for('books')) | |
if(u == 'customer2'): | |
return redirect(url_for('books')) | |
else: | |
return f'<H1>Failure</H1>' | |
def do_the_login(u,p): | |
if(u == 'admin'): | |
return redirect(url_for('books_admin')) | |
if(u == 'customer1'): | |
return redirect(url_for('books')) | |
if(u == 'customer2'): | |
return redirect(url_for('books')) | |
else: | |
return f'<H1>Failure</H1>' | |
#create checkoutpage // not correct NEED TO FIX IT | |
app.route('/checkout') | |
def checkout(): | |
con = sqlite3.connect("database.db") | |
con.row_factory = sqlite3.Row | |
cur = con.cursor() | |
cur.execute("SELECT * from books") | |
rows = cur.fetchall(); | |
return render_template('checkout.html') | |
if __name__ == "__main__": | |
app.run() | |