From 94bdf0e27af63b1f03e535f171281144c6593d96 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 25 Oct 2023 23:42:34 +0200 Subject: [PATCH] update user --- app/dependencies/user_add.py | 2 +- app/dependencies/users_token.py | 3 +++ app/models/users.py | 15 +++++++++++---- app/routers/users.py | 11 ++++++++--- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/app/dependencies/user_add.py b/app/dependencies/user_add.py index a9f5a70..9c8e85d 100644 --- a/app/dependencies/user_add.py +++ b/app/dependencies/user_add.py @@ -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 diff --git a/app/dependencies/users_token.py b/app/dependencies/users_token.py index b34ea00..b494a37 100644 --- a/app/dependencies/users_token.py +++ b/app/dependencies/users_token.py @@ -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): diff --git a/app/models/users.py b/app/models/users.py index 225a0ed..bacfa46 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -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 diff --git a/app/routers/users.py b/app/routers/users.py index ae27d57..fa431aa 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -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 \ No newline at end of file