Flask-Migrations progress

Auto make a migration file when the user lacks one
This commit is contained in:
Michał Gdula 2023-04-10 23:03:47 +00:00
parent 7358c1dc58
commit 83435f5372
2 changed files with 26 additions and 14 deletions

5
.gitignore vendored
View file

@ -1,7 +1,9 @@
gallery/static/theme
gallery/static/.webassets-cache gallery/static/.webassets-cache
gallery/static/gen gallery/static/gen
instance/
migrations/
.idea .idea
.vscode .vscode
@ -11,7 +13,6 @@ venv/
*.pyc *.pyc
__pycache__/ __pycache__/
instance/
.pytest_cache/ .pytest_cache/
.coverage .coverage

View file

@ -2,12 +2,12 @@
Onlylegs Gallery Onlylegs Gallery
This is the main app file, it loads all the other files and sets up the app This is the main app file, it loads all the other files and sets up the app
""" """
# Import system modules
import os import os
import logging import logging
import platformdirs
# Flask
from flask_assets import Bundle from flask_assets import Bundle
from flask_migrate import init as migrate_init
from flask import Flask, render_template, abort from flask import Flask, render_template, abort
from werkzeug.exceptions import HTTPException from werkzeug.exceptions import HTTPException
@ -17,12 +17,9 @@ from gallery.views import index, image, group, settings, profile
from gallery import api from gallery import api
from gallery import auth from gallery import auth
# Configuration
import platformdirs
INSTACE_DIR = os.path.join(platformdirs.user_config_dir("onlylegs"), "instance")
INSTACE_DIR = os.path.join(platformdirs.user_config_dir("onlylegs"), MIGRATIONS_DIR = os.path.join(INSTACE_DIR, "migrations")
"instance")
def create_app(): # pylint: disable=R0914 def create_app(): # pylint: disable=R0914
@ -32,15 +29,23 @@ def create_app(): # pylint: disable=R0914
app = Flask(__name__, instance_path=INSTACE_DIR) app = Flask(__name__, instance_path=INSTACE_DIR)
app.config.from_pyfile("config.py") app.config.from_pyfile("config.py")
# DATABASE
db.init_app(app) db.init_app(app)
migrate.init_app(app, db) migrate.init_app(app, db)
# if database file doesn't exist, create it # If database file doesn't exist, create it
if not os.path.exists(os.path.join(INSTACE_DIR, "gallery.sqlite3")): if not os.path.exists(os.path.join(INSTACE_DIR, "gallery.sqlite3")):
print("Creating database") print("Creating database")
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
# Check if migrations directory exists, if not create it
if not os.path.exists(MIGRATIONS_DIR):
print("Creating migrations directory")
with app.app_context():
migrate_init(directory=MIGRATIONS_DIR)
# LOGIN MANAGER
login_manager.init_app(app) login_manager.init_app(app)
login_manager.login_view = "gallery.index" login_manager.login_view = "gallery.index"
login_manager.session_protection = "normal" login_manager.session_protection = "normal"
@ -55,9 +60,12 @@ def create_app(): # pylint: disable=R0914
msg = "You are not authorized to view this page!!!!" msg = "You are not authorized to view this page!!!!"
return render_template("error.html", error=error, msg=msg), error return render_template("error.html", error=error, msg=msg), error
# Error handlers, if the error is not a HTTP error, return 500 # ERROR HANDLERS
@app.errorhandler(Exception) @app.errorhandler(Exception)
def error_page(err): # noqa def error_page(err): # noqa
"""
Error handlers, if the error is not a HTTP error, return 500
"""
if not isinstance(err, HTTPException): if not isinstance(err, HTTPException):
abort(500) abort(500)
return ( return (
@ -65,8 +73,10 @@ def create_app(): # pylint: disable=R0914
err.code, err.code,
) )
scripts = Bundle("js/*.js", filters="jsmin", output="gen/js.js", depends="js/*.js") # ASSETS
assets.init_app(app)
scripts = Bundle("js/*.js", filters="jsmin", output="gen/js.js", depends="js/*.js")
styles = Bundle( styles = Bundle(
"sass/*.sass", "sass/*.sass",
filters="libsass, cssmin", filters="libsass, cssmin",
@ -77,7 +87,7 @@ def create_app(): # pylint: disable=R0914
assets.register("scripts", scripts) assets.register("scripts", scripts)
assets.register("styles", styles) assets.register("styles", styles)
# Load all the blueprints # BLUEPRINTS
app.register_blueprint(auth.blueprint) app.register_blueprint(auth.blueprint)
app.register_blueprint(api.blueprint) app.register_blueprint(api.blueprint)
app.register_blueprint(index.blueprint) app.register_blueprint(index.blueprint)
@ -86,10 +96,11 @@ def create_app(): # pylint: disable=R0914
app.register_blueprint(profile.blueprint) app.register_blueprint(profile.blueprint)
app.register_blueprint(settings.blueprint) app.register_blueprint(settings.blueprint)
assets.init_app(app) # CACHE AND COMPRESS
cache.init_app(app) cache.init_app(app)
compress.init_app(app) compress.init_app(app)
# Yupee! We got there :3
print("Done!") print("Done!")
logging.info("Gallery started successfully!") logging.info("Gallery started successfully!")
return app return app