Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public collections #1028

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEVELOPER_SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ touch romm_mock/library/roms/switch/metroid.xci
mkdir -p romm_mock/resources
mkdir -p romm_mock/assets
mkdir -p romm_mock/config
touch romm_mock/config.yml
touch romm_mock/config/config.yml
```

### Setting up the backend
Expand Down
6 changes: 4 additions & 2 deletions backend/endpoints/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def get_collections(request: Request) -> list[CollectionSchema]:
list[CollectionSchema]: List of collections
"""

return db_collection_handler.get_collections(user_id=request.user.id)
collections = db_collection_handler.get_collections()
return CollectionSchema.for_user(request.user.id, collections)


@protected_route(router.get, "/collections/{id}", ["collections.read"])
Expand Down Expand Up @@ -128,6 +129,7 @@ async def update_collection(
request: Request,
id: int,
remove_cover: bool = False,
is_public: bool | None = None,
artwork: UploadFile | None = None,
) -> CollectionSchema:
"""Update collection endpoint
Expand Down Expand Up @@ -159,8 +161,8 @@ async def update_collection(
cleaned_data = {
"name": data.get("name", collection.name),
"description": data.get("description", collection.description),
"is_public": is_public if is_public is not None else collection.is_public,
"roms": list(set(roms)),
"is_public": data.get("is_public", collection.is_public),
"user_id": request.user.id,
}

Expand Down
11 changes: 11 additions & 0 deletions backend/endpoints/responses/collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from models.collection import Collection
from pydantic import BaseModel


Expand All @@ -22,3 +23,13 @@ class CollectionSchema(BaseModel):

class Config:
from_attributes = True

@classmethod
def for_user(
cls, user_id: int, collections: list["Collection"]
) -> list["CollectionSchema"]:
return [
cls.model_validate(c)
for c in collections
if c.user_id == user_id or c.is_public
]
16 changes: 8 additions & 8 deletions backend/endpoints/responses/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ class Config:
from_attributes = True

@classmethod
def for_user(cls, db_rom: Rom, user_id: int) -> RomUserSchema | None:
def for_user(cls, user_id: int, db_rom: Rom) -> RomUserSchema | None:
for n in db_rom.rom_users:
if n.user_id == user_id:
return cls.model_validate(n)

return None

@classmethod
def notes_for_user(cls, db_rom: Rom, user_id: int) -> list[UserNotesSchema]:
def notes_for_user(cls, user_id: int, db_rom: Rom) -> list[UserNotesSchema]:
return [
{
"user_id": n.user_id,
Expand Down Expand Up @@ -123,7 +123,7 @@ def from_orm_with_request(cls, db_rom: Rom, request: Request) -> RomSchema:
rom = cls.model_validate(db_rom)
user_id = request.user.id

rom.rom_user = RomUserSchema.for_user(db_rom, user_id)
rom.rom_user = RomUserSchema.for_user(user_id, db_rom)

return rom

Expand Down Expand Up @@ -155,8 +155,8 @@ def from_orm_with_request(cls, db_rom: Rom, request: Request) -> DetailedRomSche
rom = cls.model_validate(db_rom)
user_id = request.user.id

rom.rom_user = RomUserSchema.for_user(db_rom, user_id)
rom.user_notes = RomUserSchema.notes_for_user(db_rom, user_id)
rom.rom_user = RomUserSchema.for_user(user_id, db_rom)
rom.user_notes = RomUserSchema.notes_for_user(user_id, db_rom)
rom.sibling_roms = [
RomSchema.model_validate(r) for r in db_rom.get_sibling_roms()
]
Expand All @@ -171,9 +171,9 @@ def from_orm_with_request(cls, db_rom: Rom, request: Request) -> DetailedRomSche
for s in db_rom.screenshots
if s.user_id == user_id
]
rom.user_collections = [
CollectionSchema.model_validate(c) for c in db_rom.get_collections(user_id)
]
rom.user_collections = CollectionSchema.for_user(
user_id, db_rom.get_collections()
)

return rom

Expand Down
10 changes: 2 additions & 8 deletions backend/handler/database/collections_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,9 @@ def get_collection_by_name(
)

@begin_session
def get_collections(
self, user_id: int, session: Session = None
) -> Select[tuple[Collection]]:
def get_collections(self, session: Session = None) -> Select[tuple[Collection]]:
return (
session.scalars(
select(Collection)
.filter_by(user_id=user_id)
.order_by(Collection.name.asc())
) # type: ignore[attr-defined]
session.scalars(select(Collection).order_by(Collection.name.asc())) # type: ignore[attr-defined]
.unique()
.all()
)
Expand Down
8 changes: 2 additions & 6 deletions backend/handler/database/roms_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,12 @@ def get_sibling_roms(

@begin_session
def get_rom_collections(
self, rom: Rom, user_id: int, session: Session = None
self, rom: Rom, session: Session = None
) -> list[Collection]:

return (
session.scalars(
select(Collection)
.filter(
func.json_contains(Collection.roms, f"{rom.id}"),
Collection.user_id == user_id,
)
.filter(func.json_contains(Collection.roms, f"{rom.id}"))
.order_by(Collection.name.asc())
)
.unique()
Expand Down
4 changes: 2 additions & 2 deletions backend/models/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ def get_sibling_roms(self) -> list[Rom]:

return db_rom_handler.get_sibling_roms(self)

def get_collections(self, user_id) -> list[Collection]:
def get_collections(self) -> list[Collection]:
from handler.database import db_rom_handler

return db_rom_handler.get_rom_collections(self, user_id)
return db_rom_handler.get_rom_collections(self)

# Metadata fields
@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ function closeDialog() {
/>
</v-col>
</v-row>
<v-row class="pa-2" no-gutters>
<v-col>
<v-switch
v-model="collection.is_public"
:label="collection.is_public ? 'Public (visible to everyone)' : 'Private (only visible to me)'"
color="romm-accent-1"
class="px-2"
hide-details
/>
</v-col>
</v-row>
</v-col>
<v-col>
<v-row class="pa-2 justify-center" no-gutters>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/services/api/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function updateCollection({
formData.append("roms", JSON.stringify(collection.roms));
if (collection.artwork) formData.append("artwork", collection.artwork);
return api.put(`/collections/${collection.id}`, formData, {
params: { remove_cover: removeCover },
params: { is_public: collection.is_public, remove_cover: removeCover },
});
}

Expand Down
Loading