59 lines
1.4 KiB
Python
59 lines
1.4 KiB
Python
from typing import Optional
|
|
from tortoise.contrib.pydantic import pydantic_model_creator
|
|
from services.schema import as_form
|
|
from database.auth.models import User
|
|
from pydantic import BaseModel, validator
|
|
from services.password import validate_password
|
|
User_schema = pydantic_model_creator(User, name='users', include=[
|
|
'username', 'email', "name", "firstname"])
|
|
UserIn_schema = pydantic_model_creator(
|
|
User, name='usersIn', exclude_readonly=True)
|
|
|
|
|
|
@as_form
|
|
class UserForm(BaseModel):
|
|
username: str
|
|
firstname: Optional[str]
|
|
name: Optional[str]
|
|
email: Optional[str]
|
|
|
|
|
|
@as_form
|
|
class UserIn_Form(UserIn_schema):
|
|
pass
|
|
|
|
|
|
@as_form
|
|
class UserLog(BaseModel):
|
|
username: str
|
|
password: str
|
|
|
|
|
|
@as_form
|
|
class UserRegister(UserLog):
|
|
password_confirm: str
|
|
|
|
@validator('username')
|
|
def username_alphanumeric(cls, v):
|
|
assert v.isalnum(), 'must be alphanumeric'
|
|
return v
|
|
|
|
@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):
|
|
password: str
|
|
password_confirm: str
|