feat: 定时开始日改为每月5到25号,默认8号
This commit is contained in:
@@ -83,7 +83,7 @@ def list_users(db: Session) -> list[dict]:
|
||||
"has_proxy": enabled(account),
|
||||
"proxy": summary_of(account),
|
||||
"study_start": start_label(account),
|
||||
"study_start_day": int(account.study_start_day or 1),
|
||||
"study_start_day": int(account.study_start_day or 8),
|
||||
"study_start_hour": int(account.study_start_hour or 0),
|
||||
"study_start_minute": int(account.study_start_minute or 0),
|
||||
"study_start_second": int(account.study_start_second or 0),
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
"""定时开始日限制为每月 5 到 25 号,超出范围的旧值改为 8 号。"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "0007_study_start_day"
|
||||
down_revision = "0006_employee_width"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("UPDATE accounts SET study_start_day = 8 WHERE study_start_day < 5 OR study_start_day > 25")
|
||||
with op.batch_alter_table("accounts") as batch:
|
||||
batch.alter_column(
|
||||
"study_start_day",
|
||||
existing_type=sa.Integer(),
|
||||
existing_nullable=False,
|
||||
server_default="8",
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("accounts") as batch:
|
||||
batch.alter_column(
|
||||
"study_start_day",
|
||||
existing_type=sa.Integer(),
|
||||
existing_nullable=False,
|
||||
server_default="1",
|
||||
)
|
||||
@@ -63,7 +63,7 @@ class Account(Base):
|
||||
proxy_port: Mapped[int | None] = mapped_column(nullable=True)
|
||||
proxy_username: Mapped[str] = mapped_column(String(128), nullable=False, default="", server_default="")
|
||||
proxy_password_cipher: Mapped[str] = mapped_column(Text, nullable=False, default="", server_default="")
|
||||
study_start_day: Mapped[int] = mapped_column(nullable=False, default=1, server_default="1")
|
||||
study_start_day: Mapped[int] = mapped_column(nullable=False, default=8, server_default="8")
|
||||
study_start_hour: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
study_start_minute: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
study_start_second: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
|
||||
@@ -6,6 +6,9 @@ import calendar
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
CN_TZ = timezone(timedelta(hours=8))
|
||||
DAY_MIN = 5
|
||||
DAY_MAX = 25
|
||||
DAY_DEFAULT = 8
|
||||
|
||||
|
||||
class StudyWindowError(ValueError):
|
||||
@@ -14,7 +17,7 @@ class StudyWindowError(ValueError):
|
||||
|
||||
def validate_start(day: object, hour: object, minute: object, second: object) -> tuple[int, int, int, int]:
|
||||
for name, value, low, high in (
|
||||
("日", day, 1, 31),
|
||||
("日", day, DAY_MIN, DAY_MAX),
|
||||
("时", hour, 0, 23),
|
||||
("分", minute, 0, 59),
|
||||
("秒", second, 0, 59),
|
||||
@@ -34,10 +37,22 @@ def _clamp_int(value: object, low: int, high: int, default: int) -> int:
|
||||
return min(high, max(low, number))
|
||||
|
||||
|
||||
def _stored_day(value: object) -> int:
|
||||
if isinstance(value, bool):
|
||||
return DAY_DEFAULT
|
||||
try:
|
||||
number = int(value) # type: ignore[arg-type]
|
||||
except (TypeError, ValueError):
|
||||
return DAY_DEFAULT
|
||||
if DAY_MIN <= number <= DAY_MAX:
|
||||
return number
|
||||
return DAY_DEFAULT
|
||||
|
||||
|
||||
def stored_start(account) -> tuple[int, int, int, int]:
|
||||
"""读取时把非法时刻收进合法范围,避免首页状态接口报错。"""
|
||||
return (
|
||||
_clamp_int(getattr(account, "study_start_day", 1), 1, 31, 1),
|
||||
_stored_day(getattr(account, "study_start_day", DAY_DEFAULT)),
|
||||
_clamp_int(getattr(account, "study_start_hour", 0), 0, 23, 0),
|
||||
_clamp_int(getattr(account, "study_start_minute", 0), 0, 59, 0),
|
||||
_clamp_int(getattr(account, "study_start_second", 0), 0, 59, 0),
|
||||
|
||||
@@ -616,12 +616,12 @@ def test_user_can_set_monthly_study_start() -> None:
|
||||
empty = client.get("/api/settings/study-start")
|
||||
assert empty.status_code == 200
|
||||
assert empty.json() == {
|
||||
"day": 1,
|
||||
"day": 8,
|
||||
"hour": 0,
|
||||
"minute": 0,
|
||||
"second": 0,
|
||||
"enabled": True,
|
||||
"label": "每月1日 00:00:00",
|
||||
"label": "每月8日 00:00:00",
|
||||
}
|
||||
saved = client.put("/api/settings/study-start", json={"day": 5, "hour": 16, "minute": 0, "second": 7, "enabled": False})
|
||||
assert saved.status_code == 200
|
||||
@@ -629,8 +629,10 @@ def test_user_can_set_monthly_study_start() -> None:
|
||||
assert saved.json()["enabled"] is False
|
||||
again = client.put("/api/settings/study-start", json={"day": 5, "hour": 16, "minute": 0, "second": 7, "enabled": True})
|
||||
assert again.json()["enabled"] is True
|
||||
bad = client.put("/api/settings/study-start", json={"day": 32, "hour": 16, "minute": 0, "second": 0})
|
||||
bad = client.put("/api/settings/study-start", json={"day": 4, "hour": 16, "minute": 0, "second": 0})
|
||||
assert bad.status_code == 400
|
||||
too_late = client.put("/api/settings/study-start", json={"day": 26, "hour": 16, "minute": 0, "second": 0})
|
||||
assert too_late.status_code == 400
|
||||
|
||||
|
||||
def _freeze_beijing(monkeypatch, year: int, month: int, day: int) -> None:
|
||||
@@ -656,7 +658,7 @@ def test_user_can_start_and_stop_study_immediately(monkeypatch) -> None:
|
||||
client = TestClient(api_module.app)
|
||||
_freeze_beijing(monkeypatch, 2026, 11, 15)
|
||||
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
|
||||
client.put("/api/settings/study-start", json={"day": 31, "hour": 23, "minute": 59, "second": 59})
|
||||
client.put("/api/settings/study-start", json={"day": 25, "hour": 23, "minute": 59, "second": 59})
|
||||
idle = client.get("/api/settings/study-control")
|
||||
assert idle.status_code == 200
|
||||
assert idle.json()["workers"] == []
|
||||
@@ -714,7 +716,7 @@ def test_study_control_websocket_sends_snapshot(monkeypatch) -> None:
|
||||
ws.receive_json()
|
||||
assert closed.value.code == 4401
|
||||
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
|
||||
client.put("/api/settings/study-start", json={"day": 31, "hour": 23, "minute": 59, "second": 59})
|
||||
client.put("/api/settings/study-start", json={"day": 25, "hour": 23, "minute": 59, "second": 59})
|
||||
with client.websocket_connect("/api/settings/study-control/ws") as ws:
|
||||
data = ws.receive_json()
|
||||
assert data["started"] is False
|
||||
|
||||
@@ -406,15 +406,18 @@ def test_study_window_opens_after_configured_time() -> None:
|
||||
assert window_open(account, datetime(2026, 12, 5, 15, 0, 0, tzinfo=CN_TZ)) is False
|
||||
|
||||
|
||||
def test_study_window_clamps_missing_day() -> None:
|
||||
def test_study_window_clamps_day_into_allowed_range() -> None:
|
||||
account = _Start(31, 16, 0, 0)
|
||||
assert window_open(account, datetime(2026, 2, 28, 15, 59, 59, tzinfo=CN_TZ)) is False
|
||||
assert window_open(account, datetime(2026, 2, 28, 16, 0, 0, tzinfo=CN_TZ)) is True
|
||||
assert window_open(account, datetime(2026, 2, 8, 15, 59, 59, tzinfo=CN_TZ)) is False
|
||||
assert window_open(account, datetime(2026, 2, 8, 16, 0, 0, tzinfo=CN_TZ)) is True
|
||||
early = _Start(1, 16, 0, 0)
|
||||
assert window_open(early, datetime(2026, 11, 8, 15, 59, 59, tzinfo=CN_TZ)) is False
|
||||
assert window_open(early, datetime(2026, 11, 8, 16, 0, 0, tzinfo=CN_TZ)) is True
|
||||
|
||||
|
||||
def test_immediate_start_skips_monthly_window() -> None:
|
||||
frozen = datetime(2026, 11, 15, 12, 0, tzinfo=CN_TZ)
|
||||
account = _Start(28, 23, 59, 59)
|
||||
account = _Start(25, 23, 59, 59)
|
||||
assert study_eligible(account, frozen) is False
|
||||
start_now(account)
|
||||
assert account.study_paused is False
|
||||
@@ -461,22 +464,22 @@ def test_hours_met_clears_started_without_pause() -> None:
|
||||
|
||||
|
||||
def test_stop_this_month_does_not_block_next_month_schedule() -> None:
|
||||
account = _Start(1, 0, 0, 0)
|
||||
account = _Start(5, 0, 0, 0)
|
||||
start_now(account)
|
||||
stop_now(account, datetime(2026, 11, 15, 12, 0, tzinfo=CN_TZ))
|
||||
assert study_eligible(account, datetime(2026, 11, 20, 8, 0, tzinfo=CN_TZ)) is False
|
||||
assert study_view(account, datetime(2026, 12, 1, 0, 0, tzinfo=CN_TZ))["paused"] is False
|
||||
assert study_eligible(account, datetime(2026, 12, 1, 0, 0, tzinfo=CN_TZ)) is True
|
||||
assert study_view(account, datetime(2026, 12, 5, 0, 0, tzinfo=CN_TZ))["paused"] is False
|
||||
assert study_eligible(account, datetime(2026, 12, 5, 0, 0, tzinfo=CN_TZ)) is True
|
||||
|
||||
|
||||
def test_stale_month_hours_do_not_block_next_month() -> None:
|
||||
account = _Start(1, 0, 0, 0)
|
||||
account = _Start(5, 0, 0, 0)
|
||||
account.monthly_target = 70
|
||||
account.month_hours = "78"
|
||||
account.hours_month = "2026-11"
|
||||
assert hours_met(account, datetime(2026, 11, 20, tzinfo=CN_TZ)) is True
|
||||
assert hours_met(account, datetime(2026, 12, 1, tzinfo=CN_TZ)) is False
|
||||
assert study_eligible(account, datetime(2026, 12, 1, tzinfo=CN_TZ)) is True
|
||||
assert hours_met(account, datetime(2026, 12, 5, tzinfo=CN_TZ)) is False
|
||||
assert study_eligible(account, datetime(2026, 12, 5, tzinfo=CN_TZ)) is True
|
||||
|
||||
|
||||
def test_work_running_is_wanted_or_studying() -> None:
|
||||
@@ -623,9 +626,13 @@ def test_finish_user_keeps_failure_and_clears_idle() -> None:
|
||||
|
||||
def test_study_window_rejects_invalid_parts() -> None:
|
||||
with pytest.raises(StudyWindowError):
|
||||
validate_start(32, 0, 0, 0)
|
||||
validate_start(4, 0, 0, 0)
|
||||
with pytest.raises(StudyWindowError):
|
||||
validate_start(1, 24, 0, 0)
|
||||
validate_start(26, 0, 0, 0)
|
||||
with pytest.raises(StudyWindowError):
|
||||
validate_start(8, 24, 0, 0)
|
||||
assert validate_start(5, 0, 0, 0) == (5, 0, 0, 0)
|
||||
assert validate_start(25, 23, 59, 59) == (25, 23, 59, 59)
|
||||
|
||||
|
||||
def test_course_workers_default_and_range() -> None:
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NCheckbox, NDropdown, NForm, NFormItem, NIcon, NInputNumber, NModal, NRadioButton, NRadioGroup, NSelect, NTimePicker, NTooltip, useDialog, type DropdownOption } from "naive-ui";
|
||||
import { EllipsisHorizontalOutline, EllipsisVerticalOutline, PauseOutline, PlayOutline, SettingsOutline, WarningOutline } from "@vicons/ionicons5";
|
||||
import { EllipsisHorizontalOutline, EllipsisVerticalOutline, PauseOutline, PlayOutline, SettingsOutline } from "@vicons/ionicons5";
|
||||
import ProxyFields from "../../shared/ProxyFields.vue";
|
||||
import { dayOptions, workerOptions } from "../../shared/studyFields";
|
||||
import { STUDY_DAY_DEFAULT, STUDY_DAY_MAX, STUDY_DAY_MIN, dayOptions, workerOptions } from "../../shared/studyFields";
|
||||
import { isRecord, textOf } from "../../shared/text";
|
||||
|
||||
interface UserRow {
|
||||
@@ -56,7 +56,7 @@ const settingsSaving = ref(false);
|
||||
const settingsError = ref("");
|
||||
const settingsIds = ref<string[]>([]);
|
||||
const monthlyTarget = ref<number | null>(600);
|
||||
const startDay = ref<number | null>(1);
|
||||
const startDay = ref<number | null>(STUDY_DAY_DEFAULT);
|
||||
const startHour = ref<number | null>(0);
|
||||
const startMinute = ref<number | null>(0);
|
||||
const startSecond = ref<number | null>(0);
|
||||
@@ -163,7 +163,7 @@ function asUser(value: unknown): UserRow | null {
|
||||
has_proxy: value.has_proxy === true,
|
||||
proxy: textOf(value.proxy),
|
||||
study_start: textOf(value.study_start),
|
||||
study_start_day: typeof value.study_start_day === "number" ? value.study_start_day : 1,
|
||||
study_start_day: typeof value.study_start_day === "number" ? value.study_start_day : STUDY_DAY_DEFAULT,
|
||||
study_start_hour: typeof value.study_start_hour === "number" ? value.study_start_hour : 0,
|
||||
study_start_minute: typeof value.study_start_minute === "number" ? value.study_start_minute : 0,
|
||||
study_start_second: typeof value.study_start_second === "number" ? value.study_start_second : 0,
|
||||
@@ -598,14 +598,17 @@ async function saveSettings() {
|
||||
return;
|
||||
}
|
||||
if (scheduleTouched && (
|
||||
!integerInRange(startDay.value, 1, 31)
|
||||
|| !integerInRange(startHour.value, 0, 23)
|
||||
!integerInRange(startHour.value, 0, 23)
|
||||
|| !integerInRange(startMinute.value, 0, 59)
|
||||
|| !integerInRange(startSecond.value, 0, 59)
|
||||
)) {
|
||||
settingsError.value = "请填写完整的开始时刻";
|
||||
return;
|
||||
}
|
||||
if (scheduleTouched && !integerInRange(startDay.value, STUDY_DAY_MIN, STUDY_DAY_MAX)) {
|
||||
settingsError.value = "每月开始日须为 5 到 25 的整数";
|
||||
return;
|
||||
}
|
||||
if (workersChanged && !integerInRange(workersValue.value, 1, 8)) {
|
||||
settingsError.value = "同时学习课程数须为 1 到 8";
|
||||
return;
|
||||
@@ -904,14 +907,6 @@ onMounted(() => {
|
||||
<n-form-item label="开始时刻">
|
||||
<div class="target-control start-control">
|
||||
<n-select v-model:value="startDay" :options="dayOptions" class="day-select" placeholder="保持不变" :disabled="scheduleTimeDisabled" />
|
||||
<n-tooltip v-if="!scheduleTimeDisabled && startDay === 1" placement="top">
|
||||
<template #trigger>
|
||||
<button type="button" class="hint-icon day-warn" aria-label="开始时刻说明">
|
||||
<n-icon><WarningOutline /></n-icon>
|
||||
</button>
|
||||
</template>
|
||||
公共代理不稳定,学习可能中断
|
||||
</n-tooltip>
|
||||
<n-time-picker v-model:formatted-value="startTime" value-format="HH:mm:ss" :actions="null" placeholder="保持不变" :disabled="scheduleTimeDisabled" />
|
||||
</div>
|
||||
</n-form-item>
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
||||
import { NAlert, NButton, NForm, NFormItem, NIcon, NInputNumber, NModal, NProgress, NSelect, NSwitch, NTimePicker, NTooltip } from "naive-ui";
|
||||
import { GlobeOutline, PlayOutline, RefreshOutline, StopOutline, WarningOutline } from "@vicons/ionicons5";
|
||||
import { GlobeOutline, PlayOutline, RefreshOutline, StopOutline } from "@vicons/ionicons5";
|
||||
import ProxyFields from "../../shared/ProxyFields.vue";
|
||||
import { dayOptions, workerOptions } from "../../shared/studyFields";
|
||||
import { STUDY_DAY_DEFAULT, STUDY_DAY_MAX, STUDY_DAY_MIN, dayOptions, workerOptions } from "../../shared/studyFields";
|
||||
import { failureText } from "../../shared/text";
|
||||
|
||||
interface HomeSummary {
|
||||
@@ -285,7 +285,7 @@ async function loadTarget() {
|
||||
|
||||
const startLoaded = ref(false);
|
||||
const scheduleEnabled = ref(true);
|
||||
const startDay = ref<number | null>(1);
|
||||
const startDay = ref<number | null>(STUDY_DAY_DEFAULT);
|
||||
const startHour = ref<number | null>(0);
|
||||
const startMinute = ref<number | null>(0);
|
||||
const startSecond = ref<number | null>(0);
|
||||
@@ -309,7 +309,7 @@ async function loadStart() {
|
||||
const res = await fetch("/api/settings/study-start");
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
startDay.value = typeof data.day === "number" ? data.day : 1;
|
||||
startDay.value = typeof data.day === "number" ? data.day : STUDY_DAY_DEFAULT;
|
||||
startHour.value = typeof data.hour === "number" ? data.hour : 0;
|
||||
startMinute.value = typeof data.minute === "number" ? data.minute : 0;
|
||||
startSecond.value = typeof data.second === "number" ? data.second : 0;
|
||||
@@ -359,10 +359,15 @@ async function saveSchedule() {
|
||||
if (
|
||||
day == null || hour == null || minute == null || second == null
|
||||
|| !Number.isInteger(day) || !Number.isInteger(hour) || !Number.isInteger(minute) || !Number.isInteger(second)
|
||||
|| hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59
|
||||
) {
|
||||
scheduleError.value = "请填写完整的开始时刻";
|
||||
return;
|
||||
}
|
||||
if (day < STUDY_DAY_MIN || day > STUDY_DAY_MAX) {
|
||||
scheduleError.value = "每月开始日须为 5 到 25 的整数";
|
||||
return;
|
||||
}
|
||||
if (workers == null || !Number.isInteger(workers) || workers < 1 || workers > 8) {
|
||||
scheduleError.value = "同时学习课程数须为 1 到 8";
|
||||
return;
|
||||
@@ -826,14 +831,6 @@ onUnmounted(() => {
|
||||
<n-form-item v-if="startLoaded" label="开始时刻">
|
||||
<div class="start-control">
|
||||
<n-select v-model:value="startDay" size="small" :options="dayOptions" class="day-select" :disabled="!scheduleEnabled" />
|
||||
<n-tooltip v-if="scheduleEnabled && startDay === 1" placement="top">
|
||||
<template #trigger>
|
||||
<button type="button" class="hint-icon day-warn" aria-label="开始时刻说明">
|
||||
<n-icon><WarningOutline /></n-icon>
|
||||
</button>
|
||||
</template>
|
||||
公共代理不稳定,学习可能中断
|
||||
</n-tooltip>
|
||||
<n-time-picker
|
||||
class="start-time"
|
||||
v-model:formatted-value="startTime"
|
||||
|
||||
@@ -9,7 +9,11 @@ export const workerOptions = [
|
||||
{ label: "8", value: 8 },
|
||||
];
|
||||
|
||||
export const dayOptions = Array.from({ length: 31 }, (_, index) => ({
|
||||
label: `每月 ${index + 1} 日`,
|
||||
value: index + 1,
|
||||
}));
|
||||
export const STUDY_DAY_MIN = 5;
|
||||
export const STUDY_DAY_MAX = 25;
|
||||
export const STUDY_DAY_DEFAULT = 8;
|
||||
|
||||
export const dayOptions = Array.from({ length: STUDY_DAY_MAX - STUDY_DAY_MIN + 1 }, (_, index) => {
|
||||
const day = STUDY_DAY_MIN + index;
|
||||
return { label: `每月 ${day} 日`, value: day };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user