-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from DeepBlockDeepak/add-config-file
Add config file
- Loading branch information
Showing
8 changed files
with
117 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
db = SQLAlchemy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,36 @@ | ||
import os | ||
|
||
from flask import Flask, render_template | ||
from flask import Flask | ||
from flask_login import LoginManager | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
# attempting to create/read/write the .db in the top project directory, whether on replit or local | ||
cwd = os.getcwd() | ||
|
||
|
||
# use the following as a second paramter to dictate where you want the database to exist. Default is /instance | ||
# ,instance_path='/home/runner/computroniumflaskapp' | ||
app = Flask(__name__, instance_path=cwd) | ||
from src.config import Config | ||
from database import db # import the db instance | ||
from src.views import welcome_page, not_found | ||
|
||
# create login_manager and initialize login_manager here: | ||
login_manager = LoginManager() | ||
login_manager.init_app(app) | ||
# testing to see whether unlogged in viewers are auto-sent to the register page | ||
# @BUG -> This auto-routing to /register doesn't work | ||
login_manager.login_view = "register" | ||
|
||
# set the SQLALCHEMY_DATABASE_URI key | ||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///travel_library.db" | ||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False | ||
app.config["SECRET_KEY"] = "i-dont-know" | ||
# create an SQLAlchemy object named `db` and bind it to your app | ||
db = SQLAlchemy(app) | ||
def create_app(): | ||
# Initialize Flask app | ||
app = Flask(__name__) | ||
app.config.from_object(Config) # Load configuration from config.py | ||
|
||
# Initialize SQLAlchemy with the app | ||
db.init_app(app) | ||
|
||
# a simple initial greeting | ||
@app.route("/") | ||
@app.route("/index") | ||
@app.route("/home") | ||
def welcome_page(): | ||
# render a login page before entering this page! | ||
return render_template("welcome_page.html") | ||
# Initialize Flask-Login manager | ||
login_manager = LoginManager() | ||
login_manager.init_app(app) | ||
login_manager.login_view = "login" # Specify the route for unauthenticated users | ||
|
||
# Load user function for Flask-Login | ||
from src.models import User | ||
|
||
# app name | ||
@app.errorhandler(404) | ||
def not_found(e): # is this var, e, needed? | ||
return render_template("404.html") | ||
@login_manager.user_loader | ||
def load_user(user_id): | ||
return User.query.get(int(user_id)) | ||
|
||
# Register blueprints, routes, and error handlers | ||
app.add_url_rule("/", "welcome_page", welcome_page) | ||
app.add_url_rule("/index", "welcome_page", welcome_page) | ||
app.add_url_rule("/home", "welcome_page", welcome_page) | ||
|
||
# I still don't get why routes must be imported here!!! | ||
from src.routes import * | ||
# Register error handlers | ||
app.register_error_handler(404, not_found) | ||
|
||
# Need to use this boiler plate so that other functions can be tested in Shell without triggering the Flask App to run | ||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=81) | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from main import create_app | ||
|
||
app = create_app() | ||
|
||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
|
||
|
||
class Config: | ||
"""Base configuration.""" | ||
|
||
# Default secret key | ||
SECRET_KEY = os.getenv("SECRET_KEY", "your-default-secret-key") | ||
|
||
# Default SQLAlchemy settings | ||
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL", "sqlite:///travel_library.db") | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False | ||
|
||
|
||
class DevelopmentConfig(Config): | ||
"""Development configuration.""" | ||
|
||
DEBUG = True | ||
SQLALCHEMY_ECHO = True # If you want to see SQLAlchemy queries in the logs | ||
|
||
|
||
class TestingConfig(Config): | ||
"""Testing configuration.""" | ||
|
||
TESTING = True | ||
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:" # Use in-memory SQLite for tests | ||
|
||
|
||
class ProductionConfig(Config): | ||
"""Production configuration.""" | ||
|
||
DEBUG = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from flask import render_template | ||
|
||
|
||
# Define route for the homepage | ||
def welcome_page(): | ||
# Render the welcome page template | ||
return render_template("welcome_page.html") | ||
|
||
|
||
# Define custom error handler for 404 errors | ||
def not_found(_): # Unused error argument can be indicated with an underscore | ||
# Render the 404 error page template | ||
return render_template("404.html") |