add auto import all routes

This commit is contained in:
William Bell committed 2026-02-17 16:34:50 +00:00
1 parent ff0d434b55
commit 4b1da83dd2
9 files changed
+58 -52

No files matched your search

+20 -4
View File
@@ -2,9 +2,25 @@ import sqlite3
from datetime import datetime
database = sqlite3.connect("database/species.db")
database.execute(
"""
CREATE TABLE IF NOT EXISTS species (
species_id INTEGER PRIMARY KEY AUTOINCREMENT,
species_english TEXT,
species_latin TEXT,
body_text TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
database.commit()
print("Database initialized.")
def add_species(english, latin, body):
connection = sqlite3.connect("database/species.db")
cursor = connection.cursor()
cursor = database.cursor()
cursor.execute(
"""
@@ -19,8 +35,8 @@ def add_species(english, latin, body):
(english, latin, body, datetime.now()),
)
connection.commit()
connection.close()
cursor.close()
database.commit()
print("Species added successfully.")
-21
View File
@@ -1,21 +0,0 @@
import sqlite3
connection = sqlite3.connect("database/species.db")
cursor = connection.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS species (
species_id INTEGER PRIMARY KEY AUTOINCREMENT,
species_english TEXT,
species_latin TEXT,
body_text TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
connection.commit()
connection.close()
print("Database initialized.")
+30 -2
View File
@@ -1,9 +1,37 @@
from flask import Flask
from routes.homepage import home_page
import os
import importlib.util
def import_pages(route, app):
for root, _, files in os.walk(route):
for file in files:
if not file.endswith(".py"):
continue
path = os.path.join(root, file)
module_name = os.path.splitext(os.path.relpath(path, route))[0]
module_name = module_name.replace(os.sep, ".")
spec = importlib.util.spec_from_file_location(module_name, path)
if spec is None or spec.loader is None:
continue
module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
except Exception as e:
print(f"Failed to import {path}: {e}")
continue
if hasattr(module, "page"):
print(f"{module_name}.page =", module.page)
app.register_blueprint(module.page)
app = Flask(__name__, static_folder='static', static_url_path='')
app.register_blueprint(home_page)
import_pages("routes", app)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=3000, debug=True)
+2 -2
View File
@@ -1,8 +1,8 @@
from flask import render_template, Blueprint
donation_page = Blueprint('donation', __name__, url_prefix='/donation')
page = Blueprint('donation', __name__, url_prefix='/donation')
@donation_page.route('/')
@page.route('/')
def home():
return render_template('donation/donation.jinja')
+2 -2
View File
@@ -1,8 +1,8 @@
from flask import render_template, Blueprint
error_page = Blueprint('error', __name__, url_prefix='/error')
page = Blueprint('error', __name__, url_prefix='/error')
@error_page.route('/')
@page.route('/')
def error():
return render_template('error.jinja')
-8
View File
@@ -1,8 +0,0 @@
from flask import render_template, request, Blueprint
example_page = Blueprint("example", __name__, url_prefix="/example")
@home_page.route("/")
def home():
return render_template("example/example.jinja")
-3
View File
@@ -1,3 +0,0 @@
# route template
to split up the project into seperate files, we will seperate the webpages back end code into seperate `routes`. use `template.py` to help with creating a new route.
+2 -8
View File
@@ -1,14 +1,8 @@
from flask import render_template, Blueprint
from routes.donation.donation import donation_page
from routes.species_info.species_info import species_info_page
from routes.error.error import error_page
home_page = Blueprint("home", __name__, url_prefix="/")
home_page.register_blueprint(donation_page)
home_page.register_blueprint(species_info_page)
home_page.register_blueprint(error_page)
page = Blueprint("home", __name__, url_prefix="/")
@home_page.route("/")
@page.route("/")
def home():
return render_template("home/home.jinja")
+2 -2
View File
@@ -1,7 +1,7 @@
from flask import render_template, Blueprint
import sqlite3
species_info_page = Blueprint("species_information", __name__)
page = Blueprint("species_information", __name__)
def animal_from_database(species_id):
@@ -40,7 +40,7 @@ def animal_from_database(species_id):
# /static/images/species_database/{{species_id}}.jpg
@species_info_page.route("/species/<species_id>")
@page.route("/species/<species_id>")
def data(species_id):
try:
database_entry = animal_from_database(species_id)