Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure Views to Blueprints #8

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions labconnect/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
import os
import logging

# Import Flask modules
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

environment = os.environ.get("CONFIG", "config.TestingConfig")
# from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect

# Create flask app object
app = Flask(__name__)
app.config.from_object(environment)

csrf_protection = CSRFProtect()

# Create Database object
# db = SQLAlchemy(app)

# Import all views
import labconnect.views

def create_app() -> Flask:
# Create flask app object
app = Flask(__name__)
app.config.from_object(os.environ.get("CONFIG", "config.TestingConfig"))

initialize_extensions(app)
register_blueprints(app)

return app


# ----------------
# Helper Functions
# ----------------


def initialize_extensions(app) -> None:
# Since the application instance is now created, pass it to each Flask
# extension instance to bind it to the Flask application instance (app)
# db.init_app(app)
csrf_protection.init_app(app)

# Flask-Login configuration
# from project.models import User

# @login.user_loader
# def load_user(user_id):
# return User.query.filter(User.id == int(user_id)).first()


def register_blueprints(app) -> None:
# Since the application instance is now created, register each Blueprint
# with the Flask application instance (app)
from labconnect.main import main_blueprint
from labconnect.errors import error_blueprint

app.register_blueprint(main_blueprint)
app.register_blueprint(error_blueprint)
9 changes: 9 additions & 0 deletions labconnect/errors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Blueprint for error pages
"""
from flask import Blueprint


error_blueprint = Blueprint("errors", __name__, template_folder="templates")

from . import routes
15 changes: 15 additions & 0 deletions labconnect/errors/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from flask import make_response, render_template, Response

from . import error_blueprint


@error_blueprint.errorhandler(404)
def page_not_found(e) -> Response:
# 404 error page
return make_response(render_template("404.html"), 404)


@error_blueprint.errorhandler(500)
def error_for_server(e) -> Response:
# 500 error page
return make_response(render_template("500.html"), 500)
8 changes: 8 additions & 0 deletions labconnect/main/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
Blueprint for main pages
"""
from flask import Blueprint

main_blueprint = Blueprint("main", __name__, template_folder="templates")

from . import routes
9 changes: 9 additions & 0 deletions labconnect/main/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from flask import render_template, Response

from . import main_blueprint


@main_blueprint.route("/")
def index():
# return "Hello World"
return render_template("home.html")
30 changes: 0 additions & 30 deletions labconnect/views.py

This file was deleted.

4 changes: 3 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from labconnect import app
from labconnect import create_app

app = create_app()

if __name__ == "__main__":
app.run(port=8000)