2024-02-13 10:04:59 +00:00
|
|
|
""" The admin controller to manage all of the admin related endpoints
|
2024-02-12 19:28:15 +00:00
|
|
|
in the web app
|
|
|
|
"""
|
|
|
|
|
2024-02-14 22:13:34 +00:00
|
|
|
from flask import render_template, Blueprint, redirect, url_for, flash, request
|
2024-02-13 10:04:59 +00:00
|
|
|
|
|
|
|
from controllers.database.user import UserController
|
2024-02-14 14:56:58 +00:00
|
|
|
from controllers.database.product import ProductController
|
|
|
|
|
|
|
|
from utils.user_utils import is_role
|
2024-02-12 19:28:15 +00:00
|
|
|
|
|
|
|
# Blueprint to append user endpoints to
|
|
|
|
blueprint = Blueprint("admin", __name__, url_prefix="/admin")
|
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@blueprint.before_request
|
|
|
|
def check_admin_user():
|
|
|
|
""" Preprocess for all admin endpoints to ensure that the requesting
|
|
|
|
user is logged in as an 'Admin'
|
|
|
|
"""
|
|
|
|
if not is_role("Admin"):
|
|
|
|
flash("You must be logged in as an Admin to view this page.", "error")
|
|
|
|
return redirect(url_for('main.index'))
|
|
|
|
|
|
|
|
|
2024-02-12 19:28:15 +00:00
|
|
|
@blueprint.route('/')
|
2024-02-13 22:48:23 +00:00
|
|
|
def main():
|
2024-02-12 19:28:15 +00:00
|
|
|
""" Function responsible for delivering the admin page for the site """
|
|
|
|
return "Hello, World"
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/users/')
|
2024-02-13 22:48:23 +00:00
|
|
|
def users():
|
2024-02-12 19:28:15 +00:00
|
|
|
""" Endpoint responsible for managing a users permissions """
|
2024-02-13 10:04:59 +00:00
|
|
|
# Get all users to create admin table on frontend
|
|
|
|
db = UserController()
|
2024-02-14 22:13:34 +00:00
|
|
|
|
|
|
|
search = request.args.get('search')
|
|
|
|
|
|
|
|
# Don't try submitting a None Type
|
|
|
|
if not search:
|
|
|
|
search = ""
|
|
|
|
|
|
|
|
users = db.read_all(search)
|
2024-02-13 10:04:59 +00:00
|
|
|
|
|
|
|
return render_template("index.html", content="admin.html", users=users)
|
2024-02-14 14:56:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/products/')
|
|
|
|
def products():
|
|
|
|
""" Endpoint responsible for managing products on the site """
|
|
|
|
# Get all products to create admin table on frontend
|
|
|
|
db = ProductController()
|
|
|
|
|
2024-02-14 22:13:34 +00:00
|
|
|
search = request.args.get('search')
|
|
|
|
|
|
|
|
# Don't try submitting a None Type
|
|
|
|
if not search:
|
|
|
|
search = ""
|
|
|
|
|
|
|
|
products = db.read_all("", search)
|
2024-02-14 16:46:30 +00:00
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
return render_template(
|
|
|
|
"index.html",
|
|
|
|
content="admin.html",
|
|
|
|
products=products
|
|
|
|
)
|