flask_app.py 1.1 KB

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