You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 line
2.3KB

  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. from flask_moment import Moment
  14. import os
  15. # initiate SQLAlchemy so we can use it later in our models
  16. db = SQLAlchemy()
  17. # initiate Moment for datetime functions
  18. moment = Moment()
  19. def create_app():
  20. app = Flask(__name__)
  21. # get config variables from OS environment variables: set in env file passed through Docker Compose
  22. app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
  23. app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
  24. app.config['SQLALCHEMY_ECHO'] = False
  25. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  26. db.init_app(app)
  27. login_manager = LoginManager()
  28. login_manager.login_view = 'auth.login'
  29. login_manager.init_app(app)
  30. moment.init_app(app)
  31. from .models import User
  32. @login_manager.user_loader
  33. def load_user(user_id):
  34. # since the user_id is just the primary key of our user table, use it in the query for the user
  35. return User.query.get(int(user_id))
  36. # blueprint for auth routes in our app
  37. from .auth import auth as auth_blueprint
  38. app.register_blueprint(auth_blueprint)
  39. # blueprint for main parts of app
  40. from .main import main as main_blueprint
  41. app.register_blueprint(main_blueprint)
  42. # blueprint for tools parts of app
  43. from .tool import tool as tool_blueprint
  44. app.register_blueprint(tool_blueprint)
  45. # blueprint for book parts of app
  46. from .book import book as book_blueprint
  47. app.register_blueprint(book_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 create parts of app
  52. from .create import create as create_blueprint
  53. app.register_blueprint(create_blueprint)
  54. return app