Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -83,23 +83,38 @@ def create_app():
|
|
| 83 |
|
| 84 |
|
| 85 |
def init_database():
|
|
|
|
|
|
|
| 86 |
# Restore from seed if running in cloud and target DB is missing
|
| 87 |
-
|
| 88 |
-
if Config._cloud and os.path.exists(
|
| 89 |
db_uri = Config.SQLALCHEMY_DATABASE_URI
|
| 90 |
if db_uri.startswith('sqlite:///'):
|
| 91 |
db_file = db_uri.replace('sqlite:///', '', 1)
|
| 92 |
if not os.path.exists(db_file) or os.path.getsize(db_file) == 0:
|
| 93 |
os.makedirs(os.path.dirname(db_file), exist_ok=True)
|
| 94 |
import shutil
|
| 95 |
-
shutil.copy2(
|
| 96 |
logger.info('Restored seed database to %s', db_file)
|
| 97 |
-
# Dispose engine so it picks up the new DB file
|
| 98 |
db.session.remove()
|
| 99 |
db.engine.dispose()
|
| 100 |
|
| 101 |
db.create_all()
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
# Ensure egor (SM) exists
|
| 104 |
sm = User.query.filter_by(username='egor').first()
|
| 105 |
if not sm:
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
def init_database():
|
| 86 |
+
seed_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'seed')
|
| 87 |
+
|
| 88 |
# Restore from seed if running in cloud and target DB is missing
|
| 89 |
+
seed_db = os.path.join(seed_dir, 'kpi_dashboard.db')
|
| 90 |
+
if Config._cloud and os.path.exists(seed_db):
|
| 91 |
db_uri = Config.SQLALCHEMY_DATABASE_URI
|
| 92 |
if db_uri.startswith('sqlite:///'):
|
| 93 |
db_file = db_uri.replace('sqlite:///', '', 1)
|
| 94 |
if not os.path.exists(db_file) or os.path.getsize(db_file) == 0:
|
| 95 |
os.makedirs(os.path.dirname(db_file), exist_ok=True)
|
| 96 |
import shutil
|
| 97 |
+
shutil.copy2(seed_db, db_file)
|
| 98 |
logger.info('Restored seed database to %s', db_file)
|
|
|
|
| 99 |
db.session.remove()
|
| 100 |
db.engine.dispose()
|
| 101 |
|
| 102 |
db.create_all()
|
| 103 |
|
| 104 |
+
# Restore seed uploads in cloud mode
|
| 105 |
+
seed_uploads = os.path.join(seed_dir, 'uploads')
|
| 106 |
+
if Config._cloud and os.path.isdir(seed_uploads):
|
| 107 |
+
from flask import current_app
|
| 108 |
+
upload_dir = current_app.config['UPLOAD_FOLDER']
|
| 109 |
+
os.makedirs(upload_dir, exist_ok=True)
|
| 110 |
+
import shutil
|
| 111 |
+
for fname in os.listdir(seed_uploads):
|
| 112 |
+
src = os.path.join(seed_uploads, fname)
|
| 113 |
+
dst = os.path.join(upload_dir, fname)
|
| 114 |
+
if os.path.isfile(src) and not os.path.exists(dst):
|
| 115 |
+
shutil.copy2(src, dst)
|
| 116 |
+
logger.info('Restored upload: %s', fname)
|
| 117 |
+
|
| 118 |
# Ensure egor (SM) exists
|
| 119 |
sm = User.query.filter_by(username='egor').first()
|
| 120 |
if not sm:
|