mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-05 20:51:49 +02:00
Factorize application creation logic
This commit is contained in:
parent
46e3f6eaa2
commit
8229b41bff
3 changed files with 74 additions and 66 deletions
|
@ -5,7 +5,7 @@ from flask_script import Manager, Command
|
||||||
from flask_migrate import Migrate, MigrateCommand
|
from flask_migrate import Migrate, MigrateCommand
|
||||||
from werkzeug.security import generate_password_hash
|
from werkzeug.security import generate_password_hash
|
||||||
|
|
||||||
from ihatemoney.run import app
|
from ihatemoney.run import create_app
|
||||||
from ihatemoney.models import db
|
from ihatemoney.models import db
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,16 +16,15 @@ class GeneratePasswordHash(Command):
|
||||||
password = getpass(prompt='Password: ')
|
password = getpass(prompt='Password: ')
|
||||||
print(generate_password_hash(password))
|
print(generate_password_hash(password))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = create_app()
|
||||||
migrate = Migrate(app, db)
|
migrate = Migrate(app, db)
|
||||||
|
|
||||||
manager = Manager(app)
|
manager = Manager(app)
|
||||||
manager.add_command('db', MigrateCommand)
|
manager.add_command('db', MigrateCommand)
|
||||||
manager.add_command('generate_password_hash', GeneratePasswordHash)
|
manager.add_command('generate_password_hash', GeneratePasswordHash)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
manager.run()
|
manager.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -4,20 +4,22 @@ import warnings
|
||||||
|
|
||||||
from flask import Flask, g, request, session
|
from flask import Flask, g, request, session
|
||||||
from flask_babel import Babel
|
from flask_babel import Babel
|
||||||
|
from flask_mail import Mail
|
||||||
from flask_migrate import Migrate, upgrade, stamp
|
from flask_migrate import Migrate, upgrade, stamp
|
||||||
from raven.contrib.flask import Sentry
|
from raven.contrib.flask import Sentry
|
||||||
|
|
||||||
from ihatemoney.web import main, db, mail
|
|
||||||
from ihatemoney.api import api
|
from ihatemoney.api import api
|
||||||
|
from ihatemoney.models import db
|
||||||
from ihatemoney.utils import PrefixedWSGI
|
from ihatemoney.utils import PrefixedWSGI
|
||||||
from ihatemoney.utils import minimal_round
|
from ihatemoney.web import main as web_interface
|
||||||
|
|
||||||
from ihatemoney import default_settings
|
from ihatemoney import default_settings
|
||||||
|
|
||||||
app = Flask(__name__, instance_path='/etc/ihatemoney', instance_relative_config=True)
|
|
||||||
|
|
||||||
|
def setup_database(app):
|
||||||
|
"""Prepare the database. Create tables, run migrations etc."""
|
||||||
|
|
||||||
def pre_alembic_db():
|
def _pre_alembic_db():
|
||||||
""" Checks if we are migrating from a pre-alembic ihatemoney
|
""" Checks if we are migrating from a pre-alembic ihatemoney
|
||||||
"""
|
"""
|
||||||
con = db.engine.connect()
|
con = db.engine.connect()
|
||||||
|
@ -25,8 +27,23 @@ def pre_alembic_db():
|
||||||
alembic_setup = db.engine.dialect.has_table(con, 'alembic_version')
|
alembic_setup = db.engine.dialect.has_table(con, 'alembic_version')
|
||||||
return tables_exist and not alembic_setup
|
return tables_exist and not alembic_setup
|
||||||
|
|
||||||
|
db.init_app(app)
|
||||||
|
db.app = app
|
||||||
|
|
||||||
def configure():
|
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):
|
||||||
""" A way to (re)configure the app, specially reset the settings
|
""" A way to (re)configure the app, specially reset the settings
|
||||||
"""
|
"""
|
||||||
default_config_file = os.path.join(app.root_path, 'default_settings.py')
|
default_config_file = os.path.join(app.root_path, 'default_settings.py')
|
||||||
|
@ -43,6 +60,9 @@ def configure():
|
||||||
app.config.from_pyfile('ihatemoney.cfg', silent=True)
|
app.config.from_pyfile('ihatemoney.cfg', silent=True)
|
||||||
app.wsgi_app = PrefixedWSGI(app)
|
app.wsgi_app = PrefixedWSGI(app)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_configuration(app):
|
||||||
|
|
||||||
if app.config['SECRET_KEY'] == default_settings.SECRET_KEY:
|
if app.config['SECRET_KEY'] == default_settings.SECRET_KEY:
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
"Running a server without changing the SECRET_KEY can lead to"
|
"Running a server without changing the SECRET_KEY can lead to"
|
||||||
|
@ -57,8 +77,8 @@ def configure():
|
||||||
+ " and will be removed in further version",
|
+ " and will be removed in further version",
|
||||||
UserWarning
|
UserWarning
|
||||||
)
|
)
|
||||||
if not 'MAIL_DEFAULT_SENDER' in app.config:
|
if 'MAIL_DEFAULT_SENDER' not in app.config:
|
||||||
app.config['MAIL_DEFAULT_SENDER'] = DEFAULT_MAIL_SENDER
|
app.config['MAIL_DEFAULT_SENDER'] = default_settings.DEFAULT_MAIL_SENDER
|
||||||
|
|
||||||
if "pbkdf2:sha256:" not in app.config['ADMIN_PASSWORD'] and app.config['ADMIN_PASSWORD']:
|
if "pbkdf2:sha256:" not in app.config['ADMIN_PASSWORD'] and app.config['ADMIN_PASSWORD']:
|
||||||
# Since 2.0
|
# Since 2.0
|
||||||
|
@ -71,41 +91,28 @@ def configure():
|
||||||
UserWarning
|
UserWarning
|
||||||
)
|
)
|
||||||
|
|
||||||
configure()
|
|
||||||
|
|
||||||
|
def create_app(instance_path='/etc/ihatemoney'):
|
||||||
app.register_blueprint(main)
|
app = Flask(__name__, instance_path=instance_path,
|
||||||
|
instance_relative_config=True)
|
||||||
|
load_configuration(app)
|
||||||
|
validate_configuration(app)
|
||||||
|
app.register_blueprint(web_interface)
|
||||||
app.register_blueprint(api)
|
app.register_blueprint(api)
|
||||||
|
|
||||||
# custom jinja2 filters
|
# Configure the application
|
||||||
app.jinja_env.filters['minimal_round'] = minimal_round
|
setup_database(app)
|
||||||
|
|
||||||
# db
|
mail = Mail()
|
||||||
db.init_app(app)
|
|
||||||
db.app = app
|
|
||||||
|
|
||||||
# db migrations
|
|
||||||
migrate = 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)
|
|
||||||
|
|
||||||
# mail
|
|
||||||
mail.init_app(app)
|
mail.init_app(app)
|
||||||
|
app.mail = mail
|
||||||
|
|
||||||
# translations
|
# Error reporting
|
||||||
|
Sentry(app)
|
||||||
|
|
||||||
|
# Translations
|
||||||
babel = Babel(app)
|
babel = Babel(app)
|
||||||
|
|
||||||
# sentry
|
|
||||||
sentry = Sentry(app)
|
|
||||||
|
|
||||||
@babel.localeselector
|
@babel.localeselector
|
||||||
def get_locale():
|
def get_locale():
|
||||||
# get the lang from the session if defined, fallback on the browser "accept
|
# get the lang from the session if defined, fallback on the browser "accept
|
||||||
|
@ -114,7 +121,11 @@ def get_locale():
|
||||||
setattr(g, 'lang', lang)
|
setattr(g, 'lang', lang)
|
||||||
return lang
|
return lang
|
||||||
|
|
||||||
|
return app
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
app = create_app()
|
||||||
app.run(host="0.0.0.0", debug=True)
|
app.run(host="0.0.0.0", debug=True)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -13,10 +13,9 @@ from flask import (
|
||||||
Blueprint, current_app, flash, g, redirect, render_template, request,
|
Blueprint, current_app, flash, g, redirect, render_template, request,
|
||||||
session, url_for, send_file
|
session, url_for, send_file
|
||||||
)
|
)
|
||||||
from flask_mail import Mail, Message
|
from flask_mail import Message
|
||||||
from flask_babel import get_locale, gettext as _
|
from flask_babel import get_locale, gettext as _
|
||||||
from werkzeug.security import generate_password_hash, \
|
from werkzeug.security import check_password_hash
|
||||||
check_password_hash
|
|
||||||
from smtplib import SMTPRecipientsRefused
|
from smtplib import SMTPRecipientsRefused
|
||||||
import werkzeug
|
import werkzeug
|
||||||
from sqlalchemy import orm
|
from sqlalchemy import orm
|
||||||
|
@ -31,7 +30,6 @@ from ihatemoney.forms import (
|
||||||
from ihatemoney.utils import Redirect303, list_of_dicts2json, list_of_dicts2csv
|
from ihatemoney.utils import Redirect303, list_of_dicts2json, list_of_dicts2csv
|
||||||
|
|
||||||
main = Blueprint("main", __name__)
|
main = Blueprint("main", __name__)
|
||||||
mail = Mail()
|
|
||||||
|
|
||||||
|
|
||||||
def requires_admin(f):
|
def requires_admin(f):
|
||||||
|
@ -337,7 +335,7 @@ def invite():
|
||||||
body=message_body,
|
body=message_body,
|
||||||
recipients=[email.strip()
|
recipients=[email.strip()
|
||||||
for email in form.emails.data.split(",")])
|
for email in form.emails.data.split(",")])
|
||||||
mail.send(msg)
|
current_app.mail.send(msg)
|
||||||
flash(_("Your invitations have been sent"))
|
flash(_("Your invitations have been sent"))
|
||||||
return redirect(url_for(".list_bills"))
|
return redirect(url_for(".list_bills"))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue