修改config
This commit is contained in:
+57
-23
@@ -24,25 +24,26 @@ app = Flask(__name__, static_folder="../frontend", static_url_path="/static")
|
||||
CORS(app)
|
||||
|
||||
base_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
||||
default_db_path = os.path.join(base_dir, "data", "data.db")
|
||||
if os.name == 'nt':
|
||||
default_db_url = f"sqlite:///{default_db_path}"
|
||||
db_path = os.path.join(base_dir, "data", "data.db")
|
||||
db_url_env = os.getenv('DATABASE_URL')
|
||||
if db_url_env:
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = db_url_env
|
||||
else:
|
||||
default_db_url = f"sqlite:////{default_db_path}"
|
||||
db_url = os.getenv('DATABASE_URL') or default_db_url
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = db_url
|
||||
if os.name == 'nt':
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{db_path}"
|
||||
else:
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:////{db_path}"
|
||||
|
||||
def _ensure_sqlite_dir(url: str):
|
||||
if not isinstance(url, str) or not url.startswith('sqlite'):
|
||||
return
|
||||
p = url.replace('sqlite:////', '/').replace('sqlite:///', '')
|
||||
d = os.path.dirname(p)
|
||||
if d and not os.path.exists(d):
|
||||
def _ensure_sqlite_dir():
|
||||
d = os.path.dirname(db_path)
|
||||
if not os.path.exists(d):
|
||||
os.makedirs(d, exist_ok=True)
|
||||
|
||||
_ensure_sqlite_dir(db_url)
|
||||
_ensure_sqlite_dir()
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
scheduler = None
|
||||
|
||||
class DailyRevenue(db.Model):
|
||||
__tablename__ = 'daily_revenue'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
@@ -106,15 +107,16 @@ def daily_job(target_date=None):
|
||||
if target_date is None:
|
||||
target_date = datetime.now().date()
|
||||
|
||||
existing = DailyRevenue.query.filter_by(date=target_date).first()
|
||||
if existing:
|
||||
if not existing.is_final:
|
||||
existing.is_final = True
|
||||
existing.source = existing.source or 'generator'
|
||||
db.session.commit()
|
||||
# 补推消息
|
||||
push_feishu(target_date.isoformat(), existing.amount, "daily_finalize")
|
||||
return
|
||||
existing = DailyRevenue.query.filter_by(date=target_date).first()
|
||||
if existing:
|
||||
if not existing.is_final:
|
||||
existing.is_final = True
|
||||
existing.source = existing.source or 'generator'
|
||||
db.session.commit()
|
||||
push_feishu(target_date.isoformat(), existing.amount, "daily_finalize")
|
||||
else:
|
||||
push_feishu(target_date.isoformat(), existing.amount, "daily_exists")
|
||||
return
|
||||
|
||||
amount = gen_amount_for_date(target_date, cfg)
|
||||
rev = DailyRevenue(date=target_date, amount=amount, is_final=True, source='generator')
|
||||
@@ -328,6 +330,38 @@ def api_series7():
|
||||
cur += timedelta(days=1)
|
||||
return jsonify(series)
|
||||
|
||||
@app.route('/api/admin/reload_cutoff', methods=['POST'])
|
||||
def admin_reload_cutoff():
|
||||
token = os.getenv('ADMIN_TOKEN')
|
||||
if token and request.headers.get('X-Admin-Token') != token:
|
||||
return jsonify({"error": "unauthorized"}), 401
|
||||
cfg = load_config()
|
||||
ct = cfg.get("cutoff_time")
|
||||
ch = cfg.get("cutoff_hour", 23)
|
||||
cm = 0
|
||||
if isinstance(ct, str) and re.match(r'^\d{1,2}:\d{2}$', ct):
|
||||
try:
|
||||
p = ct.split(':')
|
||||
ch = int(p[0]); cm = int(p[1])
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
ch = int(ch)
|
||||
except Exception:
|
||||
ch = 23
|
||||
if ch < 0 or ch > 23:
|
||||
ch = 23
|
||||
if cm < 0 or cm > 59:
|
||||
cm = 0
|
||||
global scheduler
|
||||
if scheduler:
|
||||
try:
|
||||
scheduler.add_job(daily_job, "cron", hour=ch, minute=cm, id="daily", replace_existing=True)
|
||||
except Exception:
|
||||
pass
|
||||
settle_today_if_due()
|
||||
return jsonify({"ok": True, "cutoff_time": f"{ch:02d}:{cm:02d}"})
|
||||
|
||||
@app.route("/api/revenue")
|
||||
def api_revenue():
|
||||
"""查询历史营业额"""
|
||||
@@ -508,7 +542,7 @@ if __name__ == "__main__":
|
||||
ch = 23
|
||||
if cm < 0 or cm > 59:
|
||||
cm = 0
|
||||
scheduler.add_job(daily_job, "cron", hour=ch, minute=cm)
|
||||
scheduler.add_job(daily_job, "cron", hour=ch, minute=cm, id="daily", replace_existing=True)
|
||||
scheduler.start()
|
||||
with app.app_context():
|
||||
sync_log_to_db()
|
||||
|
||||
Reference in New Issue
Block a user