fix localstorage and profile settings

This commit is contained in:
Mateusz Gruszczyński
2026-06-16 13:47:39 +02:00
parent 3533b694f7
commit fc7ca12a01
9 changed files with 103 additions and 11 deletions
@@ -0,0 +1,48 @@
from __future__ import annotations
from ..db import connect, utcnow
def normalize_limit(value: object) -> int:
try:
limit = int(float(value or 0))
except (TypeError, ValueError):
return 0
return max(0, limit)
def get_limits(profile_id: int | None) -> dict:
profile_id = int(profile_id or 0)
if not profile_id:
return {"down": 0, "up": 0, "configured": False}
with connect() as conn:
row = conn.execute("SELECT down_limit, up_limit FROM profile_speed_limits WHERE profile_id=?", (profile_id,)).fetchone()
if not row:
return {"down": 0, "up": 0, "configured": False}
return {"down": int(row.get("down_limit") or 0), "up": int(row.get("up_limit") or 0), "configured": True}
def save_limits(profile_id: int, down: object, up: object) -> dict:
profile_id = int(profile_id or 0)
if not profile_id:
raise ValueError("Missing profile id")
clean = {"down": normalize_limit(down), "up": normalize_limit(up), "configured": True}
now = utcnow()
with connect() as conn:
conn.execute(
"""
INSERT INTO profile_speed_limits(profile_id, down_limit, up_limit, created_at, updated_at)
VALUES(?,?,?,?,?)
ON CONFLICT(profile_id) DO UPDATE SET
down_limit=excluded.down_limit,
up_limit=excluded.up_limit,
updated_at=excluded.updated_at
""",
(profile_id, clean["down"], clean["up"], now, now),
)
return clean
def delete_limits(profile_id: int) -> None:
with connect() as conn:
conn.execute("DELETE FROM profile_speed_limits WHERE profile_id=?", (int(profile_id or 0),))
+21 -5
View File
@@ -9,7 +9,7 @@ from .preferences import active_profile, get_profile
from ..db import default_user_id
from .torrent_cache import torrent_cache
from .torrent_summary import cached_summary
from . import rtorrent, smart_queue, traffic_history, automation_rules, torrent_stats, auth, speed_peaks, poller_control, download_planner
from . import rtorrent, smart_queue, traffic_history, automation_rules, torrent_stats, auth, speed_peaks, poller_control, download_planner, profile_speed_limits
def _profile_room(profile_id: int) -> str:
@@ -37,6 +37,14 @@ def _emit_profile(socketio, event: str, payload: dict, profile_id: int) -> None:
def _apply_configured_speed_limits(profile: dict) -> None:
limits = profile_speed_limits.get_limits(int(profile.get("id") or 0))
if not limits.get("configured"):
return
# Note: Profile-level speed limits are re-applied when the profile is opened so they are not tied to a specific user session.
rtorrent.set_limits(profile, limits.get("down"), limits.get("up"))
def _run_slow_profile_tasks(socketio, profile: dict, profile_id: int) -> None:
state = poller_control.state_for(profile_id)
# Note: Background checks keep the profile owner so bypass/admin profiles do not enqueue jobs as the fallback user.
@@ -282,10 +290,14 @@ def register_socketio_handlers(socketio):
if not profile:
emit("profile_required", {"ok": True, "profiles": []})
return
try:
_apply_configured_speed_limits(profile)
except Exception as exc:
emit("rtorrent_error", {"profile_id": profile["id"], "error": str(exc)})
rows = torrent_cache.snapshot(profile["id"])
emit("torrent_snapshot", {"profile_id": profile["id"], "torrents": rows, "summary": cached_summary(profile["id"], rows), "speed_status": _speed_status_from_rows(profile["id"], rows)})
emit("poller_settings", {"settings": poller_control.get_settings(int(profile["id"])), "runtime": poller_control.snapshot(int(profile["id"]))})
emit("download_plan_update", {"settings": download_planner.get_settings(int(profile["id"]))})
emit("poller_settings", {"profile_id": int(profile["id"]), "settings": poller_control.get_settings(int(profile["id"])), "runtime": poller_control.snapshot(int(profile["id"]))})
emit("download_plan_update", {"profile_id": int(profile["id"]), "settings": download_planner.get_settings(int(profile["id"]))})
@socketio.on("select_profile")
def handle_select_profile(data):
@@ -304,8 +316,12 @@ def register_socketio_handlers(socketio):
emit("rtorrent_error", {"error": "Profile access denied or profile does not exist"})
return
join_room(_profile_room(profile_id))
try:
_apply_configured_speed_limits(profile)
except Exception as exc:
emit("rtorrent_error", {"profile_id": profile_id, "error": str(exc)})
diff = torrent_cache.refresh(profile)
rows = torrent_cache.snapshot(profile_id)
emit("torrent_snapshot", {"profile_id": profile_id, "torrents": rows, "summary": cached_summary(profile_id, rows, force=True), "speed_status": _speed_status_from_rows(profile_id, rows), "error": diff.get("error", "")})
emit("poller_settings", {"settings": poller_control.get_settings(profile_id), "runtime": poller_control.snapshot(profile_id)})
emit("download_plan_update", {"settings": download_planner.get_settings(profile_id)})
emit("poller_settings", {"profile_id": profile_id, "settings": poller_control.get_settings(profile_id), "runtime": poller_control.snapshot(profile_id)})
emit("download_plan_update", {"profile_id": profile_id, "settings": download_planner.get_settings(profile_id)})