Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(setup): 整表错误照原样回显,局域网默认先开访问密码

longmeidao4cdd51e693bf
diff --git a/src/peach/routes_pages.py b/src/peach/routes_pages.py
index 223d8b3c8..916f8f698 100644
--- a/src/peach/routes_pages.py
+++ b/src/peach/routes_pages.py
@@ -541,7 +541,13 @@ def setup_page(
fields.append(f'<details{opened}><summary><span class="setting-title">高级设置</span>{_CHEVRON_SVG}</summary>'
+ "".join(advanced) + "</details>")
access_error = str(errors.get("access_password", ""))
- access_enabled = values.get("access_enabled") == "y" or bool(access_error)
+ host_value = next((str(values.get(question.key, question.default))
+ for question in asked if question.key == "host"), "2")
+ # 独立包默认对局域网监听,明文 HTTP 又没有别的门。第一次打开这张表时把密码
+ # 开关按 host 的默认值打开;表单回填时用户自己的选择说了算,`values` 非空就
+ # 只看 `access_enabled`——复选框不勾是不提交的,那正是「他关掉了」。
+ access_enabled = (values.get("access_enabled") == "y" or bool(access_error)
+ or (not values and host_value == "2"))
access_hidden = "" if access_enabled else " hidden"
access_disabled = "" if access_enabled else " disabled"
fields.insert(1, '<div class="field access-field">'
@@ -549,6 +555,7 @@ def setup_page(
'<small>开启后,访问 Peach 需要先登录。</small></span>'
f'<input id="access-enabled" class="ptoggle" name="access_enabled" type="checkbox" role="switch" value="y"'
f'{" checked" if access_enabled else ""}></label>'
+ '<p class="help" id="access-consequence">未设置密码时,能连接到 Peach 的设备可直接进入。</p>'
f'<div class="password-fields" id="access-password-fields"{access_hidden}>'
'<label class="field-label" for="access-password">访问密码</label><span class="entry-input">'
'<input id="access-password" name="access_password" type="password" maxlength="256" '
@@ -776,7 +783,14 @@ async def setup_submit(request: Request):
def _setup_media_source_errors(dirs: Sequence[str], kinds: object,
problems: Sequence[str], validate) -> list[str]:
- """首启的本地来源必须真的可读;CloudDrive 来源仍可离线保存。"""
+ """首启的本地来源必须真的可读;CloudDrive 来源仍可离线保存。
+
+ `media_configuration.validate` 对整表不成立的情况(一行都没填、超过 100 行)
+ 只回一句话,和行数对不上。那种时候原样交回去让页面显示,不能按行去覆盖——
+ 索引会越界,用户看到的是 500 而不是「请添加 1 到 100 个媒体文件夹」。
+ """
+ if problems and len(problems) != len(dirs):
+ return list(problems)
errors = list(problems) if problems else [""] * len(dirs)
source_kinds = list(kinds) if isinstance(kinds, (list, tuple)) else []
for index, path in enumerate(dirs):
diff --git a/tests/test_onboarding.py b/tests/test_onboarding.py
index 8a7664b76..8c3247855 100644
--- a/tests/test_onboarding.py
+++ b/tests/test_onboarding.py
@@ -482,10 +482,11 @@ class SetupPageTests(_Case):
self.assertIn("http://peach.local:8900", body)
# 端口只填写数字,localhost 是不可编辑的固定前缀。
self.assertIn('<div class="affix"><span>localhost:</span><input id="f-port"', body)
- # 密码默认关闭;开启后脚本才显示并启用两项输入。
+ # 默认对局域网监听,所以密码开关默认打开、两项输入直接可填;关掉后脚本再隐藏并禁用它们。
self.assertIn('id="access-enabled" class="ptoggle" name="access_enabled" type="checkbox" role="switch"', body)
- self.assertIn('id="access-password-fields" hidden', body)
+ self.assertNotIn('id="access-password-fields" hidden', body)
self.assertIn("accessFields.hidden=!accessToggle.checked", body)
+ self.assertIn('<p class="help" id="access-consequence">未设置密码时,能连接到 Peach 的设备可直接进入。</p>', body)
for title in ("媒体库", "访问密码", "高级设置", "完成设置后"):
self.assertIn(f'>{title}<', body)
self.assertIn('<h2 class="setting-title" id="setup-options-title">完成设置后</h2>', body)
@@ -541,6 +542,30 @@ class SetupPageTests(_Case):
self.assertFalse(missing.exists(), "校验不替用户创建媒体库")
self.assertFalse(self.data_root.exists(), "校验失败不写设置")
+ def test_the_lan_default_arms_the_access_password_and_says_what_no_password_means(self):
+ body = self._get("/").text
+ toggle = body[body.index('id="access-enabled"'):]
+ self.assertIn('value="y" checked>', toggle[:toggle.index("</label>")])
+ self.assertIn('未设置密码时,能连接到 Peach 的设备可直接进入。', body)
+ self.assertLess(body.index('id="access-enabled"'), body.index('id="access-consequence"'))
+ self.assertLess(body.index('id="access-consequence"'), body.index('id="access-password-fields"'))
+
+ def test_turning_the_access_password_off_survives_a_failed_submission(self):
+ # 复选框不勾就不提交,回填时那正是「他关掉了」,不能被局域网默认又打开一次。
+ response = self._post("/setup", self._form(port="0", access_enabled=None))
+ self.assertEqual(response.status_code, 400)
+ toggle = response.text[response.text.index('id="access-enabled"'):]
+ self.assertNotIn(' checked', toggle[:toggle.index("</label>")])
+
+ def test_more_rows_than_the_limit_come_back_as_a_form_not_a_crash(self):
+ # 整表不成立时校验器只回一句话,长度与行数对不上;按行覆盖会越界,用户拿到的是 500。
+ response = self._post("/setup", self._form(
+ media_dir=[str(self.media)] * 101, media_location=["local"] * 101,
+ media_root=[r"R:\media"] * 101))
+ self.assertEqual(response.status_code, 400)
+ self.assertIn("请添加 1 到 100 个媒体文件夹", response.text)
+ self.assertFalse(self.data_root.exists(), "校验失败不写设置")
+
def test_two_directories_are_declared_together_and_shown_in_the_scan_label(self):
second = self.root / "more"
second.mkdir()