flask_app.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """
  2. """
  3. import os
  4. from flask import Flask
  5. from flask_debugtoolbar import DebugToolbarExtension
  6. from flask.ext.sqlalchemy import SQLAlchemy
  7. from celery import Celery
  8. def make_app():
  9. app = Flask(__name__)
  10. # Load configuration.
  11. conf_file = os.environ.get("CONFIG", None)
  12. if not conf_file:
  13. raise Exception("Missing CONFIG environment variable with the configuration")
  14. app.config.from_pyfile(conf_file)
  15. if app.config['DEBUG']:
  16. app.testing = True
  17. return app
  18. # Celery.
  19. def make_celery(app=None):
  20. if not app:
  21. app = make_app()
  22. celery = Celery('tasks', broker=app.config['CELERY_BROKER_URL'])
  23. celery.conf.update(app.config)
  24. TaskBase = celery.Task
  25. class ContextTask(TaskBase):
  26. abstract = True
  27. def __call__(self, *args, **kwargs):
  28. with app.app_context():
  29. return TaskBase.__call__(self, *args, **kwargs)
  30. celery.Task = ContextTask
  31. return celery
  32. # Flask application.
  33. app = make_app()
  34. # Debugging.
  35. toolbar = DebugToolbarExtension(app)
  36. # Celery.
  37. celery = make_celery(app)
  38. # Database initialization.
  39. db = SQLAlchemy(app)
  40. from database import models
  41. config = app.config