Merge pull request #1028 from rommapp/share-public-collections

Public collections
This commit is contained in:
Georges-Antoine Assi
2024-07-25 22:38:21 -04:00
committed by GitHub
9 changed files with 42 additions and 28 deletions

View File

@@ -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

View File

@@ -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"])
@@ -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
@@ -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,
}

View File

@@ -1,5 +1,6 @@
from datetime import datetime
from models.collection import Collection
from pydantic import BaseModel
@@ -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
]

View File

@@ -44,7 +44,7 @@ class RomUserSchema(BaseModel):
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)
@@ -52,7 +52,7 @@ class RomUserSchema(BaseModel):
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,
@@ -123,7 +123,7 @@ class RomSchema(BaseModel):
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
@@ -155,8 +155,8 @@ class DetailedRomSchema(RomSchema):
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()
]
@@ -171,9 +171,9 @@ class DetailedRomSchema(RomSchema):
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

View File

@@ -28,15 +28,9 @@ class DBCollectionsHandler(DBBaseHandler):
)
@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()
)

View File

@@ -181,16 +181,12 @@ class DBRomsHandler(DBBaseHandler):
@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()

View File

@@ -104,10 +104,10 @@ class Rom(BaseModel):
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

View File

@@ -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>

View File

@@ -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 },
});
}