Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

__init__.py 2.6KB

3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 publisher parts of app
  52. from .publisher import publisher as publisher_blueprint
  53. app.register_blueprint(publisher_blueprint)
  54. # blueprint for book parts of app
  55. from .book import book as book_blueprint
  56. app.register_blueprint(book_blueprint)
  57. # blueprint for create parts of app
  58. from .create import create as create_blueprint
  59. app.register_blueprint(create_blueprint)
  60. return app