generateur_v3/backend/api/database/auth/models.py
Kilton937342 4941b5a154 tests
2023-02-22 12:43:39 +01:00

83 lines
2.3 KiB
Python

import uuid
from typing import List
from typing import TYPE_CHECKING, Optional
from uuid import UUID
from pydantic import validator, BaseModel
from sqlmodel import Field, SQLModel, Relationship
from services.password import validate_password
from services.schema import as_form
if TYPE_CHECKING:
from database.exercices.models import Exercice, Tag
from database.room.models import Member
class UserBase(SQLModel):
username: str = Field(max_length=20, index= True)
firstname: Optional[str]
name: Optional[str]
email: Optional[str]
class User(UserBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
clientId: Optional[UUID] = Field(default=uuid.uuid4(), index=True)
hashed_password: str
exercices: List['Exercice'] = Relationship(back_populates='author')
tags: List['Tag'] = Relationship(back_populates='author')
members: List["Member"] = Relationship(back_populates='user')
@as_form
class UserEdit(UserBase):
pass
class UsersRoom(BaseModel):
name: str
id_code: str
admin: bool = False
class UserRead(UserBase):
id: int
rooms: List[UsersRoom] = []
class UserEditRead(UserBase):
id: int
#rooms: List[UsersRoom] = []
@as_form
class UserRegister(BaseModel):
username: str = Field(max_length=20)
password: str
password_confirm: str
@validator('password')
def password_validation(cls, v):
is_valid = validate_password(v)
if is_valid != True:
raise ValueError(is_valid)
return v
@validator('password_confirm')
def password_match(cls, v, values):
if 'password' in values and v != values['password']:
raise ValueError('Les mots de passe ne correspondent pas')
return v
@as_form
class PasswordSet(BaseModel):
old_password: str
password: str
password_confirm: str
@validator('password')
def password_validation(cls, v):
is_valid = validate_password(v)
if is_valid != True:
raise ValueError(is_valid)
return v
@validator('password_confirm')
def password_match(cls, v, values):
if 'password' in values and v != values['password']:
raise ValueError('Les mots de passe ne correspondent pas')
return v