| Conditions | 1 |
| Total Lines | 55 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | from flask import Flask |
||
| 25 | def create_app(): |
||
| 26 | |||
| 27 | app = Flask(__name__, static_url_path='/static') |
||
| 28 | |||
| 29 | # Load environment variables from .env file |
||
| 30 | load_dotenv() |
||
| 31 | |||
| 32 | PROJECT_ID: str = os.environ.get("PROJECT_ID") or "" |
||
| 33 | API_SECRET: str = os.environ.get("API_SECRET") or "" |
||
| 34 | FRONTEND_URI: str = os.environ.get("FRONTEND_URI") or "" |
||
| 35 | |||
| 36 | # Use the API_SECRET from the environment variables |
||
| 37 | app.config["API_SECRET"] = API_SECRET |
||
| 38 | |||
| 39 | # Pass PROJECT_ID as a context variable to templates |
||
| 40 | app.config["PROJECT_ID"] = PROJECT_ID |
||
| 41 | |||
| 42 | # Pass FRONTEND_URI as a context variable to templates |
||
| 43 | app.config["FRONTEND_URI"] = FRONTEND_URI |
||
| 44 | |||
| 45 | basedir = os.path.abspath(os.path.dirname(__file__)) |
||
| 46 | secret = secrets.token_urlsafe(16) |
||
| 47 | |||
| 48 | app.config['SECRET_KEY'] = secret |
||
| 49 | # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' |
||
| 50 | app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL') or \ |
||
| 51 | 'sqlite:///' + os.path.join(basedir, 'data/db.sqlite') |
||
| 52 | |||
| 53 | db.init_app(app) |
||
| 54 | migrate.init_app(app, db) |
||
| 55 | |||
| 56 | login_manager = LoginManager() |
||
| 57 | login_manager.login_view = 'auth.login' |
||
| 58 | login_manager.init_app(app) |
||
| 59 | |||
| 60 | from .models import User |
||
| 61 | |||
| 62 | @login_manager.user_loader |
||
| 63 | def load_user(user_id): |
||
| 64 | # since the user_id is just the primary key of our user table, use it in the query for the user |
||
| 65 | return User.query.get(int(user_id)) |
||
| 66 | |||
| 67 | # blueprint for auth routes in our app |
||
| 68 | from .auth import auth as auth_blueprint |
||
| 69 | app.register_blueprint(auth_blueprint) |
||
| 70 | |||
| 71 | # blueprint for non-auth parts of app |
||
| 72 | from .main import main as main_blueprint |
||
| 73 | app.register_blueprint(main_blueprint) |
||
| 74 | |||
| 75 | # Don't use db.create_all() - use flask db upgrade instead |
||
| 76 | # with app.app_context(): |
||
| 77 | # db.create_all() |
||
| 78 | |||
| 79 | return app |
||
| 80 |