From 9b29ac47c13b642a3dab39c2af1d94b73e449eee Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Thu, 28 Sep 2023 21:46:42 -0700 Subject: [PATCH] initial commit --- .gitignore | 13 +++++++++ config.py | 9 +++++++ pyvenv.cfg | 5 ++++ src/__init__.py | 19 +++++++++++++ src/auth/__init__.py | 5 ++++ src/auth/handlers.py | 27 +++++++++++++++++++ src/errors/__init__.py | 5 ++++ src/errors/handlers.py | 14 ++++++++++ src/main/__init__.py | 5 ++++ src/main/routes.py | 6 +++++ src/templates/dashboard.html | 12 +++++++++ src/templates/errors/403.html | 7 +++++ src/templates/errors/404.html | 7 +++++ src/templates/errors/500.html | 7 +++++ src/templates/index.html | 8 ++++++ src/templates/layout.html | 50 +++++++++++++++++++++++++++++++++++ src/templates/login.html | 17 ++++++++++++ 17 files changed, 216 insertions(+) create mode 100644 .gitignore create mode 100644 config.py create mode 100644 pyvenv.cfg create mode 100644 src/__init__.py create mode 100644 src/auth/__init__.py create mode 100644 src/auth/handlers.py create mode 100644 src/errors/__init__.py create mode 100644 src/errors/handlers.py create mode 100644 src/main/__init__.py create mode 100644 src/main/routes.py create mode 100644 src/templates/dashboard.html create mode 100644 src/templates/errors/403.html create mode 100644 src/templates/errors/404.html create mode 100644 src/templates/errors/500.html create mode 100644 src/templates/index.html create mode 100644 src/templates/layout.html create mode 100644 src/templates/login.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d468bc3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# Environment / Virtual Environment files +.env +.venv +bin +include/ +lib/ +lib64 +lib64/ + +# Pycache +__pycache__ +__pycache__/ +*/__pycache__/ diff --git a/config.py b/config.py new file mode 100644 index 0000000..9ba264e --- /dev/null +++ b/config.py @@ -0,0 +1,9 @@ +import os +from dotenv import load_dotenv + +basedir = os.path.abspath(os.path.dirname(__file__)) +load_dotenv() + +class Config: + SECRET: str = os.environ.get("SECRET") + API_URL: str = os.environ.get("API_URL") diff --git a/pyvenv.cfg b/pyvenv.cfg new file mode 100644 index 0000000..48fd867 --- /dev/null +++ b/pyvenv.cfg @@ -0,0 +1,5 @@ +home = /usr/bin +include-system-site-packages = false +version = 3.11.4 +executable = /usr/bin/python3.11 +command = /usr/bin/python3.11 -m venv /home/flami/Desktop/Projects/DeviceBooking diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..25528ea --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,19 @@ +from flask import Flask +from config import Config + +def create_app(config_class=Config): + app: Flask = Flask(__name__) + app.config.from_object(config_class) + + # Initialize flask extensions here + + # Register blueprints here + from src.main import main_blueprint as main_bp + from src.errors import errors_blueprint as errors_bp + from src.auth import authentication_blueprint as auth_bp + + app.register_blueprint(main_bp) + app.register_blueprint(errors_bp) + app.register_blueprint(auth_bp) + + return app diff --git a/src/auth/__init__.py b/src/auth/__init__.py new file mode 100644 index 0000000..a2076ae --- /dev/null +++ b/src/auth/__init__.py @@ -0,0 +1,5 @@ +from flask import Blueprint + +authentication_blueprint: Blueprint = Blueprint('auth', __name__) + +from src.auth import handlers \ No newline at end of file diff --git a/src/auth/handlers.py b/src/auth/handlers.py new file mode 100644 index 0000000..7fb648a --- /dev/null +++ b/src/auth/handlers.py @@ -0,0 +1,27 @@ +from src.auth import authentication_blueprint +from flask import render_template + +@authentication_blueprint.post("/logout") +def logout(): + session.pop('user_id', None) + return redirect(url_for('login'), code=200) + +@authentication_blueprint.get('/dashboard') +def dashboard(): + if 'user_id' not in session: return redirect(url_for("login"), code=401) + return render_template('dashboard.html', user_id=session['user_id']) + +@authentication_blueprint.get('/login') +def login_get(): + token = request.cookies.get('token') + if token == '123456': return redirect(url_for("dashboard"), code=200) + return render_template('login.html', error=None) + +@authentication_blueprint.post('/login') +def login_post(): + if valid_login(request.form['user_id'], + request.form['password']): + session['user_id'] = request.form['user_id'] + return redirect(url_for("dashboard"), code=200) + + return render_template('login.html', error='Invalid User ID or Password') diff --git a/src/errors/__init__.py b/src/errors/__init__.py new file mode 100644 index 0000000..01769ef --- /dev/null +++ b/src/errors/__init__.py @@ -0,0 +1,5 @@ +from flask import Blueprint + +errors_blueprint = Blueprint('errors', __name__) + +from src.errors import handlers diff --git a/src/errors/handlers.py b/src/errors/handlers.py new file mode 100644 index 0000000..c841531 --- /dev/null +++ b/src/errors/handlers.py @@ -0,0 +1,14 @@ +from flask import render_template +from src.errors import errors_blueprint + +@errors_blueprint.app_errorhandler(404) +def error_404(error): + return render_template('errors/404.html'), 404 + +@errors_blueprint.app_errorhandler(403) +def error_403(error): + return render_template('errors/403.html'), 403 + +@errors_blueprint.app_errorhandler(500) +def error_500(error): + return render_template('errors/500.html'), 500 diff --git a/src/main/__init__.py b/src/main/__init__.py new file mode 100644 index 0000000..f707a7e --- /dev/null +++ b/src/main/__init__.py @@ -0,0 +1,5 @@ +from flask import Blueprint + +main_blueprint: Blueprint = Blueprint('main', __name__) + +from src.main import routes diff --git a/src/main/routes.py b/src/main/routes.py new file mode 100644 index 0000000..0cec189 --- /dev/null +++ b/src/main/routes.py @@ -0,0 +1,6 @@ +from src.main import main_blueprint +from flask import render_template + +@main_blueprint.get("/") +def index(): + return render_template('index.html') diff --git a/src/templates/dashboard.html b/src/templates/dashboard.html new file mode 100644 index 0000000..c57728e --- /dev/null +++ b/src/templates/dashboard.html @@ -0,0 +1,12 @@ + + + + DeviceBooking Dashboard + + +

Logged in as user {{ user_id }}

+
+ +
+ + diff --git a/src/templates/errors/403.html b/src/templates/errors/403.html new file mode 100644 index 0000000..fa9e1b7 --- /dev/null +++ b/src/templates/errors/403.html @@ -0,0 +1,7 @@ +{% extends 'layout.html' %} +{% block content %} +
+

You don't have permission to do that(403)

+

Please check your account and try again

+
+{% endblock %} \ No newline at end of file diff --git a/src/templates/errors/404.html b/src/templates/errors/404.html new file mode 100644 index 0000000..0eed715 --- /dev/null +++ b/src/templates/errors/404.html @@ -0,0 +1,7 @@ +{% extends 'layout.html' %} +{% block content %} +
+

Oops. Page Not Found (404)

+

That page does not exist. Please try a different location

+
+{% endblock %} \ No newline at end of file diff --git a/src/templates/errors/500.html b/src/templates/errors/500.html new file mode 100644 index 0000000..7bd694e --- /dev/null +++ b/src/templates/errors/500.html @@ -0,0 +1,7 @@ +{% extends 'layout.html' %} +{% block content %} +
+

Oops. Something went wrong (500)

+

We're experienceing some trouble on our end. Please try again in the near future

+
+{% endblock %} \ No newline at end of file diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..4584f29 --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,8 @@ +{% extends 'layout.html' %} + +{% block content %} +

{% block title %} The Home Page of the Device Reservation System {% endblock %}

+
+

Welcome to the Device Reservation System

+
+{% endblock %} \ No newline at end of file diff --git a/src/templates/layout.html b/src/templates/layout.html new file mode 100644 index 0000000..0ef92af --- /dev/null +++ b/src/templates/layout.html @@ -0,0 +1,50 @@ + + + + + {% block title %} {% endblock %} - DRS + + + + +
+
+ {% block content %} {% endblock %} +
+ + diff --git a/src/templates/login.html b/src/templates/login.html new file mode 100644 index 0000000..817f69d --- /dev/null +++ b/src/templates/login.html @@ -0,0 +1,17 @@ + + + + DeviceBooking Login + + +

Login

+ {% if error %} +

{{ error }}

+ {% endif %} +
+ + + +
+ +