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

215 lines
5.3 KiB
Python
Raw Normal View History

2022-10-10 01:34:38 +02:00
from uuid import UUID, uuid4
2022-09-26 10:04:02 +02:00
from pydantic import root_validator, BaseModel
2022-10-10 01:34:38 +02:00
import pydantic.json
2022-09-16 21:50:55 +02:00
from typing import List, Optional, TYPE_CHECKING
2022-10-10 01:34:38 +02:00
from sqlmodel import SQLModel, Field, Relationship, JSON, Column
2022-09-18 22:43:04 +02:00
from database.auth.models import UserRead
2022-10-10 01:34:38 +02:00
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-10-10 01:34:38 +02:00
global_results: 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-10-10 01:34:38 +02:00
parcours: List['Parcours'] = Relationship(back_populates="room")
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-10-10 01:34:38 +02:00
clientId: Optional[UUID] = Field(default=uuid4(), 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)
2022-10-10 01:34:38 +02:00
id_code: str = Field(index=True)
2022-09-16 21:50:55 +02:00
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-10-10 01:34:38 +02:00
challenges: List["Challenge"] = Relationship(back_populates="challenger")
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-10-10 01:34:38 +02:00
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
2022-09-25 22:32:19 +02:00
id_code: str
2022-10-10 01:34:38 +02:00
class Challenges(SQLModel):
2022-09-18 22:43:04 +02:00
id_code: str
2022-10-10 01:34:38 +02:00
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
2022-09-18 22:43:04 +02:00
2022-10-10 01:34:38 +02:00
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))
2022-09-18 22:43:04 +02:00
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
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
2022-10-10 01:34:38 +02:00
class RoomRead(RoomBase):
id_code: str
2022-09-26 10:04:02 +02:00
class RoomAndMember(BaseModel):
room: RoomRead
member: MemberRead
2022-10-10 01:34:38 +02:00
class RoomInfo(RoomRead):
public: bool
name: str
members: List[MemberRead]
parcours: List[ParcoursReadShort]
2022-09-26 10:04:02 +02:00
class Waiter(BaseModel):
username: str
waiter_id: str
2022-10-10 01:34:38 +02:00
class RoomConnectionInfos(BaseModel):
room: str
member: str | None = None