فهرست منبع

refactor: improve error message formatting and logging in user restrictions service

Manuel 7 ماه پیش
والد
کامیت
75f5bdab07
2فایلهای تغییر یافته به همراه13 افزوده شده و 7 حذف شده
  1. 3 1
      routes/api_routes.py
  2. 10 6
      services/squid/user_restrictions_service.py

+ 3 - 1
routes/api_routes.py

@@ -291,7 +291,9 @@ def api_throttle_user():
     except (TypeError, ValueError):
         return json_error("El campo 'pool_number' debe ser un entero", 400)
     if not username or not ip:
-        return json_error("Se requieren los campos 'username', 'ip' y 'pool_number'", 400)
+        return json_error(
+            "Se requieren los campos 'username', 'ip' y 'pool_number'", 400
+        )
 
     db = get_session()
     try:

+ 10 - 6
services/squid/user_restrictions_service.py

@@ -225,10 +225,9 @@ def _find_delay_insert_index(lines: list[str], pool_number: int, deny_all: str)
         stripped = line.strip()
         if stripped == deny_all:
             return i
-        if (
-            stripped.startswith(f"delay_parameters {pool_number} ")
-            and after_params == len(lines)
-        ):
+        if stripped.startswith(
+            f"delay_parameters {pool_number} "
+        ) and after_params == len(lines):
             after_params = i + 1
     return after_params
 
@@ -293,7 +292,9 @@ def block_user(username: str, ip: str, db, cm) -> tuple[bool, str]:
     if not _add_acl_src_file(BLOCKED_ACL_NAME, filepath, cm):
         logger.warning("Could not add squidstats_blocked ACL; DB entry saved")
     if not add_http_deny_blocklist(BLOCKED_ACL_NAME, cm):
-        logger.warning("Could not add http_access deny squidstats_blocked; DB entry saved")
+        logger.warning(
+            "Could not add http_access deny squidstats_blocked; DB entry saved"
+        )
 
     return True, f"Usuario {username} ({ip}) bloqueado"
 
@@ -339,7 +340,10 @@ def throttle_user(username: str, ip: str, pool_number: int, db, cm) -> tuple[boo
 
     existing = db.query(ThrottledUser).filter_by(ip=ip, active=1).first()
     if existing:
-        return False, f"La IP {ip} ya tiene velocidad reducida (pool #{existing.pool_number})"
+        return (
+            False,
+            f"La IP {ip} ya tiene velocidad reducida (pool #{existing.pool_number})",
+        )
 
     record = ThrottledUser(username=username, ip=ip, pool_number=pool_number, active=1)
     try: