1. 历史记录:批次合并视图新增"批量反撤销"(需反撤销权限);单条/批量撤销与反撤销提示更明确(说明记录去向,不会删除);操作列权限门控补充 restore_records 2. 操作日志:批量撤销/反撤销写入日志;单条撤销/反撤销/加减分日志详情补充学生姓名(学号)、分值变动、原因;新增"详情关键词"筛选(学生姓名/原因等) 3. 节假日接口:证书校验失败自动回退重试,提高服务器兼容性 4. 作业按次登记:默认列出全部学生、缺交次数默认0(0=已交),支持全员已交登记;后端允许空缺交名单
110 lines
3.8 KiB
Python
110 lines
3.8 KiB
Python
# ===========================================
|
|
# 班级操行分管理系统 - 后端服务
|
|
#
|
|
# 开发者: Canglan
|
|
# 联系方式: admin@sea-studio.top
|
|
# 版权归属: Sea Network Technology Studio
|
|
# 许可证: MIT License
|
|
#
|
|
# 版权所有 © Sea Network Technology Studio
|
|
# ===========================================
|
|
|
|
"""节假日服务:在线获取法定节假日与调休补班数据(带缓存与失败降级)"""
|
|
|
|
import json
|
|
import ssl
|
|
import time
|
|
import asyncio
|
|
import urllib.error
|
|
import urllib.request
|
|
from datetime import date
|
|
from typing import Dict, Any
|
|
|
|
from utils.logger import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
HOLIDAY_API = 'https://timor.tech/api/holiday/year/{year}'
|
|
CACHE_TTL = 12 * 3600
|
|
FAIL_CACHE_TTL = 600
|
|
REQUEST_TIMEOUT = 6
|
|
|
|
_cache: Dict[int, Dict[str, Any]] = {}
|
|
|
|
|
|
def _fetch_year_sync(year: int) -> Dict[str, Any]:
|
|
"""同步请求节假日接口(在线程池中调用)"""
|
|
request = urllib.request.Request(
|
|
HOLIDAY_API.format(year=year),
|
|
headers={'User-Agent': 'ClassManager/4.5 (HolidayFetcher)'}
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=REQUEST_TIMEOUT) as response:
|
|
payload = json.loads(response.read().decode('utf-8'))
|
|
except (ssl.SSLError, urllib.error.URLError) as e:
|
|
# 部分服务器根证书较旧,回退为不校验证书重试一次(节假日数据非敏感)
|
|
logger.warning(f"节假日接口证书校验失败,回退重试: {e}")
|
|
context = ssl._create_unverified_context()
|
|
with urllib.request.urlopen(request, timeout=REQUEST_TIMEOUT, context=context) as response:
|
|
payload = json.loads(response.read().decode('utf-8'))
|
|
|
|
if payload.get('code') != 0:
|
|
raise ValueError(f"接口返回错误码: {payload.get('code')}")
|
|
|
|
holidays: Dict[str, str] = {}
|
|
workdays: Dict[str, str] = {}
|
|
for day, info in (payload.get('holiday') or {}).items():
|
|
d = str(info.get('date') or f'{year}-{day}')[:10]
|
|
if info.get('holiday'):
|
|
holidays[d] = info.get('name') or '节假日'
|
|
else:
|
|
workdays[d] = info.get('name') or '调休补班'
|
|
|
|
return {'holidays': holidays, 'workdays': workdays, 'api_ok': True}
|
|
|
|
|
|
async def get_year_data(year: int) -> Dict[str, Any]:
|
|
"""获取某年的节假日/调休数据(内存缓存,接口失败自动降级)"""
|
|
now = time.time()
|
|
cached = _cache.get(year)
|
|
if cached:
|
|
ttl = CACHE_TTL if cached.get('api_ok') else FAIL_CACHE_TTL
|
|
if now - cached['fetched_at'] < ttl:
|
|
return cached
|
|
|
|
try:
|
|
loop = asyncio.get_running_loop()
|
|
data = await loop.run_in_executor(None, _fetch_year_sync, year)
|
|
data['fetched_at'] = now
|
|
_cache[year] = data
|
|
logger.info(
|
|
f"节假日数据获取成功: {year}年,放假{len(data['holidays'])}天,调休补班{len(data['workdays'])}天"
|
|
)
|
|
return data
|
|
except Exception as e:
|
|
logger.warning(f"节假日接口获取失败({year}): {e}")
|
|
failed = {'holidays': {}, 'workdays': {}, 'api_ok': False, 'fetched_at': now}
|
|
_cache[year] = failed
|
|
return failed
|
|
|
|
|
|
async def get_range_data(start: date, end: date) -> Dict[str, Any]:
|
|
"""获取日期范围内的节假日/调休数据"""
|
|
holidays: Dict[str, str] = {}
|
|
workdays: Dict[str, str] = {}
|
|
api_ok = True
|
|
|
|
for year in range(start.year, end.year + 1):
|
|
data = await get_year_data(year)
|
|
holidays.update(data['holidays'])
|
|
workdays.update(data['workdays'])
|
|
api_ok = api_ok and data['api_ok']
|
|
|
|
start_iso = start.isoformat()
|
|
end_iso = end.isoformat()
|
|
return {
|
|
'holidays': {d: n for d, n in holidays.items() if start_iso <= d <= end_iso},
|
|
'workdays': {d: n for d, n in workdays.items() if start_iso <= d <= end_iso},
|
|
'api_ok': api_ok
|
|
}
|