No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

52 líneas
1.7KB

  1. # @name: __init__.py
  2. # @version: 0.1
  3. # @creation_date: 2021-10-20
  4. # @license: The MIT License <https://opensource.org/licenses/MIT>
  5. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  6. # @purpose: Initialises the app, SQLAlchemy, and configuration variables
  7. # @acknowledgements:
  8. # https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login
  9. # Config stuff adapted from https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms
  10. from flask import Flask
  11. from flask_sqlalchemy import SQLAlchemy
  12. from flask_login import LoginManager
  13. import os
  14. # init SQLAlchemy so we can use it later in our models
  15. db = SQLAlchemy()
  16. def create_app():
  17. app = Flask(__name__)
  18. # get config variables from OS environment variables: set in env file passed through Docker Compose
  19. app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
  20. app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
  21. db.init_app(app)
  22. login_manager = LoginManager()
  23. login_manager.login_view = 'auth.login'
  24. login_manager.init_app(app)
  25. from .models import User
  26. @login_manager.user_loader
  27. def load_user(user_id):
  28. # since the user_id is just the primary key of our user table, use it in the query for the user
  29. return User.query.get(int(user_id))
  30. # blueprint for auth routes in our app
  31. from .auth import auth as auth_blueprint
  32. app.register_blueprint(auth_blueprint)
  33. # blueprint for non-auth parts of app
  34. from .main import main as main_blueprint
  35. app.register_blueprint(main_blueprint)
  36. # blueprint for tools parts of app
  37. from .tool import tool as tool_blueprint
  38. app.register_blueprint(tool_blueprint)
  39. return app