-
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 #36 from davewalker5/flask-app-factory
Flask app factory
- Loading branch information
Showing
17 changed files
with
143 additions
and
146 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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
FROM python:3.10-slim-bullseye AS runtime | ||
|
||
COPY naturerecorderpy-1.0.22.0 /opt/naturerecorderpy-1.0.22.0 | ||
COPY naturerecorderpy-1.0.23.0 /opt/naturerecorderpy-1.0.23.0 | ||
|
||
WORKDIR /opt/naturerecorderpy-1.0.22.0 | ||
WORKDIR /opt/naturerecorderpy-1.0.23.0 | ||
|
||
RUN apt-get update -y | ||
RUN pip install -r requirements.txt | ||
RUN pip install nature_recorder-1.0.22-py3-none-any.whl | ||
RUN pip install nature_recorder-1.0.23-py3-none-any.whl | ||
|
||
ENV NATURE_RECORDER_DATA_FOLDER=/var/opt/naturerecorderpy-1.0.22.0 | ||
ENV NATURE_RECORDER_DB=/var/opt/naturerecorderpy-1.0.22.0/naturerecorder.db | ||
ENV NATURE_RECORDER_DATA_FOLDER=/var/opt/naturerecorderpy-1.0.23.0 | ||
ENV NATURE_RECORDER_DB=/var/opt/naturerecorderpy-1.0.23.0/naturerecorder.db | ||
|
||
ENTRYPOINT [ "python" ] | ||
CMD [ "-m", "naturerec_web" ] | ||
CMD [ "-m", "naturerec_web", "production" ] |
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,5 @@ | ||
home_blueprint.py | ||
================= | ||
|
||
.. automodule:: naturerec_web.home.home_blueprint | ||
:members: |
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
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,3 @@ | ||
UPDATE Sightings | ||
SET Number = NULL | ||
WHERE Number = 0; |
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,5 +1,63 @@ | ||
from .naturerecorder import app | ||
import os | ||
from flask import Flask | ||
from flask_login import LoginManager | ||
from .home import home_bp | ||
from .sightings import sightings_bp | ||
from .export import export_bp | ||
from .locations import locations_bp | ||
from .categories import categories_bp | ||
from .species import species_bp | ||
from .status import status_bp | ||
from .species_ratings import species_ratings_bp | ||
from .life_list import life_list_bp | ||
from .jobs import jobs_bp | ||
from .reports import reports_bp | ||
from .auth import auth_bp | ||
from naturerec_model.logic import get_user | ||
|
||
|
||
def create_app(environment="production"): | ||
""" | ||
Flask Application Factory | ||
:return: An instance of the Flask application | ||
""" | ||
app = Flask("Nature Recorder", | ||
static_folder=os.path.join(os.path.dirname(__file__), "static"), | ||
template_folder=os.path.join(os.path.dirname(__file__), "templates")) | ||
|
||
config_object = f"naturerec_web.config.{'ProductionConfig' if environment == 'production' else 'DevelopmentConfig'}" | ||
app.config.from_object(config_object) | ||
|
||
# Register the blueprints | ||
app.secret_key = os.environ["SECRET_KEY"] | ||
app.register_blueprint(home_bp, url_prefix="") | ||
app.register_blueprint(sightings_bp, url_prefix='/sightings') | ||
app.register_blueprint(export_bp, url_prefix='/export') | ||
app.register_blueprint(locations_bp, url_prefix='/locations') | ||
app.register_blueprint(categories_bp, url_prefix='/categories') | ||
app.register_blueprint(species_bp, url_prefix='/species') | ||
app.register_blueprint(status_bp, url_prefix='/status') | ||
app.register_blueprint(species_ratings_bp, url_prefix='/species_ratings') | ||
app.register_blueprint(life_list_bp, url_prefix='/life_list') | ||
app.register_blueprint(jobs_bp, url_prefix='/jobs') | ||
app.register_blueprint(reports_bp, url_prefix='/reports') | ||
app.register_blueprint(auth_bp, url_prefix='/auth') | ||
|
||
# Create the flask-login user manager | ||
login_manager = LoginManager() | ||
login_manager.login_view = 'auth.login' | ||
login_manager.init_app(app) | ||
|
||
@login_manager.user_loader | ||
def load_user(user_id): | ||
""" | ||
Method that returns a user given their ID | ||
:param user_id: ID of the user to retrieve | ||
:return: Instance of the User class for the specified user | ||
""" | ||
return get_user(int(user_id)) | ||
|
||
return app | ||
|
||
__all__ = [ | ||
"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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import os | ||
from naturerec_web.naturerecorder import app | ||
import sys | ||
from naturerec_web import create_app | ||
|
||
try: | ||
if os.environ["FLASK_ENV"] == "development": | ||
app.run(debug=True, use_reloader=True) | ||
else: | ||
app.run(host="0.0.0.0") | ||
except KeyError: | ||
app.run(host="0.0.0.0") | ||
environment = sys.argv[1] if len(sys.argv) > 1 else "development" | ||
if environment == "development": | ||
create_app(environment).run(debug=True, use_reloader=True) | ||
else: | ||
create_app(environment).run(host="0.0.0.0") |
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,25 @@ | ||
""" | ||
Flask configuration | ||
""" | ||
|
||
import os | ||
from dotenv import load_dotenv | ||
|
||
basedir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | ||
if "NATURE_RECORDER_DATA_FOLDER" in os.environ: | ||
env_file = os.path.join(os.environ["NATURE_RECORDER_DATA_FOLDER"], ".env") | ||
else: | ||
env_file = os.path.join(basedir, "data", ".env") | ||
load_dotenv(env_file) | ||
|
||
|
||
class BaseConfig: | ||
SECRET_KEY = os.environ.get('SECRET_KEY') | ||
|
||
|
||
class ProductionConfig(BaseConfig): | ||
TESTING = False | ||
|
||
|
||
class DevelopmentConfig(BaseConfig): | ||
TESTING = True |
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,5 @@ | ||
from naturerec_web.home.home_blueprint import home_bp | ||
|
||
__all__ = [ | ||
"home_bp" | ||
] |
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,16 @@ | ||
""" | ||
The home blueprint supplies view functions and templates for the site home page | ||
""" | ||
from flask import Blueprint, redirect | ||
|
||
home_bp = Blueprint("home", __name__, template_folder='templates') | ||
|
||
|
||
@home_bp.route("/") | ||
def home(): | ||
""" | ||
Serve the home page for the site | ||
:return: Rendered home page template | ||
""" | ||
return redirect("/sightings/edit") |
This file was deleted.
Oops, something went wrong.
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