diff --git a/backend/endpoints/responses/rom.py b/backend/endpoints/responses/rom.py index dfed06107..5eece3884 100644 --- a/backend/endpoints/responses/rom.py +++ b/backend/endpoints/responses/rom.py @@ -183,15 +183,13 @@ class SimpleRomSchema(RomSchema): @classmethod def from_orm_with_request(cls, db_rom: Rom, request: Request) -> SimpleRomSchema: user_id = request.user.id - rom = cls.model_validate(db_rom) - rom.rom_user = RomUserSchema.for_user(user_id, db_rom) - return rom + db_rom.rom_user = RomUserSchema.for_user(user_id, db_rom) # type: ignore + return cls.model_validate(db_rom) @classmethod def from_orm_with_factory(cls, db_rom: Rom) -> SimpleRomSchema: - rom = cls.model_validate(db_rom) - rom.rom_user = rom_user_schema_factory() - return rom + db_rom.rom_user = rom_user_schema_factory() # type: ignore + return cls.model_validate(db_rom) class DetailedRomSchema(RomSchema): @@ -208,26 +206,24 @@ class DetailedRomSchema(RomSchema): def from_orm_with_request(cls, db_rom: Rom, request: Request) -> DetailedRomSchema: user_id = request.user.id - rom = cls.model_validate(db_rom) - - rom.rom_user = RomUserSchema.for_user(user_id, db_rom) - rom.user_notes = RomUserSchema.notes_for_user(user_id, db_rom) - rom.user_saves = [ + db_rom.rom_user = RomUserSchema.for_user(user_id, db_rom) # type: ignore + db_rom.user_notes = RomUserSchema.notes_for_user(user_id, db_rom) # type: ignore + db_rom.user_saves = [ # type: ignore SaveSchema.model_validate(s) for s in db_rom.saves if s.user_id == user_id ] - rom.user_states = [ + db_rom.user_states = [ # type: ignore StateSchema.model_validate(s) for s in db_rom.states if s.user_id == user_id ] - rom.user_screenshots = [ + db_rom.user_screenshots = [ # type: ignore ScreenshotSchema.model_validate(s) for s in db_rom.screenshots if s.user_id == user_id ] - rom.user_collections = CollectionSchema.for_user( + db_rom.user_collections = CollectionSchema.for_user( # type: ignore user_id, [c for c in db_rom.get_collections()] ) - return rom + return cls.model_validate(db_rom) class UserNotesSchema(TypedDict): diff --git a/backend/endpoints/sockets/scan.py b/backend/endpoints/sockets/scan.py index fa2619769..27feb64cd 100644 --- a/backend/endpoints/sockets/scan.py +++ b/backend/endpoints/sockets/scan.py @@ -428,8 +428,8 @@ async def _identify_rom( RomFile( id=db_rom_file.id if db_rom_file else None, rom_id=_added_rom.id, - fs_name=file.file_name, - fs_path=file.file_path, + file_name=file.file_name, + file_path=file.file_path, file_size_bytes=file.file_size_bytes, last_modified=file.last_modified, ) @@ -503,22 +503,22 @@ async def scan_handler(_sid: str, options: dict): metadata_sources = options.get("apis", []) # Uncomment this to run scan in the current process - # await scan_platforms( - # platform_ids=platform_ids, - # scan_type=scan_type, - # roms_ids=roms_ids, - # metadata_sources=metadata_sources, - # ) - - return high_prio_queue.enqueue( - scan_platforms, - platform_ids, - scan_type, - roms_ids, - metadata_sources, - job_timeout=SCAN_TIMEOUT, # Timeout (default of 4 hours) + await scan_platforms( + platform_ids=platform_ids, + scan_type=scan_type, + roms_ids=roms_ids, + metadata_sources=metadata_sources, ) + # return high_prio_queue.enqueue( + # scan_platforms, + # platform_ids, + # scan_type, + # roms_ids, + # metadata_sources, + # job_timeout=SCAN_TIMEOUT, # Timeout (default of 4 hours) + # ) + @socket_handler.socket_server.on("scan:stop") async def stop_scan_handler(_sid: str): diff --git a/backend/handler/database/collections_handler.py b/backend/handler/database/collections_handler.py index 9e60b7549..aa50baa99 100644 --- a/backend/handler/database/collections_handler.py +++ b/backend/handler/database/collections_handler.py @@ -16,13 +16,7 @@ class DBCollectionsHandler(DBBaseHandler): collection = session.merge(collection) session.flush() - new_collection = session.scalar( - select(Collection).filter_by(id=collection.id).limit(1) - ) - if not new_collection: - raise ValueError("Could not find newly created collection") - - return new_collection + return session.query(Collection).filter_by(id=collection.id).one() @begin_session def get_collection(self, id: int, session: Session = None) -> Collection | None: diff --git a/backend/handler/database/firmware_handler.py b/backend/handler/database/firmware_handler.py index f99ebc36f..059c997bd 100644 --- a/backend/handler/database/firmware_handler.py +++ b/backend/handler/database/firmware_handler.py @@ -47,12 +47,13 @@ class DBFirmwareHandler(DBBaseHandler): @begin_session def update_firmware(self, id: int, data: dict, session: Session = None) -> Firmware: - return session.scalar( + session.execute( update(Firmware) .where(Firmware.id == id) .values(**data) .execution_options(synchronize_session="evaluate") ) + return session.query(Firmware).filter_by(id=id).one() @begin_session def delete_firmware(self, id: int, session: Session = None) -> None: diff --git a/backend/handler/database/platforms_handler.py b/backend/handler/database/platforms_handler.py index e17c5de1f..54c65a40f 100644 --- a/backend/handler/database/platforms_handler.py +++ b/backend/handler/database/platforms_handler.py @@ -19,13 +19,7 @@ class DBPlatformsHandler(DBBaseHandler): platform = session.merge(platform) session.flush() - new_platform = session.scalar( - select(Platform).filter_by(id=platform.id).limit(1) - ) - if not new_platform: - raise ValueError("Could not find newly created platform") - - return new_platform + return session.query(Platform).filter_by(id=platform.id).one() @begin_session def get_platform(self, id: int, *, session: Session = None) -> Platform | None: diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index 2f7e8b8a2..aa67929d3 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -193,12 +193,13 @@ class DBRomsHandler(DBBaseHandler): @begin_session def update_rom(self, id: int, data: dict, session: Session = None) -> Rom: - return session.scalar( + session.execute( update(Rom) .where(Rom.id == id) .values(**data) .execution_options(synchronize_session="evaluate") ) + return session.query(Rom).filter_by(id=id).one() @begin_session def delete_rom(self, id: int, session: Session = None) -> None: @@ -257,15 +258,9 @@ class DBRomsHandler(DBBaseHandler): .execution_options(synchronize_session="evaluate") ) - rom_user = self.get_rom_user_by_id(id) - if not rom_user: - raise ValueError(f"RomUser with id {id} not found") - + rom_user = session.query(RomUser).filter_by(id=id).one() if data.get("is_main_sibling", False): - rom = self.get_rom(rom_user.rom_id) - if not rom: - raise ValueError(f"Rom with id {rom_user.rom_id} not found") - + rom = session.query(Rom).filter_by(id=rom_user.rom_id).one() session.execute( update(RomUser) .where( @@ -277,11 +272,7 @@ class DBRomsHandler(DBBaseHandler): .values(is_main_sibling=False) ) - rom_user = self.get_rom_user_by_id(id) - if not rom_user: - raise ValueError(f"RomUser with id {id} not found") - - return rom_user + return session.query(RomUser).filter_by(id=id).one() @begin_session def add_rom_file(self, rom_file: RomFile, session: Session = None) -> RomFile: @@ -312,8 +303,4 @@ class DBRomsHandler(DBBaseHandler): .execution_options(synchronize_session="evaluate") ) - rom_file = self.get_rom_file_by_id(id) - if not rom_file: - raise ValueError(f"RomFile with id {id} not found") - - return rom_file + return session.query(RomFile).filter_by(id=id).one() diff --git a/backend/handler/database/saves_handler.py b/backend/handler/database/saves_handler.py index 698c0279d..78e6a7deb 100644 --- a/backend/handler/database/saves_handler.py +++ b/backend/handler/database/saves_handler.py @@ -29,12 +29,13 @@ class DBSavesHandler(DBBaseHandler): @begin_session def update_save(self, id: int, data: dict, session: Session = None) -> Save: - return session.scalar( + session.execute( update(Save) .where(Save.id == id) .values(**data) .execution_options(synchronize_session="evaluate") ) + return session.query(Save).filter_by(id=id).one() @begin_session def delete_save(self, id: int, session: Session = None) -> None: diff --git a/backend/handler/database/screenshots_handler.py b/backend/handler/database/screenshots_handler.py index 94d30f43a..5c66b2c04 100644 --- a/backend/handler/database/screenshots_handler.py +++ b/backend/handler/database/screenshots_handler.py @@ -33,12 +33,13 @@ class DBScreenshotsHandler(DBBaseHandler): def update_screenshot( self, id: int, data: dict, session: Session = None ) -> Screenshot: - return session.scalar( + session.execute( update(Screenshot) .where(Screenshot.id == id) .values(**data) .execution_options(synchronize_session="evaluate") ) + return session.query(Screenshot).filter_by(id=id).one() @begin_session def delete_screenshot(self, id: int, session: Session = None) -> None: diff --git a/backend/handler/database/states_handler.py b/backend/handler/database/states_handler.py index 8b3315e8c..ecead1dee 100644 --- a/backend/handler/database/states_handler.py +++ b/backend/handler/database/states_handler.py @@ -29,12 +29,13 @@ class DBStatesHandler(DBBaseHandler): @begin_session def update_state(self, id: int, data: dict, session: Session = None) -> State: - return session.scalar( + session.execute( update(State) .where(State.id == id) .values(**data) .execution_options(synchronize_session="evaluate") ) + return session.query(State).filter_by(id=id).one() @begin_session def delete_state(self, id: int, session: Session = None) -> None: diff --git a/backend/handler/database/users_handler.py b/backend/handler/database/users_handler.py index c17641f06..103318dd3 100644 --- a/backend/handler/database/users_handler.py +++ b/backend/handler/database/users_handler.py @@ -29,12 +29,13 @@ class DBUsersHandler(DBBaseHandler): @begin_session def update_user(self, id: int, data: dict, session: Session = None) -> User: - return session.scalar( + session.execute( update(User) .where(User.id == id) .values(**data) .execution_options(synchronize_session="evaluate") ) + return session.query(User).filter_by(id=id).one() @begin_session def get_users(self, session: Session = None) -> Sequence[User]: diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 425134e16..258ca9716 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -7,9 +7,8 @@ import shutil import tarfile import zipfile from collections.abc import Callable, Iterator -from hashlib import _Hash from pathlib import Path -from typing import Final, Literal, TypedDict +from typing import Any, Final, Literal, TypedDict import magic import py7zr @@ -264,9 +263,9 @@ class FSRomsHandler(FSHandler): self, file_path: Path, rom_crc_c: int, - rom_md5_h: _Hash, - rom_sha1_h: _Hash, - ) -> tuple[int, int, _Hash, _Hash, _Hash, _Hash]: + rom_md5_h: Any, + rom_sha1_h: Any, + ) -> tuple[int, int, Any, Any, Any, Any]: mime = magic.Magic(mime=True) file_type = mime.from_file(file_path) extension = Path(file_path).suffix.lower() diff --git a/backend/handler/scan_handler.py b/backend/handler/scan_handler.py index 23be53401..f1aeb6886 100644 --- a/backend/handler/scan_handler.py +++ b/backend/handler/scan_handler.py @@ -183,8 +183,9 @@ async def scan_rom( # Set default properties rom_attrs = { - **fs_rom, "id": rom.id if rom else None, + "multi": fs_rom["multi"], + "fs_name": fs_rom["fs_name"], "platform_id": platform.id, "name": fs_rom["fs_name"], "url_cover": "", @@ -212,7 +213,7 @@ async def scan_rom( ) # Update properties that don't require metadata - filesize = sum([file["size"] for file in rom_attrs["files"]]) + filesize = sum([file.file_size_bytes for file in fs_rom["files"]]) regs, rev, langs, other_tags = fs_rom_handler.parse_tags(rom_attrs["fs_name"]) rom_attrs.update( { diff --git a/backend/handler/tests/test_fastapi.py b/backend/handler/tests/test_fastapi.py index 8afb0decc..436fc6c24 100644 --- a/backend/handler/tests/test_fastapi.py +++ b/backend/handler/tests/test_fastapi.py @@ -32,8 +32,9 @@ async def test_scan_rom(): async with initialize_context(): files = [ RomFile( - filename="Paper Mario (USA).z64", - size=1024, + file_name="Paper Mario (USA).z64", + file_path="Paper Mario (USA)", + file_size_bytes=1024, last_modified=1620000000, ) ] diff --git a/backend/models/rom.py b/backend/models/rom.py index d46d094d7..efad4710f 100644 --- a/backend/models/rom.py +++ b/backend/models/rom.py @@ -105,8 +105,7 @@ class Rom(BaseModel): primaryjoin="Rom.id == SiblingRom.rom_id", secondaryjoin="Rom.id == SiblingRom.sibling_rom_id", ) - - files: Mapped[list[RomFile]] = relationship(back_populates="rom") + files: Mapped[list[RomFile]] = relationship(back_populates="rom", lazy="immediate") saves: Mapped[list[Save]] = relationship(back_populates="rom") states: Mapped[list[State]] = relationship(back_populates="rom") screenshots: Mapped[list[Screenshot]] = relationship(back_populates="rom") diff --git a/frontend/src/components/common/Collection/Dialog/AddRoms.vue b/frontend/src/components/common/Collection/Dialog/AddRoms.vue index b6c11dbaa..e7fc607a1 100644 --- a/frontend/src/components/common/Collection/Dialog/AddRoms.vue +++ b/frontend/src/components/common/Collection/Dialog/AddRoms.vue @@ -143,7 +143,7 @@ function closeDialog() { hide-default-header >