profile id support in api requests

This commit is contained in:
Mateusz Gruszczyński
2026-06-16 19:46:16 +02:00
parent c796a740d1
commit a73aeb5544
18 changed files with 1823 additions and 185 deletions
+37 -4
View File
@@ -728,12 +728,45 @@ def install_guards(app) -> None:
def _request_profile_id() -> int | None:
if request.view_args and request.view_args.get("profile_id"):
return int(request.view_args["profile_id"])
payload = {}
try:
payload = request.get_json(silent=True) or {}
if payload.get("profile_id"):
return int(payload.get("profile_id"))
except Exception:
pass
payload = {}
raw_id = (
request.args.get("profile_id")
or request.form.get("profile_id")
or payload.get("profile_id")
or request.headers.get("X-PyTorrent-Profile-Id")
)
if raw_id not in (None, ""):
try:
return int(raw_id)
except (TypeError, ValueError):
return None
raw_name = (
request.args.get("profile_name")
or request.form.get("profile_name")
or payload.get("profile_name")
or request.headers.get("X-PyTorrent-Profile-Name")
)
if raw_name:
from . import preferences
visible = visible_profile_ids(current_user_id())
with connect() as conn:
if visible is None:
row = conn.execute("SELECT id FROM rtorrent_profiles WHERE lower(name)=lower(?) ORDER BY is_default DESC, id LIMIT 1", (str(raw_name).strip(),)).fetchone()
elif visible:
placeholders = ",".join("?" for _ in visible)
row = conn.execute(
f"SELECT id FROM rtorrent_profiles WHERE id IN ({placeholders}) AND lower(name)=lower(?) ORDER BY is_default DESC, id LIMIT 1",
(*tuple(visible), str(raw_name).strip()),
).fetchone()
else:
row = None
return int(row["id"]) if row else None
from . import preferences
profile = preferences.active_profile()
return int(profile["id"]) if profile else None
if profile:
return int(profile["id"])
return 1 if can_access_profile(1) else None
+2 -2
View File
@@ -175,8 +175,8 @@ def create_app_backup(name: str, user_id: int | None = None, automatic: bool = F
def create_profile_backup(name: str, profile_id: int, user_id: int | None = None, automatic: bool = False) -> dict:
user_id = user_id or auth.current_user_id() or default_user_id()
if not auth.can_access_profile(profile_id, user_id):
raise PermissionError("No access to profile")
if not auth.can_write_profile(profile_id, user_id):
raise PermissionError("No write access to profile")
payload = {"version": 2, "backup_type": "profile", "source_profile_id": int(profile_id), "created_at": utcnow(), "automatic": bool(automatic), "tables": {}}
with connect() as conn:
for table in PROFILE_BACKUP_TABLES:
+2 -2
View File
@@ -491,9 +491,9 @@ def get_preferences(user_id: int | None = None, profile_id: int | None = None):
merged.update(get_disk_monitor_preferences(profile_id, user_id))
return merged
def save_preferences(data: dict, user_id: int | None = None):
def save_preferences(data: dict, user_id: int | None = None, profile_id: int | None = None):
user_id = user_id or auth.current_user_id() or default_user_id()
profile_id = _active_profile_id_for_user(user_id)
profile_id = profile_id or _active_profile_id_for_user(user_id)
allowed_theme = data.get("theme") if data.get("theme") in {"light", "dark"} else None
bootstrap_theme = data.get("bootstrap_theme") if data.get("bootstrap_theme") in BOOTSTRAP_THEMES else None
font_family = data.get("font_family") if data.get("font_family") in FONT_FAMILIES else None