Advertisement
Guest User

Untitled

a guest
Aug 21st, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1.  
  2. from flask import Flask  
  3. from flask_sqlalchemy import SQLAlchemy
  4. from flask_login import LoginManager
  5. from flask_redmail import RedMail
  6. from flask_ckeditor import CKEditor  
  7. from flask_wtf.csrf import CSRFProtect
  8.  
  9. from flask_migrate import Migrate
  10.  
  11.  
  12.  
  13.  
  14. # Setup CSRF protection. This allows html forms to work and be secure
  15. csrf = CSRFProtect()
  16. # make mail work
  17. email = RedMail()
  18.  
  19.  
  20.  
  21. ckeditor = CKEditor()
  22.  
  23.  
  24.  
  25.  
  26. app = Flask(__name__)
  27.  
  28. # Make @login_required work
  29. login_manager = LoginManager(app)
  30.  
  31.  
  32.  
  33. # You get a custom login message when @login_required appears in the code.
  34. login_manager.login_message_category = 'Login is required'
  35.  
  36. # Should I use auth.login ?
  37. login_manager.login_view = 'login'
  38.  
  39.  
  40.  
  41.  
  42. # setup databases
  43. db = SQLAlchemy()
  44. migrate = Migrate(app, db)
  45.  
  46.  
  47. from app.models import User
  48. # Use User.query.get instead of User.get because of sqlalchemy ?
  49. # This function logs you in and since there is no way of storing it in the database I need the function.
  50. # Add @app because of the way the app is structured.
  51. @app.login_manager.user_loader
  52. def load_user(id):
  53.     return User.query.get(id)
  54.  
  55.  
  56. import os
  57. env = os.environ.get('TEST_ENV', 'default')
  58. from app.config import configs
  59. '''
  60. allows multiple configs for example it will try development config and
  61. if that doesn't work 'PytestConfig'
  62. '''
  63.  
  64. def create_app(config_env=configs[env]):
  65.     # The function name is from the config file which is "Class config:".
  66.     app.config.from_object(config_env)
  67.     migrate.init_app(app, db)
  68.     db.init_app(app)
  69.     login_manager.init_app(app)
  70.     email.init_app(app)  
  71.     csrf.init_app(app)
  72.     # blocks this from pytest. Because I get a weird error when it runs in pytest
  73.     if app.config['WTF_CSRF_ENABLED'] == True:
  74.         ckeditor.init_app(app)
  75.            
  76.     from app.auth.routes import auth
  77.     from app.postinfo.routes import postinfo
  78.     from app.mail.routes import mail
  79.     from app.payment.routes import payment
  80.  
  81.     app.register_blueprint(auth)
  82.     app.register_blueprint(postinfo)
  83.     app.register_blueprint(mail)
  84.     app.register_blueprint(payment)
  85.  
  86.     return app
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement