Added ability to fetch products from the database
This commit is contained in:
@ -1,5 +0,0 @@
|
||||
from flask import Blueprint
|
||||
|
||||
blueprint = Blueprint('endpoints', __name__)
|
||||
|
||||
from . import endpoints
|
@ -1,81 +1,15 @@
|
||||
from . import blueprint
|
||||
from flask import render_template, redirect, request, session, flash
|
||||
from controllers.database.user import UserController
|
||||
from models.users.customer import Customer
|
||||
from hashlib import sha512
|
||||
from flask import redirect
|
||||
from flask import Blueprint
|
||||
|
||||
from . import user
|
||||
from . import product
|
||||
|
||||
blueprint = Blueprint('main', __name__)
|
||||
|
||||
blueprint.register_blueprint(user.blueprint)
|
||||
blueprint.register_blueprint(product.blueprint)
|
||||
|
||||
# Function responsible for displaying the main landing page of the site
|
||||
@blueprint.route('/')
|
||||
def welcome_page():
|
||||
return render_template('index.html', content="content.html", user = session.get('user'))
|
||||
|
||||
|
||||
|
||||
### LOGIN FUNCTIONALITY
|
||||
# Function responsible for delivering the Login page for the site
|
||||
@blueprint.route('/login')
|
||||
def display_login():
|
||||
return render_template('index.html', content="login.html", user = session.get('user'))
|
||||
|
||||
# Function responsible for handling logins to the site
|
||||
@blueprint.post('/login')
|
||||
def login():
|
||||
database = UserController()
|
||||
user = database.read(request.form['username'])
|
||||
error = None
|
||||
|
||||
# No user found
|
||||
if user == None:
|
||||
error = "No user found with the username " + request.form['username']
|
||||
flash(error)
|
||||
return redirect("/login")
|
||||
|
||||
# Incorrect Password
|
||||
if sha512(request.form['password'].encode()).hexdigest() != user.password:
|
||||
error = "Incorrect Password"
|
||||
flash(error)
|
||||
return redirect("/login")
|
||||
|
||||
session['user'] = user.username
|
||||
return redirect("/")
|
||||
|
||||
|
||||
### SIGNUP FUNCTIONALITY
|
||||
# Function responsible for delivering the Signup page for the site
|
||||
@blueprint.route('/signup')
|
||||
def display_signup():
|
||||
return render_template('index.html', content="signup.html", user = session.get('user'))
|
||||
|
||||
# Function responsible for handling signups to the site
|
||||
@blueprint.post('/signup')
|
||||
def signup():
|
||||
database = UserController()
|
||||
|
||||
# User already exists
|
||||
if database.read(request.form['username']) != None:
|
||||
error = "User, " + request.form['username'] + " already exists"
|
||||
flash(error)
|
||||
return redirect("/signup")
|
||||
|
||||
database.create(Customer(
|
||||
0,
|
||||
request.form['username'],
|
||||
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
|
||||
request.form['firstname'],
|
||||
request.form['lastname'],
|
||||
request.form['email'],
|
||||
"123",
|
||||
"Customer"
|
||||
))
|
||||
|
||||
# Code 307 Preserves the original request (POST)
|
||||
return redirect("/login", code=307)
|
||||
|
||||
|
||||
### SIGN OUT FUNCTIONALITY
|
||||
# Function responsible for handling logouts from the site
|
||||
@blueprint.route('/logout')
|
||||
def logout():
|
||||
session.pop('user')
|
||||
return redirect("/")
|
||||
def index():
|
||||
return redirect("/products")
|
||||
|
17
controllers/web/product.py
Normal file
17
controllers/web/product.py
Normal file
@ -0,0 +1,17 @@
|
||||
from flask import Blueprint
|
||||
|
||||
from flask import render_template, redirect, request, session, flash
|
||||
from controllers.database.product import ProductController
|
||||
|
||||
blueprint = Blueprint("products", __name__, url_prefix="/products")
|
||||
|
||||
@blueprint.route('/')
|
||||
def index():
|
||||
database = ProductController()
|
||||
products = database.read_all()
|
||||
|
||||
# No Products visible
|
||||
if products == None:
|
||||
flash("No Products available")
|
||||
|
||||
return render_template('index.html', content="content.html", user = session.get('user'), products = products)
|
77
controllers/web/user.py
Normal file
77
controllers/web/user.py
Normal file
@ -0,0 +1,77 @@
|
||||
from flask import Blueprint
|
||||
|
||||
from flask import render_template, redirect, request, session, flash
|
||||
from controllers.database.user import UserController
|
||||
from models.users.customer import Customer
|
||||
from hashlib import sha512
|
||||
|
||||
# Blueprint to append user endpoints to
|
||||
blueprint = Blueprint("users", __name__)
|
||||
|
||||
### LOGIN FUNCTIONALITY
|
||||
# Function responsible for delivering the Login page for the site
|
||||
@blueprint.route('/login')
|
||||
def display_login():
|
||||
return render_template('index.html', content="login.html", user = session.get('user'))
|
||||
|
||||
# Function responsible for handling logins to the site
|
||||
@blueprint.post('/login')
|
||||
def login():
|
||||
database = UserController()
|
||||
user = database.read(request.form['username'])
|
||||
error = None
|
||||
|
||||
# No user found
|
||||
if user == None:
|
||||
error = "No user found with the username " + request.form['username']
|
||||
flash(error)
|
||||
return redirect("/login")
|
||||
|
||||
# Incorrect Password
|
||||
if sha512(request.form['password'].encode()).hexdigest() != user.password:
|
||||
error = "Incorrect Password"
|
||||
flash(error)
|
||||
return redirect("/login")
|
||||
|
||||
session['user'] = user.username
|
||||
return redirect("/")
|
||||
|
||||
|
||||
### SIGNUP FUNCTIONALITY
|
||||
# Function responsible for delivering the Signup page for the site
|
||||
@blueprint.route('/signup')
|
||||
def display_signup():
|
||||
return render_template('index.html', content="signup.html", user = session.get('user'))
|
||||
|
||||
# Function responsible for handling signups to the site
|
||||
@blueprint.post('/signup')
|
||||
def signup():
|
||||
database = UserController()
|
||||
|
||||
# User already exists
|
||||
if database.read(request.form['username']) != None:
|
||||
error = "User, " + request.form['username'] + " already exists"
|
||||
flash(error)
|
||||
return redirect("/signup")
|
||||
|
||||
database.create(Customer(
|
||||
0,
|
||||
request.form['username'],
|
||||
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
|
||||
request.form['firstname'],
|
||||
request.form['lastname'],
|
||||
request.form['email'],
|
||||
"123",
|
||||
"Customer"
|
||||
))
|
||||
|
||||
# Code 307 Preserves the original request (POST)
|
||||
return redirect("/login", code=307)
|
||||
|
||||
|
||||
### SIGN OUT FUNCTIONALITY
|
||||
# Function responsible for handling logouts from the site
|
||||
@blueprint.route('/logout')
|
||||
def logout():
|
||||
session.pop('user')
|
||||
return redirect("/")
|
Reference in New Issue
Block a user