update user

This commit is contained in:
Valentin CZERYBA 2023-10-25 23:42:34 +02:00
parent c120e60911
commit 94bdf0e27a
4 changed files with 23 additions and 8 deletions

View File

@ -10,7 +10,7 @@ def add(username="", password="", roles="User", status=1, email="test@toto.com")
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
result = user_repository.find_one_by({'username': username})
change = "added"
user = users.User(username=username, password=pwd_context.hash(password), status=status, email=email)
user = users.User(username=username, password=pwd_context.hash(password), roles=roles, status=status, email=email)
if result is not None:
result.password=pwd_context.hash(password)
result.roles=roles

View File

@ -35,6 +35,9 @@ def authenticate_user(username: str, password: str):
return False
if not verify_password(password, user.password):
return False
user.connected_at = datetime.today()
user_repository = users.UserRepository(database=database.database)
user_repository.save(user)
return user
def create_access_token(data: dict, expires_delta: timedelta | None = None):

View File

@ -1,32 +1,39 @@
from pydantic import BaseModel, EmailStr
from pydantic_mongo import AbstractRepository, ObjectIdField
from datetime import datetime
from datetime import datetime, date
class User(BaseModel):
id: ObjectIdField = None
username: str
password: str
firstName: str = ""
name: str = ""
roles: str = "User"
status: int = 0
email: EmailStr
birth: datetime = None
birth: str | None = None
created_at: datetime = datetime.today()
connected_at: datetime = None
connected_at: datetime | None = None
updated_at: datetime = datetime.today()
deleted_at: datetime = None
deleted_at: datetime | None = None
class UserOut(BaseModel):
id: ObjectIdField = None
username: str
roles: str
firstName: str
name: str
status: int = 0
email: EmailStr
class UserIn(BaseModel):
username: str
name: str
firstName: str
roles: str
password: str
birth: str
email: EmailStr

View File

@ -1,5 +1,6 @@
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import JSONResponse
from datetime import datetime
from ..dependencies import users_token, permissions_checker, database
from ..models import users
from typing import Annotated
@ -19,7 +20,7 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm
listUsers = []
user_repository = users.UserRepository(database=database.database)
for user_index in user_repository.find_by({}, limit=limit, skip=skip):
user = users.UserOut(id=user_index.id, username=user_index.username, email=user_index.email, status=user_index.status, roles=user_index.roles)
user = users.UserOut(id=user_index.id, username=user_index.username, email=user_index.email, status=user_index.status, roles=user_index.roles, firstName=user_index.firstName, name=user_index.name)
listUsers.append(user)
return listUsers
@ -39,7 +40,7 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P
listUsers = []
user_repository = users.UserRepository(database=database.database)
for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip):
user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email)
user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email, firstName=user_index.firstName, name=user_index.name)
listUsers.append(user)
return listUsers
@ -106,8 +107,12 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P
response.status_code = status.HTTP_201_CREATED
user = users.User()
user.username = userSingle.username
user.password = user_token.get_password_hash(userSingle.password)
user.password = users_token.get_password_hash(userSingle.password)
user.roles = userSingle.roles
user.email = userSingle.email
user.firstName = userSingle.firstName
user.name = userSingle.name
user.birth = userSingle.birth
user.updated_at = datetime.today()
user_repository.save(user)
return user