feat: 完善登录保持、栅格下载和任务调度

This commit is contained in:
2026-10-25 05:42:24 +08:00
parent dab26ff4ee
commit 4cf64957c5
25 changed files with 1871 additions and 320 deletions
+17 -19
View File
@@ -10,9 +10,12 @@ _HEADER_KEYS = {
"栅格id",
"20米栅格id",
"grid20_id",
"grid50_id",
"grid_id",
"栅格",
"栅格编号",
"20米栅格id",
"50米栅格id",
}
@@ -20,6 +23,8 @@ def normalize_id(value) -> str:
if value is None:
return ""
text = str(value).strip().strip("\ufeff")
text = text.translate(str.maketrans("0123456789", "0123456789"))
text = "".join(text.split())
if text.endswith(".0") and text[:-2].isdigit():
text = text[:-2]
if not text or not text.isdigit():
@@ -31,16 +36,16 @@ def _header_index(header: list[str]) -> int | None:
for index, name in enumerate(header):
key = name.strip().lower().replace(" ", "").replace("_", "")
raw = name.strip().lower().replace(" ", "")
if raw in _HEADER_KEYS or key in {"栅格id", "20米栅格id", "grid20id", "gridid"}:
if raw in _HEADER_KEYS or key in {"栅格id", "20米栅格id", "50米栅格id", "grid20id", "grid50id", "gridid"}:
return index
if "栅格" in name and "id" in raw:
return index
return None
def parse_table(rows: list[list]) -> list[str]:
def parse_table(rows: list[list]) -> tuple[list[str], int]:
if not rows:
return []
return [], 0
header = ["" if cell is None else str(cell).strip() for cell in rows[0]]
column = _header_index(header)
start = 0
@@ -52,15 +57,19 @@ def parse_table(rows: list[list]) -> list[str]:
start = 1
found: list[str] = []
seen: set[str] = set()
duplicates = 0
for row in rows[start:]:
if column >= len(row):
continue
grid_id = normalize_id(row[column])
if not grid_id or grid_id in seen:
if not grid_id:
continue
if grid_id in seen:
duplicates += 1
continue
seen.add(grid_id)
found.append(grid_id)
return found
return found, duplicates
def _decode_csv(raw: bytes) -> str:
@@ -73,23 +82,12 @@ def _decode_csv(raw: bytes) -> str:
def parse_upload(path: Path) -> list[str]:
suffix = path.suffix.lower()
if suffix == ".xlsx":
from openpyxl import load_workbook
book = load_workbook(path, read_only=True, data_only=True)
try:
sheet = book.active
rows = [list(row) for row in sheet.iter_rows(values_only=True)]
finally:
book.close()
return parse_table(rows)
if suffix not in {".csv", ".txt", ""}:
raise ValueError("区域表只支持 CSV、TXT 或 XLSX")
if path.suffix.lower() != ".csv":
raise ValueError("区域表只支持 CSV")
text = _decode_csv(path.read_bytes())
sample = text[:4096]
try:
dialect = csv.Sniffer().sniff(sample, delimiters=",\t;|")
dialect = csv.Sniffer().sniff(sample, delimiters=",\t;")
except csv.Error:
dialect = csv.excel
reader = csv.reader(io.StringIO(text), dialect)