feat: 按账号调度自动学习并让首页实时跟上状态

This commit is contained in:
2026-11-16 01:14:36 +08:00
parent 158578d17c
commit 7f0466b0f6
31 changed files with 2699 additions and 631 deletions
+41 -7
View File
@@ -45,22 +45,32 @@ def view_of(account) -> dict:
hour = int(getattr(account, "study_start_hour", 0) or 0)
minute = int(getattr(account, "study_start_minute", 0) or 0)
second = int(getattr(account, "study_start_second", 0) or 0)
enabled = getattr(account, "study_schedule_enabled", True)
return {
"day": day,
"hour": hour,
"minute": minute,
"second": second,
"enabled": False if enabled is False else True,
"label": label_of(account),
}
def window_open(account, now: datetime | None = None) -> bool:
"""当前是否已过该账号本月开始时刻。2 月 31 日等按当月最后一天。"""
def as_cn(now: datetime | None = None) -> datetime:
current = now or datetime.now(CN_TZ)
if current.tzinfo is None:
current = current.replace(tzinfo=CN_TZ)
else:
current = current.astimezone(CN_TZ)
return current.replace(tzinfo=CN_TZ)
return current.astimezone(CN_TZ)
def month_key(now: datetime | None = None) -> str:
current = as_cn(now)
return f"{current.year:04d}-{current.month:02d}"
def start_at(account, now: datetime | None = None) -> datetime:
"""该账号在 now 所在月的开始时刻。2 月 31 日等按当月最后一天。"""
current = as_cn(now)
day, hour, minute, second = validate_start(
int(getattr(account, "study_start_day", 1) or 1),
int(getattr(account, "study_start_hour", 0) or 0),
@@ -68,7 +78,7 @@ def window_open(account, now: datetime | None = None) -> bool:
int(getattr(account, "study_start_second", 0) or 0),
)
last = calendar.monthrange(current.year, current.month)[1]
start = datetime(
return datetime(
current.year,
current.month,
min(day, last),
@@ -77,4 +87,28 @@ def window_open(account, now: datetime | None = None) -> bool:
second,
tzinfo=CN_TZ,
)
return current >= start
def window_open(account, now: datetime | None = None) -> bool:
"""当前是否已过该账号本月开始时刻。"""
current = as_cn(now)
return current >= start_at(account, current)
def next_month_start(now: datetime | None = None) -> datetime:
current = as_cn(now)
if current.month == 12:
return datetime(current.year + 1, 1, 1, tzinfo=CN_TZ)
return datetime(current.year, current.month + 1, 1, tzinfo=CN_TZ)
def next_fire_at(account, now: datetime | None = None) -> datetime | None:
"""下次定时触发。本月已到点则等到下月;关闭定时则没有下一次。"""
enabled = getattr(account, "study_schedule_enabled", True)
if enabled is False:
return None
current = as_cn(now)
this = start_at(account, current)
if current < this:
return this
return start_at(account, next_month_start(current))