2024-01-22 08:33:28 +00:00
|
|
|
""" The user controller to manage all of the user related endpoints
|
|
|
|
in the web app
|
|
|
|
"""
|
2024-01-05 18:20:56 +00:00
|
|
|
from flask import Blueprint
|
|
|
|
|
|
|
|
from flask import render_template, redirect, request, session, flash
|
|
|
|
from controllers.database.user import UserController
|
2024-01-19 11:57:53 +00:00
|
|
|
from models.users.user import User
|
2024-01-05 18:20:56 +00:00
|
|
|
from models.users.customer import Customer
|
2024-01-18 20:24:49 +00:00
|
|
|
from models.users.seller import Seller
|
2024-01-05 18:20:56 +00:00
|
|
|
from hashlib import sha512
|
|
|
|
|
|
|
|
# Blueprint to append user endpoints to
|
|
|
|
blueprint = Blueprint("users", __name__)
|
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-22 08:33:28 +00:00
|
|
|
# LOGIN FUNCTIONALITY
|
2024-01-05 18:20:56 +00:00
|
|
|
@blueprint.route('/login')
|
|
|
|
def display_login():
|
2024-01-22 08:33:28 +00:00
|
|
|
""" Function responsible for delivering the Login page for the site """
|
2024-01-19 11:57:53 +00:00
|
|
|
return render_template('index.html', content="login.html")
|
2024-01-05 18:20:56 +00:00
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
@blueprint.post('/login')
|
|
|
|
def login():
|
2024-01-22 08:33:28 +00:00
|
|
|
""" Function to handle the backend processing of a login request """
|
2024-01-05 18:20:56 +00:00
|
|
|
database = UserController()
|
|
|
|
user = database.read(request.form['username'])
|
|
|
|
error = None
|
|
|
|
|
|
|
|
# No user found
|
2024-01-21 22:22:29 +00:00
|
|
|
if user is None:
|
2024-01-05 18:20:56 +00:00
|
|
|
error = "No user found with the username " + request.form['username']
|
|
|
|
flash(error)
|
|
|
|
return redirect("/login")
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
# Incorrect Password
|
|
|
|
if sha512(request.form['password'].encode()).hexdigest() != user.password:
|
|
|
|
error = "Incorrect Password"
|
|
|
|
flash(error)
|
|
|
|
return redirect("/login")
|
|
|
|
|
2024-01-19 11:57:53 +00:00
|
|
|
session['user_id'] = user.id
|
2024-01-05 18:20:56 +00:00
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
# SIGNUP FUNCTIONALITY
|
2024-01-05 18:20:56 +00:00
|
|
|
@blueprint.route('/signup')
|
|
|
|
def display_signup():
|
2024-01-22 08:33:28 +00:00
|
|
|
""" Function responsible for delivering the Signup page for the site """
|
2024-01-19 11:57:53 +00:00
|
|
|
return render_template('index.html', content="signup.html")
|
2024-01-05 18:20:56 +00:00
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
@blueprint.post('/signup')
|
|
|
|
def signup():
|
2024-01-22 08:33:28 +00:00
|
|
|
""" Function to handle the backend processing of a signup request """
|
2024-01-05 18:20:56 +00:00
|
|
|
database = UserController()
|
|
|
|
|
|
|
|
# User already exists
|
2024-01-21 22:22:29 +00:00
|
|
|
if database.read(request.form['username']) is not None:
|
2024-01-05 18:20:56 +00:00
|
|
|
error = "User, " + request.form['username'] + " already exists"
|
|
|
|
flash(error)
|
|
|
|
return redirect("/signup")
|
|
|
|
|
2024-01-19 11:57:53 +00:00
|
|
|
# Signup as Seller or Customer
|
2024-01-18 20:24:49 +00:00
|
|
|
if request.form.get('seller'):
|
|
|
|
user = Seller(
|
|
|
|
request.form['username'],
|
2024-01-21 22:06:06 +00:00
|
|
|
# Hashed as soon as it is recieved on the backend
|
|
|
|
sha512(request.form['password'].encode()).hexdigest(),
|
2024-01-18 20:24:49 +00:00
|
|
|
request.form['firstname'],
|
|
|
|
request.form['lastname'],
|
|
|
|
request.form['email'],
|
|
|
|
"123"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
user = Customer(
|
|
|
|
request.form['username'],
|
2024-01-21 22:06:06 +00:00
|
|
|
# Hashed as soon as it is recieved on the backend
|
|
|
|
sha512(request.form['password'].encode()).hexdigest(),
|
2024-01-18 20:24:49 +00:00
|
|
|
request.form['firstname'],
|
|
|
|
request.form['lastname'],
|
|
|
|
request.form['email'],
|
|
|
|
"123"
|
|
|
|
)
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-18 20:24:49 +00:00
|
|
|
database.create(user)
|
2024-01-05 18:20:56 +00:00
|
|
|
|
|
|
|
# Code 307 Preserves the original request (POST)
|
|
|
|
return redirect("/login", code=307)
|
|
|
|
|
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
# SIGN OUT FUNCTIONALITY
|
2024-01-05 18:20:56 +00:00
|
|
|
@blueprint.route('/logout')
|
|
|
|
def logout():
|
2024-01-22 08:33:28 +00:00
|
|
|
""" Function responsible for handling logouts from the site """
|
2024-01-19 12:37:51 +00:00
|
|
|
# Clear the current user from the session if they are logged in
|
|
|
|
session.pop('user_id', None)
|
2024-01-21 22:06:06 +00:00
|
|
|
return redirect("/")
|