Compare commits
3 Commits
4ebe15522f
...
test
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f66edbb21 | |||
| 38f35d1915 | |||
| ca8e70781c |
45
app.py
45
app.py
@@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
from flask import Flask, session, request
|
from flask import Flask, session, request
|
||||||
|
from sqlalchemy import inspect
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
# 1. Загрузка переменных окружения - в самом верху
|
# 1. Загрузка переменных окружения - в самом верху
|
||||||
@@ -62,26 +63,30 @@ def create_app():
|
|||||||
login_manager.login_message_category = "info"
|
login_manager.login_message_category = "info"
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
from models import User, UserConfig
|
if inspect(db.engine).has_table('user_config'):
|
||||||
all_configs = UserConfig.query.all()
|
from models import User, UserConfig
|
||||||
for config in all_configs:
|
all_configs = UserConfig.query.all()
|
||||||
user_id = config.user_id
|
for config in all_configs:
|
||||||
mappings = config.mappings
|
user_id = config.user_id
|
||||||
for sheer_title, params in mappings.items():
|
mappings = config.mappings
|
||||||
cron_schedule = params.get('schedule_cron')
|
for sheer_title, params in mappings.items():
|
||||||
if cron_schedule:
|
if isinstance(params, dict):
|
||||||
job_id = f"user_{user_id}_sheet_{sheer_title}"
|
cron_schedule = params.get('schedule_cron')
|
||||||
try:
|
if cron_schedule:
|
||||||
scheduler.add_job(
|
job_id = f"user_{user_id}_sheet_{sheer_title}"
|
||||||
id=job_id,
|
try:
|
||||||
func=execute_olap_export,
|
scheduler.add_job(
|
||||||
trigger='cron',
|
id=job_id,
|
||||||
args=[user_id, sheer_title],
|
func=execute_olap_export,
|
||||||
**_parse_cron_string(cron_schedule)
|
trigger='cron',
|
||||||
)
|
args=[user_id, sheer_title],
|
||||||
app.logger.info(f"Job {job_id} loaded on startup.")
|
**_parse_cron_string(cron_schedule)
|
||||||
except Exception as e:
|
)
|
||||||
app.logger.error(f"Failed to load job {job_id}: {e}")
|
app.logger.info(f"Job {job_id} loaded on startup.")
|
||||||
|
except Exception as e:
|
||||||
|
app.logger.error(f"Failed to load job {job_id}: {e}")
|
||||||
|
else:
|
||||||
|
app.logger.warning("Database tables not found. Skipping job loading on startup. Run 'flask init-db' to create the tables.")
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
|
|
||||||
# --- Регистрация команд CLI ---
|
# --- Регистрация команд CLI ---
|
||||||
|
|||||||
28
routes.py
28
routes.py
@@ -131,13 +131,24 @@ def logout():
|
|||||||
@login_required
|
@login_required
|
||||||
def index():
|
def index():
|
||||||
config = g.user_config
|
config = g.user_config
|
||||||
|
clean_mappings = {}
|
||||||
|
if config.mappings:
|
||||||
|
for key, value in config.mappings.items():
|
||||||
|
if isinstance(value, dict):
|
||||||
|
clean_mappings[key] = value
|
||||||
|
else:
|
||||||
|
clean_mappings[key] = {
|
||||||
|
'report_id': value,
|
||||||
|
'schedule_cron': None,
|
||||||
|
'schedule_period': None
|
||||||
|
}
|
||||||
return render_template(
|
return render_template(
|
||||||
'index.html',
|
'index.html',
|
||||||
rms_config=config.get_rms_dict(),
|
rms_config=config.get_rms_dict(),
|
||||||
google_config=config.get_google_dict(),
|
google_config=config.get_google_dict(),
|
||||||
presets=config.presets,
|
presets=config.presets,
|
||||||
sheets=config.sheets,
|
sheets=config.sheets,
|
||||||
mappings=config.mappings,
|
mappings=clean_mappings,
|
||||||
client_email=config.google_client_email
|
client_email=config.google_client_email
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -291,10 +302,18 @@ def mapping_set():
|
|||||||
if selected_report_id:
|
if selected_report_id:
|
||||||
# Получаем существующие данные расписания для этого листа
|
# Получаем существующие данные расписания для этого листа
|
||||||
existing_schedule = current_mappings.get(sheet['title'], {})
|
existing_schedule = current_mappings.get(sheet['title'], {})
|
||||||
|
schedule_cron = None
|
||||||
|
schedule_period = None
|
||||||
|
|
||||||
|
if isinstance(existing_mapping_value, dict):
|
||||||
|
schedule_cron = existing_mapping_value.get('schedule_cron')
|
||||||
|
schedule_period = existing_mapping_value.get('schedule_period')
|
||||||
|
|
||||||
|
# Сохраняем новые настройки расписания в новом словаре
|
||||||
new_mappings[sheet['title']] = {
|
new_mappings[sheet['title']] = {
|
||||||
'report_id': selected_report_id,
|
'report_id': selected_report_id,
|
||||||
'schedule_cron': existing_schedule.get('schedule_cron'),
|
'schedule_cron': schedule_cron,
|
||||||
'schedule_period': existing_schedule.get('schedule_period')
|
'schedule_period': schedule_period
|
||||||
}
|
}
|
||||||
|
|
||||||
config.mappings = new_mappings
|
config.mappings = new_mappings
|
||||||
@@ -462,6 +481,9 @@ def save_schedule():
|
|||||||
updated_mappings = config.mappings or {}
|
updated_mappings = config.mappings or {}
|
||||||
|
|
||||||
for sheet_title, params in updated_mappings.items():
|
for sheet_title, params in updated_mappings.items():
|
||||||
|
if not isinstance(params, dict):
|
||||||
|
continue
|
||||||
|
|
||||||
cron_value = request.form.get(f"cron-{sheet_title}", "").strip()
|
cron_value = request.form.get(f"cron-{sheet_title}", "").strip()
|
||||||
period_value = request.form.get(f"period-{sheet_title}", "").strip()
|
period_value = request.form.get(f"period-{sheet_title}", "").strip()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user