Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask_migrate import Migrate
- from sqlalchemy.orm import DeclarativeBase
- from flask import Flask
- from flask_ckeditor import CKEditor
- from flask_login import LoginManager
- from flask_sqlalchemy import SQLAlchemy
- from flask_wtf.csrf import CSRFProtect
- from flask_mail import Mail
- class Base(DeclarativeBase):
- pass
- # Setup CSRF protection. This allows html forms to work and be secure
- csrf = CSRFProtect()
- ckeditor = CKEditor()
- # Make @login_required work
- login_manager = LoginManager()
- # You get a custom login message when @login_required appears in the code.
- login_manager.login_message_category = 'Login is required'
- # redirects to this route when using @login_required and you are not logged in
- login_manager.login_view = 'auth.login'
- # setup databases
- db = SQLAlchemy(model_class=Base)
- #for flask migrate
- migrate = Migrate()
- mail = Mail()
- import os
- from ..config import DevelopmentConfig, PytestConfig
- from email_login_confirmation.routes import send_registration_token_email
- def create_app():
- # links to the static folder to make the css work.
- app = Flask(__name__, static_folder='static')
- app.config['SESSION_TYPE'] = 'filesystem'
- from main.forms import SearchForm
- @app.context_processor
- def inject_searchform():
- '''
- Pass Stuff to Navbar such as a form in layout.html from search.html
- If I don't pass on the form in base function then I will
- get an error in layout.html because of {{form.csrf_token}}
- '''
- # The variable name is "searchform" and not "form" because in the html I would have 2 "form" variables
- return dict(searchform=SearchForm())
- current_config = os.environ['FLASK_ENV']
- if current_config == 'dev':
- app.config.from_object(DevelopmentConfig)
- elif current_config == 'test':
- app.config.from_object(PytestConfig)
- db.init_app(app)
- migrate.init_app(app, db)
- login_manager.init_app(app)
- csrf.init_app(app)
- mail.init_app(app)
- ckeditor.init_app(app)
- from models import User
- # This function logs you in and since there is no way of storing it in the database I need the function.
- # how does id work in the function below?
- @login_manager.user_loader
- def load_user(id):
- return db.session.execute(db.select(User).where(User.id==id)).scalar_one_or_none()
- # blocks this from pytest. Because I get a weird error when it runs in pytest
- #if current_config == 'dev':
- # ckeditor.init_app(app)
- # with statement isn't removing the warning
- from auth.routes import auth
- from email_login_confirmation.routes import email_login_confirmation
- from email_password_reset.routes import email_password_reset
- from main.routes import main
- from payment.routes import payment
- from postinfo.routes import postinfo
- from admin.routes import admin
- app.register_blueprint(auth)
- app.register_blueprint(email_login_confirmation)
- app.register_blueprint(email_password_reset)
- app.register_blueprint(main)
- app.register_blueprint(payment)
- app.register_blueprint(postinfo)
- app.register_blueprint(admin)
- return app
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement