Commit c2a01bd27a14f618fd72ad5f4b291e5e06fc59de

Authored by Antoine Goutenoir
1 parent f3124890
Exists in master

Prepare mail flask extension.

flaskr/__init__.py
1 1 #! ../venv/bin/python
2 2 import os
3   -from dotenv import load_dotenv, find_dotenv
4   -load_dotenv(find_dotenv())
  3 +from markdown import markdown
  4 +from dotenv import load_dotenv, find_dotenv, dotenv_values
  5 +# Load config from .env ; do this before local libs (and flask)
  6 +load_dotenv(find_dotenv(), override=True) # write into OS environment
  7 +local_env = dotenv_values(find_dotenv()) # load it as well to inject it later
5 8  
6 9  
7 10 from flask import Flask, url_for
... ... @@ -12,25 +15,22 @@ from webassets.loaders import PythonLoader as PythonAssetsLoader
12 15 from flaskr import assets
13 16 from flaskr.models import db, Estimation, EstimationView
14 17 from flaskr.controllers.main_controller import main
15   -
16 18 from flaskr.extensions import (
17 19 admin,
18   - cache,
19 20 assets_env,
  21 + basic_auth,
  22 + cache,
20 23 debug_toolbar,
21 24 login_manager,
22   - basic_auth
  25 + mail,
23 26 )
24   -
25 27 from flaskr.content import content
26 28 from flaskr.core import increment_hit_counter, get_hit_counter
27 29  
28   -from markdown import markdown
29   -
30 30  
31 31 def create_app(object_name):
32 32 """
33   - An flask application factory, as explained here:
  33 + A flask application factory, as explained here:
34 34 http://flask.pocoo.org/docs/patterns/appfactories/
35 35  
36 36 Arguments:
... ... @@ -46,12 +46,16 @@ def create_app(object_name):
46 46  
47 47 # Load configuration
48 48 app.config.from_object(object_name)
49   -
  49 + if local_env:
  50 + app.config.update(**local_env)
  51 + else:
  52 + pass # FIXME
50 53 app.config['BASIC_AUTH_USERNAME'] = os.getenv('ADMIN_USERNAME')
51 54 app.config['BASIC_AUTH_PASSWORD'] = os.getenv('ADMIN_PASSWORD')
52 55  
53 56 # Initialize
54 57 cache.init_app(app)
  58 + mail.init_app(app)
55 59 debug_toolbar.init_app(app)
56 60 db.init_app(app)
57 61 login_manager.init_app(app)
... ... @@ -65,11 +69,10 @@ def create_app(object_name):
65 69 for name, bundle in assets_loader.load_bundles().items():
66 70 assets_env.register(name, bundle)
67 71  
68   - # register our blueprints
  72 + # Register our blueprints
69 73 app.register_blueprint(main)
70 74  
71   -
72   - # VERSION (move to version.py is necessary)
  75 + # VERSION (move to version.py ?)
73 76 version = "0.0.0"
74 77 with open('VERSION', 'r') as version_file:
75 78 version = version_file.read().strip()
... ...
flaskr/controllers/main_controller.py
... ... @@ -10,7 +10,9 @@ from os.path import join
10 10  
11 11 from os import unlink
12 12  
13   -from flaskr.extensions import cache, basic_auth
  13 +from flask_mail import Message
  14 +
  15 +from flaskr.extensions import cache, basic_auth, mail
14 16 from flaskr.forms import LoginForm, EstimateForm
15 17 from flaskr.models import db, User, Estimation, StatusEnum, ScenarioEnum
16 18 from flaskr.geocoder import CachedGeocoder
... ... @@ -736,8 +738,21 @@ def get_scaling_laws_csv():
736 738  
737 739  
738 740 @main.route("/test")
739   -@basic_auth.required
  741 +# @basic_auth.required
740 742 def dev_test():
741 743 import os
742 744  
743   - return os.getenv('ADMIN_USERNAME')
  745 + # return os.getenv('ADMIN_USERNAME')
  746 +
  747 +
  748 +def send_email(to_recipient, subject, message):
  749 + try:
  750 + msg = Message(
  751 + subject=subject,
  752 + html=message,
  753 + sender="bot-noreply@travel-carbon-footprint.irap.omp.eu",
  754 + recipients=[to_recipient],
  755 + )
  756 + mail.send(msg)
  757 + except:
  758 + pass
... ...
flaskr/extensions.py
... ... @@ -3,22 +3,29 @@ from flask_basicauth import BasicAuth
3 3 from flask_caching import Cache
4 4 from flask_debugtoolbar import DebugToolbarExtension
5 5 from flask_login import LoginManager
6   -from flask_assets import Environment
  6 +from flask_assets import Environment as Assets
  7 +from flask_mail import Mail
7 8  
8 9 from flaskr.models import User
9 10  
10 11 # Setup flask cache
11 12 cache = Cache()
12 13  
13   -# init flask assets
14   -assets_env = Environment()
  14 +# Init flask assets
  15 +assets_env = Assets()
15 16  
  17 +# Mail handler
  18 +mail = Mail()
  19 +
  20 +# Debug toolbar for easy dev (disabled in prod)
16 21 debug_toolbar = DebugToolbarExtension()
17 22  
  23 +# Basic auth
18 24 login_manager = LoginManager()
19 25 login_manager.login_view = "main.login"
20 26 login_manager.login_message_category = "warning"
21 27  
  28 +# Admin backoffice
22 29 admin = Admin()
23 30 basic_auth = BasicAuth()
24 31  
... ...
requirements.txt
... ... @@ -7,6 +7,7 @@ Flask-BasicAuth==0.2.0
7 7 Flask-Caching==1.7.2
8 8 Flask-DebugToolbar==0.10.0
9 9 Flask-Login==0.4.0
  10 +Flask-Mail==0.9.1
10 11 Flask-Script==2.0.5
11 12 Flask-SQLAlchemy==2.1
12 13 Flask-WTF==0.14
... ...