mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 10:08:40 +01:00
fix deprecations in pydantic
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from pydantic import model_validator
|
||||
from pydantic import ConfigDict, model_validator
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.exc import InvalidRequestError
|
||||
|
||||
@@ -10,6 +10,8 @@ from .device import DeviceSyncSchema
|
||||
|
||||
|
||||
class BaseAsset(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
rom_id: int
|
||||
user_id: int
|
||||
@@ -27,9 +29,6 @@ class BaseAsset(BaseModel):
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class ScreenshotSchema(BaseAsset):
|
||||
pass
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import ConfigDict
|
||||
|
||||
from .base import BaseModel
|
||||
|
||||
|
||||
class ClientTokenSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
name: str
|
||||
scopes: list[str]
|
||||
@@ -12,9 +16,6 @@ class ClientTokenSchema(BaseModel):
|
||||
created_at: datetime
|
||||
user_id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class ClientTokenCreateSchema(ClientTokenSchema):
|
||||
raw_token: str
|
||||
|
||||
@@ -2,6 +2,8 @@ from collections.abc import Sequence
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from pydantic import ConfigDict
|
||||
|
||||
from models.collection import Collection, SmartCollection
|
||||
|
||||
from .base import BaseModel
|
||||
@@ -25,14 +27,13 @@ class BaseCollectionSchema(BaseModel):
|
||||
|
||||
|
||||
class CollectionSchema(BaseCollectionSchema):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
url_cover: str | None
|
||||
user_id: int
|
||||
owner_username: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@classmethod
|
||||
def for_user(
|
||||
cls, user_id: int, collections: Sequence["Collection"]
|
||||
@@ -45,15 +46,16 @@ class CollectionSchema(BaseCollectionSchema):
|
||||
|
||||
|
||||
class VirtualCollectionSchema(BaseCollectionSchema):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: str
|
||||
type: str
|
||||
is_virtual: bool = True
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class SmartCollectionSchema(BaseCollectionSchema):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
name: str
|
||||
description: str = ""
|
||||
@@ -63,9 +65,6 @@ class SmartCollectionSchema(BaseCollectionSchema):
|
||||
owner_username: str
|
||||
is_smart: bool = True
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@classmethod
|
||||
def for_user(
|
||||
cls, user_id: int, smart_collections: Sequence["SmartCollection"]
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import ConfigDict
|
||||
|
||||
from models.device import SyncMode
|
||||
|
||||
from .base import BaseModel
|
||||
|
||||
|
||||
class DeviceSyncSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
device_id: str
|
||||
device_name: str | None
|
||||
last_synced_at: datetime
|
||||
is_untracked: bool
|
||||
is_current: bool
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class DeviceSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: str
|
||||
user_id: int
|
||||
name: str | None
|
||||
@@ -33,9 +36,6 @@ class DeviceSchema(BaseModel):
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class DeviceCreateResponse(BaseModel):
|
||||
device_id: str
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
from datetime import datetime
|
||||
from typing import TypedDict
|
||||
|
||||
from pydantic import ConfigDict
|
||||
|
||||
from .base import BaseModel
|
||||
|
||||
|
||||
class FirmwareSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
|
||||
file_name: str
|
||||
@@ -25,9 +29,6 @@ class FirmwareSchema(BaseModel):
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class AddFirmwareResponse(TypedDict):
|
||||
uploaded: int
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from datetime import datetime
|
||||
from typing import NotRequired, TypedDict, get_type_hints
|
||||
|
||||
from pydantic import ConfigDict
|
||||
|
||||
from handler.metadata.ra_handler import RAUserProgression
|
||||
from models.user import Role
|
||||
|
||||
@@ -14,6 +16,8 @@ RAProgression = TypedDict( # type: ignore[misc]
|
||||
|
||||
|
||||
class UserSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
username: str
|
||||
email: str | None
|
||||
@@ -30,9 +34,6 @@ class UserSchema(BaseModel):
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class InviteLinkSchema(BaseModel):
|
||||
token: str
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import Field, computed_field, field_validator
|
||||
from pydantic import ConfigDict, Field, computed_field, field_validator
|
||||
|
||||
from models.platform import DEFAULT_COVER_ASPECT_RATIO
|
||||
|
||||
@@ -9,6 +9,8 @@ from .firmware import FirmwareSchema
|
||||
|
||||
|
||||
class PlatformSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
slug: str
|
||||
fs_slug: str
|
||||
@@ -42,9 +44,6 @@ class PlatformSchema(BaseModel):
|
||||
is_identified: bool
|
||||
missing_from_fs: bool
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@computed_field # type: ignore
|
||||
@property
|
||||
def display_name(self) -> str:
|
||||
|
||||
@@ -5,7 +5,7 @@ from datetime import datetime, timezone
|
||||
from typing import NotRequired, TypedDict, get_type_hints
|
||||
|
||||
from fastapi import Request
|
||||
from pydantic import computed_field, field_validator
|
||||
from pydantic import ConfigDict, computed_field, field_validator
|
||||
|
||||
from endpoints.responses.assets import SaveSchema, ScreenshotSchema, StateSchema
|
||||
from handler.metadata.flashpoint_handler import FlashpointMetadata
|
||||
@@ -26,6 +26,8 @@ SORT_COMPARE_REGEX = re.compile(r"^([Tt]he|[Aa]|[Aa]nd)\s")
|
||||
|
||||
|
||||
class UserNoteSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
title: str
|
||||
content: str
|
||||
@@ -36,9 +38,6 @@ class UserNoteSchema(BaseModel):
|
||||
user_id: int
|
||||
username: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
RomIGDBMetadata = TypedDict( # type: ignore[misc]
|
||||
"RomIGDBMetadata",
|
||||
@@ -121,6 +120,8 @@ def rom_user_schema_factory() -> RomUserSchema:
|
||||
|
||||
|
||||
class RomUserSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
user_id: int
|
||||
rom_id: int
|
||||
@@ -136,9 +137,6 @@ class RomUserSchema(BaseModel):
|
||||
completion: int
|
||||
status: RomUserStatus | None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@classmethod
|
||||
def for_user(cls, user_id: int, db_rom: Rom) -> RomUserSchema:
|
||||
for n in db_rom.rom_users:
|
||||
@@ -150,6 +148,8 @@ class RomUserSchema(BaseModel):
|
||||
|
||||
|
||||
class RomFileSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
rom_id: int
|
||||
file_name: str
|
||||
@@ -165,11 +165,10 @@ class RomFileSchema(BaseModel):
|
||||
ra_hash: str | None
|
||||
category: RomFileCategory | None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class RomMetadataSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
rom_id: int
|
||||
genres: list[str]
|
||||
franchises: list[str]
|
||||
@@ -181,9 +180,6 @@ class RomMetadataSchema(BaseModel):
|
||||
first_release_date: int | None
|
||||
average_rating: float | None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@field_validator("genres")
|
||||
def sort_genres(cls, v: list[str]) -> list[str]:
|
||||
return sorted(v)
|
||||
@@ -210,6 +206,8 @@ class RomMetadataSchema(BaseModel):
|
||||
|
||||
|
||||
class RomSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
igdb_id: int | None
|
||||
sgdb_id: int | None
|
||||
@@ -293,9 +291,6 @@ class RomSchema(BaseModel):
|
||||
merged_screenshots: list[str]
|
||||
merged_ra_metadata: RomRAMetadata | None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@classmethod
|
||||
def populate_properties(cls, db_rom: Rom, request: Request) -> Rom:
|
||||
db_rom.rom_user = RomUserSchema.for_user(request.user.id, db_rom) # type: ignore
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from datetime import datetime
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import ConfigDict
|
||||
|
||||
from .base import BaseModel
|
||||
|
||||
|
||||
@@ -26,6 +28,8 @@ class SyncNegotiateResponse(BaseModel):
|
||||
|
||||
|
||||
class SyncSessionSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
device_id: str
|
||||
user_id: int
|
||||
@@ -38,6 +42,3 @@ class SyncSessionSchema(BaseModel):
|
||||
error_message: str | None = None
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@@ -187,7 +187,7 @@ async def upload_chunk(
|
||||
|
||||
if expected_chunk_size > ROM_UPLOAD_MAX_CHUNK_SIZE:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
|
||||
status_code=status.HTTP_413_CONTENT_TOO_LARGE,
|
||||
detail="Chunk size exceeds server maximum",
|
||||
)
|
||||
|
||||
@@ -202,7 +202,7 @@ async def upload_chunk(
|
||||
|
||||
if content_length_bytes > ROM_UPLOAD_MAX_CHUNK_SIZE:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
|
||||
status_code=status.HTTP_413_CONTENT_TOO_LARGE,
|
||||
detail="Chunk exceeds maximum allowed size",
|
||||
)
|
||||
|
||||
@@ -215,7 +215,7 @@ async def upload_chunk(
|
||||
chunk_bytes_written += len(body_chunk)
|
||||
if chunk_bytes_written > ROM_UPLOAD_MAX_CHUNK_SIZE:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
|
||||
status_code=status.HTTP_413_CONTENT_TOO_LARGE,
|
||||
detail="Chunk exceeds maximum allowed size",
|
||||
)
|
||||
await f.write(body_chunk)
|
||||
|
||||
@@ -32,7 +32,7 @@ class LocalSource:
|
||||
try:
|
||||
indexed_val: dict[str, dict[str, str]] = {}
|
||||
root = ET.parse(str(xml_path.resolve())).getroot()
|
||||
if root:
|
||||
if root is not None:
|
||||
for game_elem in root.findall(".//Game"):
|
||||
entry: dict[str, str] = {}
|
||||
for child_elem in game_elem:
|
||||
|
||||
@@ -185,7 +185,7 @@ def test_upload_chunk_oversized_returns_413(
|
||||
content=b"1234567",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_413_REQUEST_ENTITY_TOO_LARGE
|
||||
assert response.status_code == status.HTTP_413_CONTENT_TOO_LARGE
|
||||
assert response.json()["detail"] == "Chunk exceeds maximum allowed size"
|
||||
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ class TestClientTokenCRUD:
|
||||
},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
|
||||
|
||||
|
||||
class TestClientTokenAuth:
|
||||
|
||||
@@ -172,8 +172,11 @@ async def test_password_change_invalidates_sessions(client, admin_user: User):
|
||||
old_session_cookie = response.cookies.get("romm_session")
|
||||
assert old_session_cookie is not None
|
||||
|
||||
def _cookie_header(cookie_value: str) -> dict[str, str]:
|
||||
return {"Cookie": f"romm_session={cookie_value}"}
|
||||
|
||||
# Verify session works
|
||||
response = client.get("/api/users/me", cookies={"romm_session": old_session_cookie})
|
||||
response = client.get("/api/users/me", headers=_cookie_header(old_session_cookie))
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
|
||||
# Update the user's password
|
||||
@@ -185,7 +188,7 @@ async def test_password_change_invalidates_sessions(client, admin_user: User):
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
|
||||
# Attempt to access a protected resource using the old session cookie
|
||||
response = client.get("/api/users/me", cookies={"romm_session": old_session_cookie})
|
||||
response = client.get("/api/users/me", headers=_cookie_header(old_session_cookie))
|
||||
assert response.status_code in [HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN]
|
||||
|
||||
# Login with the new credentials
|
||||
@@ -201,7 +204,7 @@ async def test_password_change_invalidates_sessions(client, admin_user: User):
|
||||
assert new_session_cookie != old_session_cookie
|
||||
|
||||
# Attempt to access a protected resource using the new session cookie
|
||||
response = client.get("/api/users/me", cookies={"romm_session": new_session_cookie})
|
||||
response = client.get("/api/users/me", headers=_cookie_header(new_session_cookie))
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
|
||||
await RedisSessionMiddleware.clear_user_sessions(admin_user.username)
|
||||
@@ -220,16 +223,19 @@ async def test_logout_invalidates_session(client, admin_user: User):
|
||||
session_cookie = response.cookies.get("romm_session")
|
||||
assert session_cookie is not None
|
||||
|
||||
def _cookie_header(cookie_value: str) -> dict[str, str]:
|
||||
return {"Cookie": f"romm_session={cookie_value}"}
|
||||
|
||||
# Verify session works
|
||||
response = client.get("/api/users/me", cookies={"romm_session": session_cookie})
|
||||
response = client.get("/api/users/me", headers=_cookie_header(session_cookie))
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
|
||||
# Log out the user
|
||||
response = client.post("/api/logout", cookies={"romm_session": session_cookie})
|
||||
response = client.post("/api/logout", headers=_cookie_header(session_cookie))
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
|
||||
# Attempt to access a protected resource using the old session cookie
|
||||
response = client.get("/api/users/me", cookies={"romm_session": session_cookie})
|
||||
response = client.get("/api/users/me", headers=_cookie_header(session_cookie))
|
||||
assert response.status_code in [HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN]
|
||||
|
||||
await RedisSessionMiddleware.clear_user_sessions(admin_user.username)
|
||||
@@ -277,7 +283,7 @@ async def test_logout_with_oidc_rp_initiated_logout(client, admin_user: User):
|
||||
):
|
||||
response = client.post(
|
||||
"/api/logout",
|
||||
cookies={"romm_session": session_cookie},
|
||||
headers={"Cookie": f"romm_session={session_cookie}"},
|
||||
)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
data = response.json()
|
||||
|
||||
@@ -1534,7 +1534,7 @@ class TestSavesSummaryEndpoint:
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
|
||||
data = response.json()
|
||||
assert "detail" in data
|
||||
assert any("rom_id" in str(err).lower() for err in data["detail"])
|
||||
|
||||
@@ -167,7 +167,8 @@ class TestSessionMiddleware:
|
||||
key = OctKey.import_key("test-secret-key")
|
||||
token = jwt.encode({"alg": "HS256"}, payload, key=key)
|
||||
|
||||
response = client.get("/get", cookies={"session": token})
|
||||
client.cookies.set("session", token)
|
||||
response = client.get("/get")
|
||||
assert response.status_code == 200
|
||||
# middleware must reject the expired token → empty session
|
||||
assert response.json()["session"] == {}
|
||||
@@ -183,7 +184,8 @@ class TestSessionMiddleware:
|
||||
key = OctKey.import_key("test-secret-key")
|
||||
token = jwt.encode({"alg": "HS256"}, payload, key=key)
|
||||
|
||||
response = client.get("/get", cookies={"session": token})
|
||||
client.cookies.set("session", token)
|
||||
response = client.get("/get")
|
||||
assert response.status_code == 200
|
||||
# middleware must reject the future token → empty session
|
||||
assert response.json()["session"] == {}
|
||||
@@ -199,7 +201,8 @@ class TestSessionMiddleware:
|
||||
|
||||
# Tamper with the cookie (simulate bad signature)
|
||||
tampered_cookie = response.cookies["session"][:-10] + "tampereddata"
|
||||
response = client.get("/get", cookies={"session": tampered_cookie})
|
||||
client.cookies.set("session", tampered_cookie)
|
||||
response = client.get("/get")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ def dummy_task():
|
||||
pass
|
||||
|
||||
|
||||
class TestTask(PeriodicTask):
|
||||
class DummyTask(PeriodicTask):
|
||||
async def run(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
@@ -23,7 +23,7 @@ class TestPreventRequeue(unittest.TestCase):
|
||||
):
|
||||
for method_name in ["init", "schedule"]:
|
||||
with self.subTest(method=method_name):
|
||||
task = TestTask(
|
||||
task = DummyTask(
|
||||
title="Test Task",
|
||||
description="A test task",
|
||||
task_type=TaskType.GENERIC,
|
||||
|
||||
@@ -39,11 +39,13 @@ class TestScanLibraryTask:
|
||||
mocker.patch.object(HLTBHandler, "is_enabled", return_value=False)
|
||||
mocker.patch.object(TGDBHandler, "is_enabled", return_value=False)
|
||||
mocker.patch("tasks.scheduled.scan_library.ENABLE_SCHEDULED_RESCAN", True)
|
||||
|
||||
scan_result = MagicMock()
|
||||
mock_scan_platforms = mocker.patch(
|
||||
"tasks.scheduled.scan_library.scan_platforms"
|
||||
"tasks.scheduled.scan_library.scan_platforms",
|
||||
side_effect=AsyncMock(return_value=scan_result),
|
||||
)
|
||||
mock_log = mocker.patch("tasks.scheduled.scan_library.log")
|
||||
mock_scan_platforms.return_value = AsyncMock()
|
||||
|
||||
await task.run()
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ def parse_expiry(expires_in: str | None) -> datetime | None:
|
||||
delta = EXPIRY_MAP.get(expires_in)
|
||||
if delta is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
|
||||
detail=f"Invalid expires_in value: {expires_in}. "
|
||||
f"Valid values: {', '.join(EXPIRY_MAP.keys())}, never",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user