"""Persistent selection preferences scoped to a library and its stable item ID.""" def flags(db, source_id, item_id): row = db.one( "SELECT starred,excluded FROM photo_preferences WHERE source_id=? AND item_id=?", (source_id, item_id), ) return {key: bool(row and row[key]) for key in ("starred", "excluded")} def set_flags(db, source_id, item_id, *, starred=None, excluded=None): with db.transaction() as connection: connection.execute( "INSERT INTO photo_preferences(source_id,item_id,starred,excluded) VALUES(?,?,?,?) ON CONFLICT(source_id,item_id) DO UPDATE SET starred=coalesce(?,starred),excluded=coalesce(?,excluded)", ( source_id, item_id, int(starred or False), int(excluded or False), None if starred is None else int(starred), None if excluded is None else int(excluded), ), ) return flags(db, source_id, item_id) def annotate(db, source_id, items): preferences = { row["item_id"]: row for row in db.all( "SELECT item_id,starred,excluded FROM photo_preferences WHERE source_id=?", (source_id,), ) } return [ { **item, **{ key: bool(preferences.get(item["id"], {}).get(key)) for key in ("starred", "excluded") }, } for item in items ] def eligible(db, source_id, items): excluded = { row["item_id"] for row in db.all( "SELECT item_id FROM photo_preferences WHERE source_id=? AND excluded=1", (source_id,), ) } return [item for item in items if item["id"] not in excluded]