__init__.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. '''
  2. this file is part of "El Botadero"
  3. copyright 2018 Rodrigo Garcia <strysg@riseup.net>
  4. AGPL liberated.
  5. '''
  6. import os
  7. from flask import Flask
  8. from .configs import Parameters
  9. ######
  10. # Nota importante si solo se usa 'from .shared import globalParams' dentro
  11. # de este modulo *se crea una copia* local de `globalParams' de shared
  12. # y las modificaciones aqui no suponen un cambio global
  13. # por eso se utiliza from . import shared
  14. ######
  15. from . import shared
  16. def create_app(config=None, instance_path=None, db_path='sqlite:///db.sqlite3', testing=False):
  17. """ Crear la app.
  18. :param instance_path: An alternative instance path for the application.
  19. By default the folder ``'instance'`` next to the
  20. package or module is assumed to be the instance
  21. path.
  22. See :ref:`Instance Folders <flask:instance-folders>`.
  23. :param config: The configuration file or object.
  24. The environment variable is weightet as the heaviest.
  25. For example, if the config is specified via an file
  26. and a ENVVAR, it will load the config via the file and
  27. later overwrite it from the ENVVAR.
  28. :param db_path: Database URI to be `SQLALCHEMY_DATABASE_URI', if not
  29. provided it uses 'sqlite:///db.sqlite3'
  30. """
  31. app = Flask(__name__,
  32. instance_path=instance_path,
  33. instance_relative_config=True)
  34. print ('\nINICIANDO\n')
  35. print ('os.environ.FLASK_ENV:', str(os.environ['FLASK_ENV']))
  36. print ('instance_path:', app.instance_path)
  37. # instance folders are not automatically created by flask
  38. if not os.path.exists(app.instance_path):
  39. os.makedirs(app.instance_path)
  40. # config file and parameters
  41. # if config is None:
  42. # nothing yet!
  43. if os.environ['FLASK_ENV'] == 'development':
  44. app.config.from_pyfile('../botadero/configs/configsDevelopment.py')
  45. elif os.environ['FLASK_ENV'] == 'production':
  46. app.config.from_pyfile('../botadero/configs/configs.py')
  47. print ('app.config:', str(app.config), '\n')
  48. # configuraciones adicionales
  49. shared.globalParams = Parameters(app)
  50. print ('Configs cargadas--')
  51. print (shared.globalParams)
  52. app.config['UPLOAD_FOLDER'] = shared.globalParams.uploadDirectory
  53. shared.globalParams.sizeLimitsAndTimeToDelete.sort(reverse=True)
  54. # app.config['MAX_CONTENT_LENGTH'] = int(shared.globalParams.sizeLimitsAndTimeToDelete[0][0])
  55. # base de datos
  56. print('Max file size:', shared.globalParams.sizeLimitsAndTimeToDelete[0][0])
  57. print ('\nBase de datos setup---')
  58. from . import database
  59. ctx = app.app_context()
  60. ctx.push()
  61. database.setup_db(app, db_path=db_path, testing=testing)
  62. # blueprints
  63. configure_blueprints(app)
  64. print ('\nCreating app finished!')
  65. return app
  66. def configure_blueprints(app):
  67. from . import views
  68. app.register_blueprint(views.botaderoBp)