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.

78 líneas
2.4KB

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