__init__.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. from flask import Flask, g, current_app, request
  3. from flask.ext.babel import Babel
  4. from flask.ext.sqlalchemy import SQLAlchemy, event
  5. from flask.ext.mail import Mail
  6. from flask.ext.cache 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 current_app.config['LANGUAGES'].keys():
  15. return request.cookies.get('locale')
  16. return request.accept_languages.best_match(current_app.config['LANGUAGES'].keys(), 'en')
  17. def create_app(config={}):
  18. global babel, db, mail, sess
  19. app = Flask(__name__)
  20. app.config["CACHE_TYPE"] = "null"
  21. app.config.from_object('ffdnispdb.default_settings')
  22. app.config.from_envvar('FFDNISPDB_SETTINGS', True)
  23. if isinstance(config, dict):
  24. app.config.update(config)
  25. else:
  26. app.config.from_object(config)
  27. babel.init_app(app)
  28. babel.localeselector(get_locale)
  29. db.init_app(app)
  30. with app.app_context():
  31. @event.listens_for(db.engine, "first_connect")
  32. def connect(sqlite, connection_rec):
  33. sqlite.enable_load_extension(True)
  34. try:
  35. sqlite.execute('select load_extension("libspatialite")')
  36. current_app.spatialite_modulename = 'libspatialite'
  37. except Exception as e:
  38. sqlite.execute('select load_extension("mod_spatialite")')
  39. current_app.spatialite_modulename = 'mod_spatialite'
  40. sqlite.enable_load_extension(False)
  41. @event.listens_for(db.engine, "connect")
  42. def connect(sqlite, connection_rec):
  43. sqlite.enable_load_extension(True)
  44. sqlite.execute('select load_extension("%s")' % current_app.spatialite_modulename)
  45. sqlite.enable_load_extension(False)
  46. app.session_interface = sess
  47. mail.init_app(app)
  48. cache.init_app(app)
  49. from .views import ispdb
  50. from .views_api import ispdbapi
  51. app.register_blueprint(ispdb)
  52. app.register_blueprint(ispdbapi, url_prefix='/api')
  53. return app
  54. from . import models