2023-12-27 21:36:53 +00:00
|
|
|
from . import blueprint
|
2024-01-02 22:22:14 +00:00
|
|
|
from flask import render_template, redirect, request, session
|
2024-01-01 20:19:57 +00:00
|
|
|
from controllers.database.user import UserController
|
|
|
|
from models.users.customer import Customer
|
2024-01-01 23:13:09 +00:00
|
|
|
from hashlib import sha512
|
2023-12-27 21:36:53 +00:00
|
|
|
|
2023-12-31 19:03:29 +00:00
|
|
|
|
|
|
|
# Function responsible for displaying the main landing page of the site
|
2023-12-27 21:36:53 +00:00
|
|
|
@blueprint.route('/')
|
|
|
|
def welcome_page():
|
2024-01-02 22:22:14 +00:00
|
|
|
return render_template('index.html', content="content.html", user = session.get('user'))
|
2023-12-31 19:03:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### LOGIN FUNCTIONALITY
|
|
|
|
# Function responsible for delivering the Login page for the site
|
|
|
|
@blueprint.route('/login')
|
|
|
|
def display_login():
|
2024-01-02 22:22:14 +00:00
|
|
|
return render_template('index.html', content="login.html", user = session.get('user'))
|
2023-12-31 19:03:29 +00:00
|
|
|
|
|
|
|
# Function responsible for handling logins to the site
|
|
|
|
@blueprint.post('/login')
|
|
|
|
def login():
|
2024-01-02 22:22:14 +00:00
|
|
|
database = UserController()
|
|
|
|
user = database.read(request.form['username'])
|
|
|
|
|
|
|
|
# No user found
|
|
|
|
if user == None:
|
|
|
|
return redirect("/login")
|
|
|
|
|
|
|
|
# Incorrect Password
|
|
|
|
if sha512(request.form['password'].encode()).hexdigest() != user.password:
|
|
|
|
return redirect("/login")
|
|
|
|
|
|
|
|
session['user'] = user.username
|
2023-12-31 19:03:29 +00:00
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
|
|
|
|
### SIGNUP FUNCTIONALITY
|
|
|
|
# Function responsible for delivering the Signup page for the site
|
|
|
|
@blueprint.route('/signup')
|
|
|
|
def display_signup():
|
2024-01-02 22:22:14 +00:00
|
|
|
return render_template('index.html', content="signup.html", user = session.get('user'))
|
2023-12-31 19:03:29 +00:00
|
|
|
|
|
|
|
# Function responsible for handling signups to the site
|
|
|
|
@blueprint.post('/signup')
|
|
|
|
def signup():
|
2024-01-01 20:19:57 +00:00
|
|
|
database = UserController()
|
2024-01-01 23:13:09 +00:00
|
|
|
database.create(Customer(
|
|
|
|
0,
|
|
|
|
request.form['username'],
|
2024-01-02 22:22:14 +00:00
|
|
|
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
|
2024-01-01 23:13:09 +00:00
|
|
|
request.form['firstname'],
|
|
|
|
request.form['lastname'],
|
2024-01-02 22:22:14 +00:00
|
|
|
request.form['email'],
|
2024-01-01 23:13:09 +00:00
|
|
|
"123",
|
|
|
|
"Customer"
|
|
|
|
))
|
2024-01-01 20:19:57 +00:00
|
|
|
|
2024-01-02 22:22:14 +00:00
|
|
|
# Code 307 Preserves the original request (POST)
|
|
|
|
return redirect("/login", code=307)
|
|
|
|
|
|
|
|
|
|
|
|
# Function responsible for handling logouts from the site
|
|
|
|
@blueprint.route('/logout')
|
|
|
|
def logout():
|
|
|
|
session.pop('user')
|
|
|
|
return redirect("/")
|