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.

__init__.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. db.init_app(app)
  26. login_manager = LoginManager()
  27. login_manager.login_view = 'auth.login'
  28. login_manager.init_app(app)
  29. moment.init_app(app)
  30. from .models import User
  31. @login_manager.user_loader
  32. def load_user(user_id):
  33. # since the user_id is just the primary key of our user table, use it in the query for the user
  34. return User.query.get(int(user_id))
  35. # blueprint for auth routes in our app
  36. from .auth import auth as auth_blueprint
  37. app.register_blueprint(auth_blueprint)
  38. # blueprint for main parts of app
  39. from .main import main as main_blueprint
  40. app.register_blueprint(main_blueprint)
  41. # blueprint for tools parts of app
  42. from .tool import tool as tool_blueprint
  43. app.register_blueprint(tool_blueprint)
  44. # blueprint for practice parts of app
  45. from .practice import practice as practice_blueprint
  46. app.register_blueprint(practice_blueprint)
  47. # blueprint for publisher parts of app
  48. from .publisher import publisher as publisher_blueprint
  49. app.register_blueprint(publisher_blueprint)
  50. # blueprint for book parts of app
  51. from .book import book as book_blueprint
  52. app.register_blueprint(book_blueprint)
  53. # blueprint for create parts of app
  54. from .create import create as create_blueprint
  55. app.register_blueprint(create_blueprint)
  56. return app