docekr
This commit is contained in:
+37
-39
@@ -1,12 +1,23 @@
|
||||
from flask import Flask
|
||||
from flask import Flask, request, url_for
|
||||
import click
|
||||
from sqlalchemy import text
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
from .extensions import db, login_manager, socketio
|
||||
from .models import User, CompanySettings, AppSetting
|
||||
from .models import User, AppSetting
|
||||
from .config import Config
|
||||
|
||||
THEMES = {name: {} for name in ("bootstrap", "flatly", "darkly", "pulse")}
|
||||
|
||||
|
||||
def _static_hashes(app):
|
||||
static_root = Path(app.static_folder)
|
||||
hashes = {}
|
||||
for path in static_root.rglob("*"):
|
||||
if path.is_file():
|
||||
relative = path.relative_to(static_root).as_posix()
|
||||
hashes[relative] = hashlib.md5(path.read_bytes()).hexdigest()
|
||||
return hashes
|
||||
|
||||
def _themes(app):
|
||||
return {
|
||||
"bootstrap": {"label":"Bootstrap", "url":app.config["THEME_BOOTSTRAP_URL"], "mode":"light"},
|
||||
@@ -21,11 +32,34 @@ def create_app(test_config=None):
|
||||
app.config.from_object(Config)
|
||||
if test_config: app.config.update(test_config)
|
||||
db.init_app(app); login_manager.init_app(app); socketio.init_app(app)
|
||||
|
||||
from .database import prepare_database
|
||||
prepare_database(app)
|
||||
from .auth import auth; from .main import main
|
||||
from .api import api
|
||||
from .openapi import docs
|
||||
app.register_blueprint(auth); app.register_blueprint(main); app.register_blueprint(api); app.register_blueprint(docs)
|
||||
|
||||
static_hashes = _static_hashes(app)
|
||||
|
||||
@app.context_processor
|
||||
def inject_static_url():
|
||||
def static_url(filename):
|
||||
return url_for("static", filename=filename, v=static_hashes.get(filename, ""))
|
||||
return {"static_url": static_url}
|
||||
|
||||
@app.after_request
|
||||
def set_cache_headers(response):
|
||||
if request.endpoint == "static":
|
||||
filename = request.view_args.get("filename", "") if request.view_args else ""
|
||||
if filename.endswith(".css"):
|
||||
response.headers["Cache-Control"] = "public, max-age=2592000, immutable"
|
||||
else:
|
||||
response.headers["Cache-Control"] = "public, max-age=2592000, immutable"
|
||||
elif response.mimetype == "text/html":
|
||||
response.headers["Cache-Control"] = "no-store, no-cache, private, must-revalidate"
|
||||
return response
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id): return db.session.get(User, int(user_id))
|
||||
|
||||
@@ -60,40 +94,4 @@ def create_app(test_config=None):
|
||||
db.session.commit()
|
||||
click.echo(f"Zresetowano hasło administratora: {normalized_email}")
|
||||
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
if db.engine.dialect.name == "sqlite":
|
||||
def add_column(table, column, ddl, index_sql=None):
|
||||
columns = {row[1] for row in db.session.execute(text(f"PRAGMA table_info({table})")).fetchall()}
|
||||
if column not in columns:
|
||||
db.session.execute(text(f"ALTER TABLE {table} ADD COLUMN {ddl}"))
|
||||
if index_sql:
|
||||
db.session.execute(text(index_sql))
|
||||
add_column("fuel_entry", "station_company_id", "station_company_id INTEGER REFERENCES fuel_station_company(id)", "CREATE INDEX IF NOT EXISTS ix_fuel_entry_station_company_id ON fuel_entry (station_company_id)")
|
||||
add_column("fuel_entry", "fuel_card_id", "fuel_card_id INTEGER REFERENCES fuel_card(id)", "CREATE INDEX IF NOT EXISTS ix_fuel_entry_fuel_card_id ON fuel_entry (fuel_card_id)")
|
||||
add_column("vehicle", "fuel_card_id", "fuel_card_id INTEGER REFERENCES fuel_card(id)", "CREATE INDEX IF NOT EXISTS ix_vehicle_fuel_card_id ON vehicle (fuel_card_id)")
|
||||
add_column("user", "fuel_card_id", "fuel_card_id INTEGER REFERENCES fuel_card(id)", "CREATE INDEX IF NOT EXISTS ix_user_fuel_card_id ON user (fuel_card_id)")
|
||||
add_column("user", "company_id", "company_id INTEGER REFERENCES company_settings(id)", "CREATE INDEX IF NOT EXISTS ix_user_company_id ON user (company_id)")
|
||||
add_column("vehicle", "company_id", "company_id INTEGER REFERENCES company_settings(id)", "CREATE INDEX IF NOT EXISTS ix_vehicle_company_id ON vehicle (company_id)")
|
||||
add_column("fuel_card", "company_id", "company_id INTEGER REFERENCES company_settings(id)", "CREATE INDEX IF NOT EXISTS ix_fuel_card_company_id ON fuel_card (company_id)")
|
||||
add_column("company_settings", "active", "active BOOLEAN NOT NULL DEFAULT 1")
|
||||
add_column("fuel_station_company", "brand_name", "brand_name VARCHAR(120) NOT NULL DEFAULT ''", "CREATE INDEX IF NOT EXISTS ix_fuel_station_company_brand_name ON fuel_station_company (brand_name)")
|
||||
db.session.commit()
|
||||
if not CompanySettings.query.first(): db.session.add(CompanySettings())
|
||||
if not db.session.get(AppSetting, "theme"): db.session.add(AppSetting(key="theme", value="bootstrap"))
|
||||
if not db.session.get(AppSetting, "invoice_split_enabled"): db.session.add(AppSetting(key="invoice_split_enabled", value="1"))
|
||||
if not User.query.first():
|
||||
u=User(email="admin@example.com", name="Administrator", role="admin"); u.set_password("admin123!"); db.session.add(u)
|
||||
db.session.commit()
|
||||
from .models import FuelStationCompany
|
||||
from .integrations.ure import infer_brand
|
||||
for station in FuelStationCompany.query.filter(db.or_(FuelStationCompany.brand_name.is_(None), FuelStationCompany.brand_name == "")).all():
|
||||
station.brand_name = infer_brand(station.company_name)
|
||||
db.session.commit()
|
||||
default_company=CompanySettings.query.order_by(CompanySettings.id).first()
|
||||
if default_company:
|
||||
User.query.filter(User.company_id.is_(None)).update({User.company_id:default_company.id})
|
||||
db.session.execute(text("UPDATE vehicle SET company_id = (SELECT company_id FROM user WHERE user.id = vehicle.owner_id) WHERE company_id IS NULL"))
|
||||
db.session.execute(text("UPDATE fuel_card SET company_id = :cid WHERE company_id IS NULL"), {"cid":default_company.id})
|
||||
db.session.commit()
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user