generateur_v3/backend/api/database/room/models.py
Kilton937342 84b38cc12d ...
2022-09-18 22:43:04 +02:00

65 lines
1.7 KiB
Python

from typing import List, Optional, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship
from database.auth.models import UserRead
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 = Field(index=True)
members: List['Member'] = Relationship(back_populates="room")
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 = Field(index=True)
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)
user: Optional["User"] = Relationship(back_populates='members')
anonymous_id: Optional[int] = Field(foreign_key="anonymous.id", default=None)
anonymous: Optional[Anonymous] = Relationship(back_populates="member")
room_id: int = Field(foreign_key="room.id")
room: Room = Relationship(back_populates='members')
is_admin: bool = False
waiting: bool = True
online: bool = False
class RoomRead(RoomBase):
id_code: str
#members: List['Member']
class AnonymousRead(AnonymousBase):
reconnect_code: str
class Username(SQLModel):
username: str
class MemberRead(SQLModel):
anonymous: AnonymousRead = None
user: Username = None