app   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A create_app() 0 54 2
1
from flask import Flask
2
from flask_sqlalchemy import SQLAlchemy
3
from flask_login import LoginManager
4
import secrets
5
import os
6
from flask_migrate import Migrate
7
from dotenv import load_dotenv
8
9
10
# init SQLAlchemy so we can use it later in our models
11
db = SQLAlchemy()
12
migrate = Migrate()
13
14
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
    with app.app_context():
66
        db.create_all()
67
68
    return app
69