summaryrefslogtreecommitdiff
path: root/app/__init__.py
diff options
context:
space:
mode:
authorNavan Chauhan <navanchauhan@gmail.com>2023-11-15 20:34:41 -0700
committerNavan Chauhan <navanchauhan@gmail.com>2023-11-15 20:34:41 -0700
commit3743f7056dcdbe1a16a00418ea10ebef2669cf61 (patch)
tree298657931ead09efbcf53ccb08bfc3ee501623c1 /app/__init__.py
parent81834656747ead193334a36d8f49045cc214b53e (diff)
initial structureHEADmain
Diffstat (limited to 'app/__init__.py')
-rw-r--r--app/__init__.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/app/__init__.py b/app/__init__.py
new file mode 100644
index 0000000..070c02a
--- /dev/null
+++ b/app/__init__.py
@@ -0,0 +1,73 @@
+import click
+
+from flask import Flask
+from flask.cli import AppGroup
+from config import Config
+from app.extensions import db, Migrate, LoginManager
+
+import flask_login
+from flask_admin import Admin
+from flask_admin.contrib.sqla import ModelView
+
+class ModelView(ModelView):
+ def is_accessible(self):
+ is_admin = False
+ try:
+ is_admin = flask_login.current_user.admin
+ except AttributeError:
+ is_admin = False
+ return is_admin
+
+def create_app(config_class=Config):
+ app = Flask(__name__)
+ app.config.from_object(config_class)
+
+ # Flask Extensions
+ db.init_app(app)
+ migrate = Migrate(app, db)
+
+ login_manager = LoginManager()
+ login_manager.login_view = 'auth.login'
+ login_manager.login_message = "You need to be logged in to view the page..."
+ login_manager.init_app(app)
+
+ from app.models.user import User
+ @login_manager.user_loader
+ def load_user(user_id):
+ return User.query.get(int(user_id))
+
+ admin = Admin(app, name="Flask-App", template_mode="bootstrap4")
+ admin.add_view(ModelView(User, db.session))
+
+ # CLI Commands
+ database_cli = AppGroup('database')
+
+ @database_cli.command('create')
+ def create_database():
+ db.create_all()
+
+ @database_cli.command('delete')
+ def delete_database():
+ db.drop_all()
+
+ @database_cli.command('superuser')
+ @click.argument('user_email')
+ def make_superuser(user_email):
+ user = User.query.filter_by(email=user_email).first()
+ if user:
+ print(f"Making {user.name} into admin")
+ user.admin = True
+ db.session.commit()
+ else:
+ print("User not found with email f{user_email}")
+
+ app.cli.add_command(database_cli)
+ # Blueprints
+
+ from app.auth import bp as auth_routes_bp
+ app.register_blueprint(auth_routes_bp)
+
+ from app.main import bp as main_bp
+ app.register_blueprint(main_bp)
+
+ return app