Advertisement
Guest User

Untitled

a guest
Jun 10th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. from flask_migrate import Migrate
  2. from sqlalchemy.orm import DeclarativeBase
  3.  
  4. from flask import Flask
  5. from flask_ckeditor import CKEditor
  6. from flask_login import LoginManager
  7.  
  8. from flask_sqlalchemy import SQLAlchemy
  9. from flask_wtf.csrf import CSRFProtect
  10. from flask_mail import Mail
  11.  
  12.  
  13. class Base(DeclarativeBase):
  14.     pass
  15.  
  16.  
  17. # Setup CSRF protection. This allows html forms to work and be secure
  18. csrf = CSRFProtect()
  19. ckeditor = CKEditor()
  20. # Make @login_required work
  21. login_manager = LoginManager()
  22. # You get a custom login message when @login_required appears in the code.
  23. login_manager.login_message_category = 'Login is required'
  24. # redirects to this route when using @login_required and you are not logged in
  25. login_manager.login_view = 'auth.login'
  26. # setup databases
  27. db = SQLAlchemy(model_class=Base)
  28. #for flask migrate
  29. migrate = Migrate()
  30. mail = Mail()
  31.  
  32.  
  33.  
  34.  
  35.  
  36. import os
  37. from ..config import DevelopmentConfig, PytestConfig
  38. from email_login_confirmation.routes import send_registration_token_email
  39.  
  40.  
  41.  
  42. def create_app():
  43.     # links to the  static folder to make the css work.
  44.     app = Flask(__name__, static_folder='static')
  45.  
  46.     app.config['SESSION_TYPE'] = 'filesystem'
  47.     from main.forms import SearchForm
  48.     @app.context_processor
  49.     def inject_searchform():
  50.         '''
  51.        Pass Stuff to Navbar such as a form in layout.html from search.html
  52.            
  53.        If I don't pass on the form in base function then I will
  54.        get an error in layout.html because of {{form.csrf_token}}
  55.        '''
  56.         # The variable name is "searchform" and not "form" because in the html I would have 2 "form" variables
  57.         return dict(searchform=SearchForm())
  58.        
  59.     current_config = os.environ['FLASK_ENV']
  60.     if current_config == 'dev':
  61.           app.config.from_object(DevelopmentConfig)
  62.     elif current_config == 'test':
  63.         app.config.from_object(PytestConfig)
  64.  
  65.  
  66.  
  67.     db.init_app(app)
  68.     migrate.init_app(app, db)
  69.     login_manager.init_app(app)
  70.     csrf.init_app(app)
  71.     mail.init_app(app)
  72.     ckeditor.init_app(app)
  73.  
  74.  
  75.     from models import User
  76.     # This function logs you in and since there is no way of storing it in the database I need the function.
  77.     # how does id work in the function below?
  78.     @login_manager.user_loader
  79.     def load_user(id):
  80.         return db.session.execute(db.select(User).where(User.id==id)).scalar_one_or_none()
  81.  
  82.  
  83.  
  84.  
  85.     # blocks this from pytest. Because I get a weird error when it runs in pytest
  86.     #if current_config == 'dev':
  87.     #    ckeditor.init_app(app)
  88.  
  89.     # with statement isn't removing the warning
  90.    
  91.     from auth.routes import auth
  92.     from email_login_confirmation.routes import email_login_confirmation
  93.     from email_password_reset.routes import email_password_reset
  94.     from main.routes import main
  95.     from payment.routes import payment
  96.     from postinfo.routes import postinfo
  97.     from admin.routes import admin
  98.    
  99.     app.register_blueprint(auth)
  100.     app.register_blueprint(email_login_confirmation)
  101.     app.register_blueprint(email_password_reset)
  102.     app.register_blueprint(main)
  103.     app.register_blueprint(payment)    
  104.     app.register_blueprint(postinfo)
  105.     app.register_blueprint(admin)
  106.  
  107.  
  108.  
  109.     return app
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement