__init__.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. from flask import Flask, g, current_app, request
  3. from flask_babel import Babel
  4. from flask_sqlalchemy import SQLAlchemy, event
  5. from flask_mail import Mail
  6. from flask_caching import Cache
  7. from .sessions import MySessionInterface
  8. babel = Babel()
  9. db = SQLAlchemy()
  10. sess = MySessionInterface(db)
  11. cache = Cache()
  12. mail = Mail()
  13. def get_locale():
  14. if request.cookies.get('locale') in list(current_app.config['LANGUAGES'].keys()):
  15. return request.cookies.get('locale')
  16. return request.accept_languages.best_match(list(current_app.config['LANGUAGES'].keys()), 'en')
  17. def create_app(config={}):
  18. global babel, db, mail, sess
  19. app = Flask(__name__)
  20. app.config.from_object('ffdnispdb.default_settings')
  21. app.config.from_envvar('FFDNISPDB_SETTINGS', True)
  22. if isinstance(config, dict):
  23. app.config.update(config)
  24. else:
  25. app.config.from_object(config)
  26. babel.init_app(app)
  27. babel.localeselector(get_locale)
  28. db.init_app(app)
  29. with app.app_context():
  30. @event.listens_for(db.engine, "connect")
  31. def connect(sqlite, connection_rec):
  32. sqlite.enable_load_extension(True)
  33. sqlite.execute('select load_extension("mod_spatialite")')
  34. sqlite.execute('SELECT InitSpatialMetaData()')
  35. sqlite.enable_load_extension(False)
  36. app.session_interface = sess
  37. mail.init_app(app)
  38. cache.init_app(app)
  39. from .views import ispdb
  40. from .views_api import ispdbapi
  41. app.register_blueprint(ispdb)
  42. app.register_blueprint(ispdbapi, url_prefix='/api')
  43. return app
  44. from . import models