generateur_v3/backend/api/database/room/models.py
Kilton937342 936931f047 api
2022-10-10 01:34:38 +02:00

215 lines
5.3 KiB
Python

from uuid import UUID, uuid4
from pydantic import root_validator, BaseModel
import pydantic.json
from typing import List, Optional, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship, JSON, Column
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)
global_results: 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")
parcours: List['Parcours'] = 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)
clientId: Optional[UUID] = Field(default=uuid4(), index=True)
member: 'Member' = Relationship(back_populates="anonymous")
class Member(SQLModel, table = True):
id: Optional[int] = Field(default=None, primary_key=True)
id_code: str = Field(index=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')
challenges: List["Challenge"] = Relationship(back_populates="challenger")
is_admin: bool = False
waiting: bool = False
online: bool = False
waiter_code: Optional[str] = Field(default= None)
corrections: List['TmpCorrection'] = Relationship(back_populates="member")
class ExercicesCreate(SQLModel):
exercice_id: str
quantity: int = 10
class Exercices(ExercicesCreate):
name: str
class Parcours(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
id_code: str = Field(index=True, unique=True)
room_id: int = Field(foreign_key="room.id")
room: Room = Relationship(back_populates='parcours')
name: str
time: int
validate_condition: int
exercices: List[Exercices] = Field(sa_column=Column(JSON))
challenges: List["Challenge"] = Relationship(back_populates="parcours")
corrections: List["TmpCorrection"] = Relationship(back_populates="parcours")
class Note(BaseModel):
value: int
total: int
class TimedNote(Note):
time: int
class ParcoursReadShort(SQLModel):
name: str
best_note: str | None = None
id_code: str
class Challenges(SQLModel):
id_code: str
challenger: str
note: Note
time: int
isCorriged: bool
canCorrige: bool
validated: bool
class ParcoursRead(SQLModel):
name: str
time: int
validate_condition: int
id_code: str
exercices: List[Exercices]
challenges: List[Challenges]
class ParcoursCreate(SQLModel):
name: str
time: int
validate_condition: int
exercices: List[ExercicesCreate]
class NotCorrigedInput(BaseModel):
index: int
value: str
class CorrigedInput(NotCorrigedInput):
correction: str
class ParsedGeneratorOut(BaseModel):
calcul: str
inputs: List[NotCorrigedInput]
class CorrigedGeneratorOut(BaseModel):
calcul: str
inputs: List[CorrigedInput]
class Challenge(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
id_code: str = Field(index=True, unique=True)
challenger_id: int = Field(foreign_key="member.id")
challenger: Member = Relationship(back_populates="challenges")
parcours_id: int = Field(foreign_key="parcours.id_code")
parcours: Parcours = Relationship(back_populates="challenges")
data: Optional[List[List[CorrigedGeneratorOut]]] = Field(sa_column=Column(JSON), default=[])
time: int
note: Note = Field(
sa_column=Column(JSON))
validated: bool
isCorriged: bool
class ChallengeRead(SQLModel):
id_code: str
data: Optional[List[List[CorrigedGeneratorOut]]] = []
time: int
note: Note
validated: bool
isCorriged: bool
class TmpCorrection(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
id_code: str = Field(index=True)
parcours_id: str = Field(foreign_key="parcours.id_code")
parcours: Parcours = Relationship(back_populates="corrections")
member_id: int = Field(foreign_key="member.id")
member: Member = Relationship(back_populates="corrections")
data: List[List[CorrigedGeneratorOut]] = Field(sa_column=Column(JSON))
class AnonymousRead(AnonymousBase):
reconnect_code: str
class Username(SQLModel):
username: str
class MemberRead(SQLModel):
username: str
reconnect_code: str = ''
isUser: bool
isAdmin: bool
id_code: str
class RoomRead(RoomBase):
id_code: str
class RoomAndMember(BaseModel):
room: RoomRead
member: MemberRead
class RoomInfo(RoomRead):
public: bool
name: str
members: List[MemberRead]
parcours: List[ParcoursReadShort]
class Waiter(BaseModel):
username: str
waiter_id: str
class RoomConnectionInfos(BaseModel):
room: str
member: str | None = None