Posted By

alejandrombernardis on 05/04/11


Tagged

flask


Versions (?)

Py | Flask


 / Published in: Python
 

URL: http://www.alejandrob.com.ar/

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (c) 2011 The Octopus Apps Inc.
  5. # Licensed under the Apache License, Version 2.0 (the "License")
  6. #
  7. # Author: Alejandro M. Bernardis
  8. # Email: alejandro.m.bernardis at gmail.com
  9. # Created: May 2, 2011 4:07:52 PM
  10. #
  11. from flask import Flask, request, redirect, url_for, jsonify
  12.  
  13. import modules
  14. from .config import Production
  15. from .extensions import db
  16.  
  17. #: -- define --
  18.  
  19. __all__ = ['create_app']
  20.  
  21. DEFAULT_APP_NAME = __name__
  22.  
  23. DEFAULT_APP_MODULES = (
  24. (modules.mod_frontend, ''),
  25. (modules.mod_error, '/error'),
  26. )
  27.  
  28. #: -- application --
  29.  
  30. def create_app(name=None, config=None, modules=None):
  31.  
  32. if not name:
  33. name = DEFAULT_APP_NAME
  34.  
  35. if not config:
  36. config = Production()
  37.  
  38. if not modules:
  39. modules = DEFAULT_APP_MODULES
  40.  
  41. #: application
  42.  
  43. app = Flask(name)
  44.  
  45. #: config
  46.  
  47. app.config.from_object(config)
  48. app.config.from_envvar('APP_CONFIG', silent=True)
  49.  
  50. #: extensions
  51.  
  52. db.init_app(app)
  53.  
  54. #: errors
  55.  
  56. @app.errorhandler(403)
  57. @app.errorhandler(404)
  58. @app.errorhandler(500)
  59. def error_manager(error):
  60. if request.is_xhr:
  61. return jsonify(error=error)
  62. url = 'error.error_%s' % error.__str__()[0:3]
  63. return redirect(url_for(url))
  64.  
  65. #: modules
  66.  
  67. for module, url_prefix in modules:
  68. app.register_module(module, url_prefix=url_prefix)
  69.  
  70. return app

Report this snippet  

You need to login to post a comment.