Peach: Local-first video metadata scraper and media manager.
fix(web): 资料页的目录图集按张数计,同一张图挂几行也只算一次
longmeidao763fd5bf651a
diff --git a/src/peach/web_entity.py b/src/peach/web_entity.pyindex 93b5cfdd1..e71c69932 100644--- a/src/peach/web_entity.py+++ b/src/peach/web_entity.py@@ -398,7 +398,7 @@ def q_entity_photos(contract: WebContract, args):row = resolve_entity(c, kind, name)if not row:return {"error": "not found"}- # 图集、总数和分页数的是同一批图,`total` 才对得上翻到底的张数。+ # 图集和分页数的是同一批图,`total` 由图集相加,才对得上翻到底的张数。scoped = ("FROM asset_entity ae CROSS JOIN asset a ON a.id=ae.asset_id ""WHERE " + scope_predicate(kind, "ae.entity_id") +" AND a.medium='image' AND a.name IS NOT NULL "@@ -412,11 +412,15 @@ def q_entity_photos(contract: WebContract, args):"location": item["location"],"cost": COST.get(item["location"], "metered"),} for item in c.execute(+ # 一张图在范围里可能挂好几行(两位成员、片商连同旗下 label、同一位的不同 role 或+ # source)。先只按 id 去重,再回 `asset` 取路径分组,`n` 和 `bytes` 数的才是图;+ # 每张图恰好落在一个图集里,各组 `n` 相加就是 `total`。f"SELECT {PHOTO_DIR} dir,min(a.id) id,count(*) n,sum(a.size) bytes,a.location "- + scoped + f"GROUP BY {PHOTO_DIR},a.location ORDER BY n DESC,dir",+ "FROM (SELECT DISTINCT a.id " + scoped + ") u CROSS JOIN asset a ON a.id=u.id "+ f"GROUP BY {PHOTO_DIR},a.location ORDER BY n DESC,dir",(row["id"],),)]- total = c.execute("SELECT count(DISTINCT a.id) " + scoped, (row["id"],)).fetchone()[0]+ total = sum(item["n"] for item in sets)items = [{"id": item["id"], "name": item["name"], "size": item["size"] or 0,"location": item["location"]}for item in c.execute(diff --git a/tests/test_agency_entity.py b/tests/test_agency_entity.pyindex 17ecff950..77f3a780f 100644--- a/tests/test_agency_entity.py+++ b/tests/test_agency_entity.py@@ -199,8 +199,15 @@ class AgencyLedgerTests(unittest.TestCase):def page(self, kind, name):return rm_web.q_entity(self.contract, {"kind": kind, "name": name})- def photos(self, name):- return rm_web.q_entity_photos(self.contract, {"kind": "agency", "name": name})+ def photos(self, name, kind="agency"):+ return rm_web.q_entity_photos(self.contract, {"kind": kind, "name": name})++ def counted_once(self, photos):+ """总数、分页和图集数的是同一张图:各一次,图集字节数也只算一份。"""+ self.assertEqual(+ (photos["total"], [item["id"] for item in photos["items"]],+ [(item["kind"], item["n"], item["bytes"]) for item in photos["sets"]]),+ (1, [21], [("dir", 1, 10)]))def add_pictures(self, rows):"""(id, 路径, 挂在哪几位名下, disposal)。"""@@ -269,8 +276,15 @@ class AgencyLedgerTests(unittest.TestCase):def test_a_picture_of_two_members_is_counted_and_listed_once(self):self.add_pictures([(21, r"R:\Media\duo\01.jpg", [11, 12], None)])- photos = self.photos("Capsule Agency")- self.assertEqual((photos["total"], [item["id"] for item in photos["items"]]), (1, [21]))+ self.counted_once(self.photos("Capsule Agency"))++ def test_a_picture_filed_twice_under_one_entity_is_counted_once(self):+ """主键含 role 与 source:同一位名下,复核再写一行也还是同一张图。"""+ self.add_pictures([(21, r"R:\Media\solo\01.jpg", [11], None)])+ self.con.execute("INSERT INTO asset_entity(asset_id,entity_id,role,source,confidence)"+ " VALUES(21,11,'performer','review',1.0)")+ self.con.commit()+ self.counted_once(self.photos("七泽美亚", "performer"))def test_a_performer_profile_points_at_her_agency_entity(self):home = self.page("performer", "七泽美亚")["agency"]