-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy path__init__.py
More file actions
61 lines (47 loc) · 1.7 KB
/
__init__.py
File metadata and controls
61 lines (47 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager
from flask_migrate import Migrate
# Create SQLAlchemy instance
db = SQLAlchemy()
DB_NAME = "database.db"
migrate = Migrate()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs'
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
# Initialize SQLAlchemy with the Flask app
db.init_app(app)
migrate.init_app(app, db)
# Import blueprints
from .views import views
from .auth import auth
from .weight_tracker import weight_tracker_bp # Import the weight tracker blueprint
# Register blueprints
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/auth')
app.register_blueprint(weight_tracker_bp, url_prefix='/weight') # Register the weight tracker blueprint
# Import models
from .models import User, Note, QuestionnaireResponse, WeightEntry
with app.app_context():
# Create database tables if they don't exist
db.create_all()
# Initialize LoginManager
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
# Define the user loader function
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
# Inject current_user into templates
@app.context_processor
def inject_user():
from flask_login import current_user
return dict(user=current_user)
return app
def create_database(app):
if not path.exists('website/' + DB_NAME):
db.create_all(app=app)
print('Created Database!')