mirror of
https://github.com/rommapp/romm.git
synced 2026-07-01 08:16:21 +00:00
This change makes some database columns non-nullable, and includes the migration to seamlessly upgrade the schema. It includes: * User and Platform names * Every boolean column
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from functools import cached_property
|
|
|
|
from models.base import BaseModel
|
|
from models.user import User
|
|
from sqlalchemy import JSON, ForeignKey, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
|
|
class Collection(BaseModel):
|
|
__tablename__ = "collections"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
|
|
name: Mapped[str] = mapped_column(String(length=400))
|
|
description: Mapped[str | None] = mapped_column(Text)
|
|
|
|
path_cover_l: Mapped[str | None] = mapped_column(Text, default="")
|
|
path_cover_s: Mapped[str | None] = mapped_column(Text, default="")
|
|
|
|
url_cover: Mapped[str | None] = mapped_column(
|
|
Text, default="", doc="URL to cover image stored in IGDB"
|
|
)
|
|
|
|
roms: Mapped[set[int]] = mapped_column(
|
|
JSON, default=[], doc="Rom id's that belong to this collection"
|
|
)
|
|
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
is_public: Mapped[bool] = mapped_column(default=False)
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="collections")
|
|
|
|
@property
|
|
def user__username(self) -> str:
|
|
return self.user.username
|
|
|
|
@property
|
|
def rom_count(self) -> int:
|
|
return len(self.roms)
|
|
|
|
@cached_property
|
|
def has_cover(self) -> bool:
|
|
return bool(self.path_cover_s or self.path_cover_l)
|
|
|
|
@property
|
|
def fs_resources_path(self) -> str:
|
|
return f"collections/{str(self.id)}"
|
|
|
|
def __repr__(self) -> str:
|
|
return self.name
|