Overhauled the way in which user details are passed to the frontend
This commit is contained in:
parent
19165220fa
commit
b2e32d720c
@ -50,6 +50,30 @@ class UserController(DatabaseController):
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def read_id(self, id: int) -> User | None:
|
||||
params = [
|
||||
id
|
||||
]
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"SELECT * FROM Users WHERE id = ?",
|
||||
params
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
|
||||
if row != None:
|
||||
params = dict(zip(self.FIELDS, row))
|
||||
|
||||
# Is user a seller
|
||||
type = Customer
|
||||
if row[7] == "Seller":
|
||||
type = Seller
|
||||
|
||||
obj = self.new_instance(type, params)
|
||||
return obj
|
||||
|
||||
return None
|
||||
def update(self):
|
||||
print("Doing work")
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
from flask import redirect
|
||||
from flask import Blueprint
|
||||
from models.users.user import User
|
||||
from controllers.database.user import UserController
|
||||
|
||||
from flask import redirect, Blueprint, session
|
||||
|
||||
from . import user
|
||||
from . import product
|
||||
@ -9,6 +11,24 @@ blueprint = Blueprint('main', __name__)
|
||||
blueprint.register_blueprint(user.blueprint)
|
||||
blueprint.register_blueprint(product.blueprint)
|
||||
|
||||
|
||||
### CONTEXTS ###
|
||||
|
||||
# Function that returns a given user class based on the ID in the session
|
||||
@blueprint.context_processor
|
||||
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)
|
||||
print(user)
|
||||
|
||||
return dict(user=user)
|
||||
|
||||
|
||||
# Function responsible for displaying the main landing page of the site
|
||||
@blueprint.route('/')
|
||||
def index():
|
||||
|
@ -24,7 +24,7 @@ def index():
|
||||
if products == None:
|
||||
flash("No Products available")
|
||||
|
||||
return render_template('index.html', content="content.html", user = session.get('user'), products = products)
|
||||
return render_template('index.html', content="content.html", products = products)
|
||||
|
||||
# Loads a given product category page
|
||||
@blueprint.route('/<string:category>')
|
||||
@ -43,7 +43,7 @@ def category(category: str):
|
||||
if products == None:
|
||||
flash(f"No Products available in {category}")
|
||||
|
||||
return render_template('index.html', content="content.html", user = session.get('user'), 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>')
|
||||
|
@ -2,6 +2,7 @@ from flask import Blueprint
|
||||
|
||||
from flask import render_template, redirect, request, session, flash
|
||||
from controllers.database.user import UserController
|
||||
from models.users.user import User
|
||||
from models.users.customer import Customer
|
||||
from models.users.seller import Seller
|
||||
from hashlib import sha512
|
||||
@ -13,7 +14,7 @@ blueprint = Blueprint("users", __name__)
|
||||
# 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'))
|
||||
return render_template('index.html', content="login.html")
|
||||
|
||||
# Function responsible for handling logins to the site
|
||||
@blueprint.post('/login')
|
||||
@ -34,7 +35,7 @@ def login():
|
||||
flash(error)
|
||||
return redirect("/login")
|
||||
|
||||
session['user'] = user.username
|
||||
session['user_id'] = user.id
|
||||
return redirect("/")
|
||||
|
||||
|
||||
@ -42,7 +43,7 @@ def login():
|
||||
# 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'))
|
||||
return render_template('index.html', content="signup.html")
|
||||
|
||||
# Function responsible for handling signups to the site
|
||||
@blueprint.post('/signup')
|
||||
@ -55,7 +56,7 @@ def signup():
|
||||
flash(error)
|
||||
return redirect("/signup")
|
||||
|
||||
# Signup as seller or Customer
|
||||
# Signup as Seller or Customer
|
||||
if request.form.get('seller'):
|
||||
user = Seller(
|
||||
request.form['username'],
|
||||
@ -85,5 +86,5 @@ def signup():
|
||||
# Function responsible for handling logouts from the site
|
||||
@blueprint.route('/logout')
|
||||
def logout():
|
||||
session.pop('user')
|
||||
session.pop('user_id')
|
||||
return redirect("/")
|
@ -6,7 +6,7 @@
|
||||
<input type="submit" class="search-button">
|
||||
</form>
|
||||
{% if user != None: %}
|
||||
<a href="/logout">Welcome, {{ user }}</a>
|
||||
<a href="/logout">Welcome, {{ user.username }}</a>
|
||||
{% else %}
|
||||
<a href="/login">Login/Signup</a>
|
||||
{% endif %}
|
||||
|
Loading…
Reference in New Issue
Block a user