generateur_v3/backend/api/database/room/models.py
2022-09-16 21:50:55 +02:00

49 lines
1.3 KiB
Python

from typing import List, Optional, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship
if TYPE_CHECKING:
from database.auth.models import User
class RoomBase(SQLModel):
name: str = Field(max_length=20)
public: bool = Field(default=False)
class RoomCreate(RoomBase):
pass
class Room(RoomBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
id_code: str
members: List['Member'] = Relationship(back_populates="room")
class RoomRead(RoomBase):
id_code: str
#members: List[]
class AnonymousBase(SQLModel):
username: str = Field(max_length=20)
class AnonymousCreate(AnonymousBase):
pass
class Anonymous(AnonymousBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
reconnect_code: str
member: 'Member' = Relationship(back_populates="anonymous")
class Member(SQLModel, table = True):
id: Optional[int] = Field(default=None, primary_key=True)
user_id: Optional[int] = Field(foreign_key="user.id", default=None)
anonymous_id: Optional[int] = Field(foreign_key="anonymous.id", default=None)
anonymous: Optional[Anonymous] = Relationship(back_populates="member")
user: Optional['User'] = Relationship(back_populates='members')
room_id: int = Field(foreign_key="room.id")
room: Room = Relationship(back_populates='members')