Applied auto pep 8 changes
This commit is contained in:
@ -16,15 +16,15 @@ blueprint.register_blueprint(product.blueprint)
|
||||
|
||||
# Function that returns a given user class based on the ID in the session
|
||||
@blueprint.context_processor
|
||||
def get_user() -> dict[User|None]:
|
||||
def get_user() -> dict[User | None]:
|
||||
# Get the user based on the user ID
|
||||
user_id = session.get('user_id')
|
||||
user = None
|
||||
|
||||
|
||||
if user_id != None:
|
||||
db = UserController()
|
||||
user = db.read_id(user_id)
|
||||
|
||||
|
||||
return dict(user=user)
|
||||
|
||||
|
||||
|
@ -15,14 +15,18 @@ import pathlib
|
||||
|
||||
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
blueprint = Blueprint("products", __name__, url_prefix="/products")
|
||||
|
||||
# Global context to enable the categories to be accessed
|
||||
# Global context to enable the categories to be accessed
|
||||
# from any view
|
||||
|
||||
|
||||
@blueprint.context_processor
|
||||
def category_list():
|
||||
database = CategoryController()
|
||||
@ -30,6 +34,8 @@ def category_list():
|
||||
return dict(categories=categories)
|
||||
|
||||
# Loads the front product page
|
||||
|
||||
|
||||
@blueprint.route('/')
|
||||
def index():
|
||||
database = ProductController()
|
||||
@ -38,14 +44,16 @@ def index():
|
||||
# No Products visible
|
||||
if products == None:
|
||||
flash("No Products available")
|
||||
|
||||
return render_template('index.html', content="content.html", products = products)
|
||||
|
||||
return render_template('index.html', content="content.html", products=products)
|
||||
|
||||
# Loads a given product category page
|
||||
|
||||
|
||||
@blueprint.route('/<string:category>')
|
||||
def category(category: str):
|
||||
database = ProductController()
|
||||
|
||||
|
||||
# Check to see if there is a custome search term
|
||||
search_term = request.args.get("search", type=str)
|
||||
if search_term != None:
|
||||
@ -57,10 +65,12 @@ def category(category: str):
|
||||
# No Products visible
|
||||
if products == None:
|
||||
flash(f"No Products available in {category}")
|
||||
|
||||
return render_template('index.html', content="content.html", products = products, category = category)
|
||||
|
||||
return render_template('index.html', content="content.html", products=products, category=category)
|
||||
|
||||
# Loads a given product based on ID
|
||||
|
||||
|
||||
@blueprint.route('/<int:id>')
|
||||
def id(id: int):
|
||||
return "ID: " + str(id)
|
||||
@ -70,12 +80,12 @@ def id(id: int):
|
||||
@blueprint.route('/add')
|
||||
def display_add_product():
|
||||
user_id = session.get('user_id')
|
||||
|
||||
|
||||
# User must be logged in to view this page
|
||||
if user_id == None:
|
||||
flash("Please Login to view this page")
|
||||
return redirect('/login')
|
||||
|
||||
|
||||
db = UserController()
|
||||
user = db.read_id(user_id)
|
||||
if user == None or user.role != "Seller":
|
||||
@ -89,12 +99,12 @@ def display_add_product():
|
||||
@blueprint.post('/add')
|
||||
def add_product():
|
||||
user_id = session.get('user_id')
|
||||
|
||||
|
||||
# User must be logged in to view this page
|
||||
if user_id == None:
|
||||
flash("Please Login to view this page")
|
||||
return redirect('/login', code=302)
|
||||
|
||||
|
||||
db = UserController()
|
||||
user = db.read_id(user_id)
|
||||
if user == None or user.role != "Seller":
|
||||
@ -102,7 +112,7 @@ def add_product():
|
||||
return redirect('/', code=302)
|
||||
|
||||
file = request.files.get('image')
|
||||
|
||||
|
||||
# Ensure that the correct file type is uploaded
|
||||
if file == None or not allowed_file(file.filename):
|
||||
flash("Invalid File Uploaded")
|
||||
|
@ -10,13 +10,17 @@ from hashlib import sha512
|
||||
# Blueprint to append user endpoints to
|
||||
blueprint = Blueprint("users", __name__)
|
||||
|
||||
### LOGIN FUNCTIONALITY
|
||||
# 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")
|
||||
|
||||
# Function responsible for handling logins to the site
|
||||
|
||||
|
||||
@blueprint.post('/login')
|
||||
def login():
|
||||
database = UserController()
|
||||
@ -28,7 +32,7 @@ def login():
|
||||
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"
|
||||
@ -39,13 +43,15 @@ def login():
|
||||
return redirect("/")
|
||||
|
||||
|
||||
### SIGNUP FUNCTIONALITY
|
||||
# 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")
|
||||
|
||||
# Function responsible for handling signups to the site
|
||||
|
||||
|
||||
@blueprint.post('/signup')
|
||||
def signup():
|
||||
database = UserController()
|
||||
@ -60,7 +66,8 @@ def signup():
|
||||
if request.form.get('seller'):
|
||||
user = Seller(
|
||||
request.form['username'],
|
||||
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
|
||||
# Hashed as soon as it is recieved on the backend
|
||||
sha512(request.form['password'].encode()).hexdigest(),
|
||||
request.form['firstname'],
|
||||
request.form['lastname'],
|
||||
request.form['email'],
|
||||
@ -69,23 +76,24 @@ def signup():
|
||||
else:
|
||||
user = Customer(
|
||||
request.form['username'],
|
||||
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
|
||||
# Hashed as soon as it is recieved on the backend
|
||||
sha512(request.form['password'].encode()).hexdigest(),
|
||||
request.form['firstname'],
|
||||
request.form['lastname'],
|
||||
request.form['email'],
|
||||
"123"
|
||||
)
|
||||
|
||||
|
||||
database.create(user)
|
||||
|
||||
# Code 307 Preserves the original request (POST)
|
||||
return redirect("/login", code=307)
|
||||
|
||||
|
||||
### SIGN OUT FUNCTIONALITY
|
||||
# SIGN OUT FUNCTIONALITY
|
||||
# Function responsible for handling logouts from the site
|
||||
@blueprint.route('/logout')
|
||||
def logout():
|
||||
# Clear the current user from the session if they are logged in
|
||||
session.pop('user_id', None)
|
||||
return redirect("/")
|
||||
return redirect("/")
|
||||
|
Reference in New Issue
Block a user