Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
3 лет назад
3 лет назад
1 год назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. from flask_marshmallow import Marshmallow
  14. import os
  15. # initiate SQLAlchemy so we can use it later in our models
  16. db = SQLAlchemy()
  17. # initiate Marshmallow for defining schemas for JSON export
  18. ma = Marshmallow()
  19. # initiate Moment for datetime functions
  20. moment = Moment()
  21. def create_app():
  22. app = Flask(__name__)
  23. # get config variables from OS environment variables: set in env file passed through Docker Compose
  24. app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
  25. app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
  26. app.config['SQLALCHEMY_ECHO'] = False
  27. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  28. # for sorting API output correctly
  29. app.config ['JSON_SORT_KEYS'] = False
  30. app.url_map.strict_slashes = False
  31. db.init_app(app)
  32. from . import models
  33. with app.app_context():
  34. db.create_all()
  35. login_manager = LoginManager()
  36. login_manager.login_view = 'auth.login'
  37. login_manager.init_app(app)
  38. moment.init_app(app)
  39. from .models import User
  40. @login_manager.user_loader
  41. def load_user(user_id):
  42. # since the user_id is just the primary key of our user table, use it in the query for the user
  43. return User.query.get(int(user_id))
  44. # blueprint for auth routes in our app
  45. from .auth import auth as auth_blueprint
  46. app.register_blueprint(auth_blueprint)
  47. # blueprint for main parts of app
  48. from .main import main as main_blueprint
  49. app.register_blueprint(main_blueprint)
  50. # blueprint for tools parts of app
  51. from .tool import tool as tool_blueprint
  52. app.register_blueprint(tool_blueprint)
  53. # blueprint for practice parts of app
  54. from .practice import practice as practice_blueprint
  55. app.register_blueprint(practice_blueprint)
  56. # blueprint for book parts of app
  57. from .book import book as book_blueprint
  58. app.register_blueprint(book_blueprint)
  59. # blueprint for create parts of app
  60. from .create import create as create_blueprint
  61. app.register_blueprint(create_blueprint)
  62. # blueprint for API parts of app
  63. from .api import api as api_blueprint
  64. app.register_blueprint(api_blueprint)
  65. # blueprint for search parts of app
  66. from .search import search as search_blueprint
  67. app.register_blueprint(search_blueprint)
  68. return app