generateur_v3/backend/api/database/room/models.py

93 lines
2.4 KiB
Python
Raw Normal View History

2022-09-21 22:31:50 +02:00
from pydantic import root_validator
2022-09-16 21:50:55 +02:00
from typing import List, Optional, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship
2022-09-18 22:43:04 +02:00
from database.auth.models import UserRead
2022-09-16 21:50:55 +02:00
if TYPE_CHECKING:
from database.auth.models import User
class RoomBase(SQLModel):
name: str = Field(max_length=20)
public: bool = Field(default=False)
2022-09-18 22:43:04 +02:00
2022-09-16 21:50:55 +02:00
class RoomCreate(RoomBase):
pass
class Room(RoomBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
2022-09-18 22:43:04 +02:00
id_code: str = Field(index=True)
2022-09-16 21:50:55 +02:00
members: List['Member'] = Relationship(back_populates="room")
2022-09-18 22:43:04 +02:00
2022-09-16 21:50:55 +02:00
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)
2022-09-18 22:43:04 +02:00
reconnect_code: str = Field(index=True)
2022-09-16 21:50:55 +02:00
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)
2022-09-18 22:43:04 +02:00
user: Optional["User"] = Relationship(back_populates='members')
2022-09-16 21:50:55 +02:00
2022-09-18 22:43:04 +02:00
anonymous_id: Optional[int] = Field(foreign_key="anonymous.id", default=None)
2022-09-16 21:50:55 +02:00
anonymous: Optional[Anonymous] = Relationship(back_populates="member")
2022-09-18 22:43:04 +02:00
2022-09-16 21:50:55 +02:00
room_id: int = Field(foreign_key="room.id")
room: Room = Relationship(back_populates='members')
2022-09-18 22:43:04 +02:00
is_admin: bool = False
2022-09-21 22:31:50 +02:00
waiting: bool = False
2022-09-18 22:43:04 +02:00
online: bool = False
2022-09-21 22:31:50 +02:00
waiter_code: Optional[str] = Field(default= None)
2022-09-25 22:32:19 +02:00
id_code: str
2022-09-18 22:43:04 +02:00
class RoomRead(RoomBase):
id_code: str
#members: List['Member']
class AnonymousRead(AnonymousBase):
reconnect_code: str
2022-09-21 22:31:50 +02:00
2022-09-18 22:43:04 +02:00
class Username(SQLModel):
username: str
2022-09-21 22:31:50 +02:00
class MemberWithRelations(SQLModel):
is_admin: bool
user: UserRead | None = None
anonymous: AnonymousRead | None = None
2022-09-18 22:43:04 +02:00
class MemberRead(SQLModel):
2022-09-21 22:31:50 +02:00
username: str
reconnect_code: str = ''
isUser: bool
isAdmin: bool
2022-09-25 22:32:19 +02:00
id_code: str
2022-09-21 22:31:50 +02:00
class MemberSerializer(MemberRead):
member: MemberWithRelations
@root_validator
def parse_member(cls, values):
member = values.get('member')
if member == None:
return values
member_obj = member.user or member.anonymous
if member_obj is None:
raise ValueError('User or anonymous required')
return {"username": member_obj.username, "reconnect_code": getattr(member_obj, "reconnect_code", ""), "isAdmin": member.is_admin, "isUser": member.user != None}