| 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 |
||
| 15 | def create_app(): |
||
| 16 | |||
| 17 | app = Flask(__name__, static_url_path='/static') |
||
| 18 | |||
| 19 | # Load environment variables from .env file |
||
| 20 | load_dotenv() |
||
| 21 | |||
| 22 | PROJECT_ID: str = os.environ.get("PROJECT_ID") or "" |
||
| 23 | API_SECRET: str = os.environ.get("API_SECRET") or "" |
||
| 24 | FRONTEND_URI: str = os.environ.get("FRONTEND_URI") or "" |
||
| 25 | |||
| 26 | # Use the API_SECRET from the environment variables |
||
| 27 | app.config["API_SECRET"] = API_SECRET |
||
| 28 | |||
| 29 | # Pass PROJECT_ID as a context variable to templates |
||
| 30 | app.config["PROJECT_ID"] = PROJECT_ID |
||
| 31 | |||
| 32 | # Pass FRONTEND_URI as a context variable to templates |
||
| 33 | app.config["FRONTEND_URI"] = FRONTEND_URI |
||
| 34 | |||
| 35 | basedir = os.path.abspath(os.path.dirname(__file__)) |
||
| 36 | secret = secrets.token_urlsafe(16) |
||
| 37 | |||
| 38 | app.config['SECRET_KEY'] = secret |
||
| 39 | # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' |
||
| 40 | app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL') or \ |
||
| 41 | 'sqlite:///' + os.path.join(basedir, 'data/db.sqlite') |
||
| 42 | |||
| 43 | db.init_app(app) |
||
| 44 | migrate.init_app(app, db) |
||
| 45 | |||
| 46 | login_manager = LoginManager() |
||
| 47 | login_manager.login_view = 'auth.login' |
||
| 48 | login_manager.init_app(app) |
||
| 49 | |||
| 50 | from .models import User |
||
| 51 | |||
| 52 | @login_manager.user_loader |
||
| 53 | def load_user(user_id): |
||
| 54 | # since the user_id is just the primary key of our user table, use it in the query for the user |
||
| 55 | return User.query.get(int(user_id)) |
||
| 56 | |||
| 57 | # blueprint for auth routes in our app |
||
| 58 | from .auth import auth as auth_blueprint |
||
| 59 | app.register_blueprint(auth_blueprint) |
||
| 60 | |||
| 61 | # blueprint for non-auth parts of app |
||
| 62 | from .main import main as main_blueprint |
||
| 63 | app.register_blueprint(main_blueprint) |
||
| 64 | |||
| 65 | # Don't use db.create_all() - use flask db upgrade instead |
||
| 66 | # with app.app_context(): |
||
| 67 | # db.create_all() |
||
| 68 | |||
| 69 | return app |
||
| 70 |