diff --git a/flaskr/__init__.py b/flaskr/__init__.py index d01681d..4bf2e56 100755 --- a/flaskr/__init__.py +++ b/flaskr/__init__.py @@ -1,7 +1,10 @@ #! ../venv/bin/python import os -from dotenv import load_dotenv, find_dotenv -load_dotenv(find_dotenv()) +from markdown import markdown +from dotenv import load_dotenv, find_dotenv, dotenv_values +# Load config from .env ; do this before local libs (and flask) +load_dotenv(find_dotenv(), override=True) # write into OS environment +local_env = dotenv_values(find_dotenv()) # load it as well to inject it later from flask import Flask, url_for @@ -12,25 +15,22 @@ from webassets.loaders import PythonLoader as PythonAssetsLoader from flaskr import assets from flaskr.models import db, Estimation, EstimationView from flaskr.controllers.main_controller import main - from flaskr.extensions import ( admin, - cache, assets_env, + basic_auth, + cache, debug_toolbar, login_manager, - basic_auth + mail, ) - from flaskr.content import content from flaskr.core import increment_hit_counter, get_hit_counter -from markdown import markdown - def create_app(object_name): """ - An flask application factory, as explained here: + A flask application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/ Arguments: @@ -46,12 +46,16 @@ def create_app(object_name): # Load configuration app.config.from_object(object_name) - + if local_env: + app.config.update(**local_env) + else: + pass # FIXME app.config['BASIC_AUTH_USERNAME'] = os.getenv('ADMIN_USERNAME') app.config['BASIC_AUTH_PASSWORD'] = os.getenv('ADMIN_PASSWORD') # Initialize cache.init_app(app) + mail.init_app(app) debug_toolbar.init_app(app) db.init_app(app) login_manager.init_app(app) @@ -65,11 +69,10 @@ def create_app(object_name): for name, bundle in assets_loader.load_bundles().items(): assets_env.register(name, bundle) - # register our blueprints + # Register our blueprints app.register_blueprint(main) - - # VERSION (move to version.py is necessary) + # VERSION (move to version.py ?) version = "0.0.0" with open('VERSION', 'r') as version_file: version = version_file.read().strip() diff --git a/flaskr/controllers/main_controller.py b/flaskr/controllers/main_controller.py index 38a715b..ea4de54 100644 --- a/flaskr/controllers/main_controller.py +++ b/flaskr/controllers/main_controller.py @@ -10,7 +10,9 @@ from os.path import join from os import unlink -from flaskr.extensions import cache, basic_auth +from flask_mail import Message + +from flaskr.extensions import cache, basic_auth, mail from flaskr.forms import LoginForm, EstimateForm from flaskr.models import db, User, Estimation, StatusEnum, ScenarioEnum from flaskr.geocoder import CachedGeocoder @@ -736,8 +738,21 @@ def get_scaling_laws_csv(): @main.route("/test") -@basic_auth.required +# @basic_auth.required def dev_test(): import os - return os.getenv('ADMIN_USERNAME') + # return os.getenv('ADMIN_USERNAME') + + +def send_email(to_recipient, subject, message): + try: + msg = Message( + subject=subject, + html=message, + sender="bot-noreply@travel-carbon-footprint.irap.omp.eu", + recipients=[to_recipient], + ) + mail.send(msg) + except: + pass diff --git a/flaskr/extensions.py b/flaskr/extensions.py index 6edc215..5d2db95 100644 --- a/flaskr/extensions.py +++ b/flaskr/extensions.py @@ -3,22 +3,29 @@ from flask_basicauth import BasicAuth from flask_caching import Cache from flask_debugtoolbar import DebugToolbarExtension from flask_login import LoginManager -from flask_assets import Environment +from flask_assets import Environment as Assets +from flask_mail import Mail from flaskr.models import User # Setup flask cache cache = Cache() -# init flask assets -assets_env = Environment() +# Init flask assets +assets_env = Assets() +# Mail handler +mail = Mail() + +# Debug toolbar for easy dev (disabled in prod) debug_toolbar = DebugToolbarExtension() +# Basic auth login_manager = LoginManager() login_manager.login_view = "main.login" login_manager.login_message_category = "warning" +# Admin backoffice admin = Admin() basic_auth = BasicAuth() diff --git a/requirements.txt b/requirements.txt index 1cc998f..a5eb6d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ Flask-BasicAuth==0.2.0 Flask-Caching==1.7.2 Flask-DebugToolbar==0.10.0 Flask-Login==0.4.0 +Flask-Mail==0.9.1 Flask-Script==2.0.5 Flask-SQLAlchemy==2.1 Flask-WTF==0.14 -- libgit2 0.21.2