Format code with Black

This commit is contained in:
Michał Gdula 2023-04-07 12:35:30 +00:00
parent fef8a557d2
commit 128464d43f
18 changed files with 697 additions and 604 deletions

View file

@ -25,14 +25,14 @@ from sqlalchemy.orm import sessionmaker
from gallery import db
USER_DIR = platformdirs.user_config_dir('onlylegs')
USER_DIR = platformdirs.user_config_dir("onlylegs")
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
login_manager = LoginManager()
assets = Environment()
cache = Cache(config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 300})
cache = Cache(config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 300})
compress = Compress()
@ -40,37 +40,37 @@ def create_app(test_config=None):
"""
Create and configure the main app
"""
app = Flask(__name__, instance_path=os.path.join(USER_DIR, 'instance'))
app = Flask(__name__, instance_path=os.path.join(USER_DIR, "instance"))
# Get environment variables
load_dotenv(os.path.join(USER_DIR, '.env'))
load_dotenv(os.path.join(USER_DIR, ".env"))
print("Loaded environment variables")
# Get config file
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8', mode='r') as file:
with open(os.path.join(USER_DIR, "conf.yml"), encoding="utf-8", mode="r") as file:
conf = safe_load(file)
print("Loaded config")
# App configuration
app.config.from_mapping(
SECRET_KEY=os.environ.get('FLASK_SECRET'),
DATABASE=os.path.join(app.instance_path, 'gallery.sqlite3'),
UPLOAD_FOLDER=os.path.join(USER_DIR, 'uploads'),
ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'],
MAX_CONTENT_LENGTH=1024 * 1024 * conf['upload']['max-size'],
ADMIN_CONF=conf['admin'],
UPLOAD_CONF=conf['upload'],
WEBSITE_CONF=conf['website'],
SECRET_KEY=os.environ.get("FLASK_SECRET"),
DATABASE=os.path.join(app.instance_path, "gallery.sqlite3"),
UPLOAD_FOLDER=os.path.join(USER_DIR, "uploads"),
ALLOWED_EXTENSIONS=conf["upload"]["allowed-extensions"],
MAX_CONTENT_LENGTH=1024 * 1024 * conf["upload"]["max-size"],
ADMIN_CONF=conf["admin"],
UPLOAD_CONF=conf["upload"],
WEBSITE_CONF=conf["website"],
)
if test_config is None:
app.config.from_pyfile('config.py', silent=True)
app.config.from_pyfile("config.py", silent=True)
else:
app.config.from_mapping(test_config)
login_manager.init_app(app)
login_manager.login_view = 'gallery.index'
login_manager.session_protection = 'strong'
login_manager.login_view = "gallery.index"
login_manager.session_protection = "strong"
@login_manager.user_loader
def load_user(user_id):
@ -79,49 +79,46 @@ def create_app(test_config=None):
@login_manager.unauthorized_handler
def unauthorized():
error = 401
msg = 'You are not authorized to view this page!!!!'
return render_template('error.html', error=error, msg=msg), error
msg = "You are not authorized to view this page!!!!"
return render_template("error.html", error=error, msg=msg), error
js_pre = Bundle(
'js/pre/*.js',
filters='jsmin',
output='gen/pre_packed.js',
depends='**'
"js/pre/*.js", filters="jsmin", output="gen/pre_packed.js", depends="**"
)
js_post = Bundle(
'js/post/*.js',
filters='jsmin',
output='gen/post_packed.js',
depends='**'
"js/post/*.js", filters="jsmin", output="gen/post_packed.js", depends="**"
)
styles = Bundle(
'sass/*.sass',
filters='libsass,cssmin',
output='gen/styles.css',
depends='**'
"sass/*.sass", filters="libsass,cssmin", output="gen/styles.css", depends="**"
)
assets.register('js_pre', js_pre)
assets.register('js_post', js_post)
assets.register('styles', styles)
assets.register("js_pre", js_pre)
assets.register("js_post", js_post)
assets.register("styles", styles)
# Error handlers, if the error is not a HTTP error, return 500
@app.errorhandler(Exception)
def error_page(err): # noqa
if not isinstance(err, HTTPException):
abort(500)
return render_template('error.html', error=err.code, msg=err.description), err.code
return (
render_template("error.html", error=err.code, msg=err.description),
err.code,
)
# Load login, registration and logout manager
from gallery import auth
app.register_blueprint(auth.blueprint)
# Load the API
from gallery import api
app.register_blueprint(api.blueprint)
# Load the different views
from gallery.views import index, image, group, settings, profile
app.register_blueprint(index.blueprint)
app.register_blueprint(image.blueprint)
app.register_blueprint(group.blueprint)
@ -129,7 +126,7 @@ def create_app(test_config=None):
app.register_blueprint(settings.blueprint)
# Log to file that the app has started
logging.info('Gallery started successfully!')
logging.info("Gallery started successfully!")
# Initialize extensions and return app
assets.init_app(app)