aboutsummaryrefslogtreecommitdiff
path: root/ihatemoney/run.py
diff options
context:
space:
mode:
authorAlexis Metaireau <alexis@notmyidea.org>2017-07-07 00:06:56 +0200
committerGitHub <noreply@github.com>2017-07-07 00:06:56 +0200
commit3a4282fd75e3b3317b2b08b4aa2e6ac154310e73 (patch)
tree9470c907ba1f884246af87d26d55c3aaac6d6dc5 /ihatemoney/run.py
parent0e374cd5e0ef5a9be67084365f91de2ab84f636c (diff)
downloadihatemoney-mirror-3a4282fd75e3b3317b2b08b4aa2e6ac154310e73.zip
ihatemoney-mirror-3a4282fd75e3b3317b2b08b4aa2e6ac154310e73.tar.gz
ihatemoney-mirror-3a4282fd75e3b3317b2b08b4aa2e6ac154310e73.tar.bz2
Absolute imports & some other improvements (#243)
* Use absolute imports and rename package to ihatemoney * Add a ihatemoney command * Factorize application creation logic * Refactor the tests * Update the wsgi.py module with the new create_app() function * Fix some styling thanks to Flake8. * Automate Flake8 check in the CI.
Diffstat (limited to 'ihatemoney/run.py')
-rw-r--r--ihatemoney/run.py144
1 files changed, 144 insertions, 0 deletions
diff --git a/ihatemoney/run.py b/ihatemoney/run.py
new file mode 100644
index 0000000..22cf235
--- /dev/null
+++ b/ihatemoney/run.py
@@ -0,0 +1,144 @@
+import os
+import os.path
+import warnings
+
+from flask import Flask, g, request, session
+from flask_babel import Babel
+from flask_mail import Mail
+from flask_migrate import Migrate, upgrade, stamp
+from raven.contrib.flask import Sentry
+
+from ihatemoney.api import api
+from ihatemoney.models import db
+from ihatemoney.utils import PrefixedWSGI, minimal_round
+from ihatemoney.web import main as web_interface
+
+from ihatemoney import default_settings
+
+
+def setup_database(app):
+ """Prepare the database. Create tables, run migrations etc."""
+
+ def _pre_alembic_db():
+ """ Checks if we are migrating from a pre-alembic ihatemoney
+ """
+ con = db.engine.connect()
+ tables_exist = db.engine.dialect.has_table(con, 'project')
+ alembic_setup = db.engine.dialect.has_table(con, 'alembic_version')
+ return tables_exist and not alembic_setup
+
+ db.init_app(app)
+ db.app = app
+
+ Migrate(app, db)
+ migrations_path = os.path.join(app.root_path, 'migrations')
+
+ if _pre_alembic_db():
+ with app.app_context():
+ # fake the first migration
+ stamp(migrations_path, revision='b9a10d5d63ce')
+
+ # auto-execute migrations on runtime
+ with app.app_context():
+ upgrade(migrations_path)
+
+
+def load_configuration(app, configuration=None):
+ """ Find the right configuration file for the application and load it.
+
+ By order of preference:
+ - Use the IHATEMONEY_SETTINGS_FILE_PATH env var if defined ;
+ - If not, use /etc/ihatemoney/ihatemoney.cfg ;
+ - Otherwise, load the default settings.
+ """
+
+ env_var_config = os.environ.get('IHATEMONEY_SETTINGS_FILE_PATH')
+ app.config.from_object('ihatemoney.default_settings')
+ if configuration:
+ app.config.from_object(configuration)
+ elif env_var_config:
+ app.config.from_pyfile(env_var_config)
+ else:
+ app.config.from_pyfile('ihatemoney.cfg', silent=True)
+
+
+def validate_configuration(app):
+
+ if app.config['SECRET_KEY'] == default_settings.SECRET_KEY:
+ warnings.warn(
+ "Running a server without changing the SECRET_KEY can lead to"
+ + " user impersonation. Please update your configuration file.",
+ UserWarning
+ )
+ # Deprecations
+ if 'DEFAULT_MAIL_SENDER' in app.config:
+ # Since flask-mail 0.8
+ warnings.warn(
+ "DEFAULT_MAIL_SENDER is deprecated in favor of MAIL_DEFAULT_SENDER"
+ + " and will be removed in further version",
+ UserWarning
+ )
+ if 'MAIL_DEFAULT_SENDER' not in app.config:
+ app.config['MAIL_DEFAULT_SENDER'] = default_settings.DEFAULT_MAIL_SENDER
+
+ if "pbkdf2:sha256:" not in app.config['ADMIN_PASSWORD'] and app.config['ADMIN_PASSWORD']:
+ # Since 2.0
+ warnings.warn(
+ "The way Ihatemoney stores your ADMIN_PASSWORD has changed. You are using an unhashed"
+ + " ADMIN_PASSWORD, which is not supported anymore and won't let you access your admin"
+ + " endpoints. Please use the command 'ihatemoney generate_password_hash'"
+ + " to generate a proper password HASH and copy the output to the value of"
+ + " ADMIN_PASSWORD in your settings file.",
+ UserWarning
+ )
+
+
+def create_app(configuration=None, instance_path='/etc/ihatemoney',
+ instance_relative_config=True):
+ app = Flask(
+ __name__,
+ instance_path=instance_path,
+ instance_relative_config=instance_relative_config)
+
+ # If a configuration object is passed, use it. Otherwise try to find one.
+ load_configuration(app, configuration)
+ app.wsgi_app = PrefixedWSGI(app)
+
+ validate_configuration(app)
+ app.register_blueprint(web_interface)
+ app.register_blueprint(api)
+
+ # Configure the application
+ setup_database(app)
+
+ mail = Mail()
+ mail.init_app(app)
+ app.mail = mail
+
+ # Error reporting
+ Sentry(app)
+
+ # Jinja filters
+ app.jinja_env.filters['minimal_round'] = minimal_round
+
+ # Translations
+ babel = Babel(app)
+
+ @babel.localeselector
+ def get_locale():
+ # get the lang from the session if defined, fallback on the browser "accept
+ # languages" header.
+ lang = session.get('lang', request.accept_languages.best_match(['fr', 'en']))
+ setattr(g, 'lang', lang)
+ return lang
+
+ return app
+
+
+def main():
+ app = create_app()
+ app.run(host="0.0.0.0", debug=True)
+
+
+if __name__ == '__main__':
+ main()