fix: 按日期统计时保留翻页状态
This commit is contained in:
+58
-11
@@ -375,6 +375,50 @@ def day_span(when: datetime) -> tuple[str, str]:
|
|||||||
return text, text
|
return text, text
|
||||||
|
|
||||||
|
|
||||||
|
def ajax_hidden_fields(payload: str) -> dict[str, str]:
|
||||||
|
"""读出我的学时翻页回包里的视图状态。回包不是整页,隐藏域在末尾的分段里。"""
|
||||||
|
fields: dict[str, str] = {}
|
||||||
|
text = payload or ""
|
||||||
|
index = 0
|
||||||
|
size = len(text)
|
||||||
|
while index < size:
|
||||||
|
bar = text.find("|", index)
|
||||||
|
if bar < 0:
|
||||||
|
break
|
||||||
|
length_text = text[index:bar]
|
||||||
|
if not length_text.isdigit():
|
||||||
|
index = bar + 1
|
||||||
|
continue
|
||||||
|
length = int(length_text)
|
||||||
|
index = bar + 1
|
||||||
|
bar = text.find("|", index)
|
||||||
|
if bar < 0:
|
||||||
|
break
|
||||||
|
kind = text[index:bar]
|
||||||
|
index = bar + 1
|
||||||
|
bar = text.find("|", index)
|
||||||
|
if bar < 0:
|
||||||
|
break
|
||||||
|
field_id = text[index:bar]
|
||||||
|
index = bar + 1
|
||||||
|
if index + length > size:
|
||||||
|
break
|
||||||
|
content = text[index:index + length]
|
||||||
|
index += length
|
||||||
|
if index < size and text[index] == "|":
|
||||||
|
index += 1
|
||||||
|
if kind == "hiddenField":
|
||||||
|
fields[field_id] = content
|
||||||
|
return fields
|
||||||
|
|
||||||
|
|
||||||
|
def _carry_state(form: dict[str, str], html: str) -> dict[str, str]:
|
||||||
|
for key, value in ajax_hidden_fields(html).items():
|
||||||
|
if key.startswith("__"):
|
||||||
|
form[key] = value
|
||||||
|
return form
|
||||||
|
|
||||||
|
|
||||||
def period_search_form(html: str, start: str, end: str) -> dict[str, str]:
|
def period_search_form(html: str, start: str, end: str) -> dict[str, str]:
|
||||||
form = training_form(html)
|
form = training_form(html)
|
||||||
form["txtStartDate"] = start
|
form["txtStartDate"] = start
|
||||||
@@ -385,7 +429,7 @@ def period_search_form(html: str, start: str, end: str) -> dict[str, str]:
|
|||||||
form["__EVENTARGUMENT"] = ""
|
form["__EVENTARGUMENT"] = ""
|
||||||
form["__ASYNCPOST"] = "true"
|
form["__ASYNCPOST"] = "true"
|
||||||
form["btnSearch"] = "搜索"
|
form["btnSearch"] = "搜索"
|
||||||
return form
|
return _carry_state(form, html)
|
||||||
|
|
||||||
|
|
||||||
def period_form(html: str, argument: str, page_nav: str | None = None) -> dict[str, str]:
|
def period_form(html: str, argument: str, page_nav: str | None = None) -> dict[str, str]:
|
||||||
@@ -396,7 +440,7 @@ def period_form(html: str, argument: str, page_nav: str | None = None) -> dict[s
|
|||||||
form["__ASYNCPOST"] = "true"
|
form["__ASYNCPOST"] = "true"
|
||||||
if page_nav is not None:
|
if page_nav is not None:
|
||||||
form["pageNav"] = page_nav
|
form["pageNav"] = page_nav
|
||||||
return form
|
return _carry_state(form, html)
|
||||||
|
|
||||||
|
|
||||||
def period_has_next(html: str, page: int) -> bool:
|
def period_has_next(html: str, page: int) -> bool:
|
||||||
@@ -1396,15 +1440,6 @@ class EunivClient:
|
|||||||
def _rows_for_range(self, start: str | None = None, end: str | None = None) -> list[dict]:
|
def _rows_for_range(self, start: str | None = None, end: str | None = None) -> list[dict]:
|
||||||
"""读取我的学时表格。给出日期时先按这个范围搜索,再按每页 100 条取完。"""
|
"""读取我的学时表格。给出日期时先按这个范围搜索,再按每页 100 条取完。"""
|
||||||
html = self._open_period()
|
html = self._open_period()
|
||||||
if start:
|
|
||||||
html = self._post_period(period_search_form(html, start, end or start))
|
|
||||||
nav = training_form(html).get("pageNav", "")
|
|
||||||
if not nav.endswith("|100"):
|
|
||||||
form = period_form(html, "current", "1|0|100")
|
|
||||||
if start:
|
|
||||||
form["txtStartDate"] = start
|
|
||||||
form["txtEndDate"] = end or start
|
|
||||||
html = self._post_period(form)
|
|
||||||
items: list[dict] = []
|
items: list[dict] = []
|
||||||
seen: set[tuple] = set()
|
seen: set[tuple] = set()
|
||||||
|
|
||||||
@@ -1419,6 +1454,18 @@ class EunivClient:
|
|||||||
added += 1
|
added += 1
|
||||||
return added
|
return added
|
||||||
|
|
||||||
|
if start:
|
||||||
|
html = self._post_period(period_search_form(html, start, end or start))
|
||||||
|
take(html)
|
||||||
|
nav = training_form(html).get("pageNav", "")
|
||||||
|
if not nav.endswith("|100"):
|
||||||
|
form = period_form(html, "current", "1|0|100")
|
||||||
|
if start:
|
||||||
|
form["txtStartDate"] = start
|
||||||
|
form["txtEndDate"] = end or start
|
||||||
|
widened = self._post_period(form)
|
||||||
|
if history_rows_from_page(widened):
|
||||||
|
html = widened
|
||||||
take(html)
|
take(html)
|
||||||
limited = False
|
limited = False
|
||||||
for page in range(2, 51):
|
for page in range(2, 51):
|
||||||
|
|||||||
@@ -1212,6 +1212,17 @@ def test_history_course_path_follows_type_and_keeps_explicit_url() -> None:
|
|||||||
assert named["path"] == "/kng/view/package/abc.html"
|
assert named["path"] == "/kng/view/package/abc.html"
|
||||||
|
|
||||||
|
|
||||||
|
def test_ajax_reply_keeps_the_next_page_state() -> None:
|
||||||
|
from euniv.client import ajax_hidden_fields, period_form
|
||||||
|
|
||||||
|
html = "1|#||4|5|updatePanel|upList|<tr></tr>|8|hiddenField|__VIEWSTATEGENERATOR|A3C7C079|3|hiddenField|__VIEWSTATE|abc|"
|
||||||
|
assert ajax_hidden_fields(html)["__VIEWSTATE"] == "abc"
|
||||||
|
assert ajax_hidden_fields(html)["__VIEWSTATEGENERATOR"] == "A3C7C079"
|
||||||
|
form = period_form(html, "2")
|
||||||
|
assert form["__VIEWSTATE"] == "abc"
|
||||||
|
assert form["__EVENTARGUMENT"] == "2"
|
||||||
|
|
||||||
|
|
||||||
def test_month_span_covers_the_whole_month() -> None:
|
def test_month_span_covers_the_whole_month() -> None:
|
||||||
from euniv.client import day_span, month_span, period_search_form
|
from euniv.client import day_span, month_span, period_search_form
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user