update user
This commit is contained in:
parent
c120e60911
commit
94bdf0e27a
@ -10,7 +10,7 @@ def add(username="", password="", roles="User", status=1, email="test@toto.com")
|
|||||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||||
result = user_repository.find_one_by({'username': username})
|
result = user_repository.find_one_by({'username': username})
|
||||||
change = "added"
|
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:
|
if result is not None:
|
||||||
result.password=pwd_context.hash(password)
|
result.password=pwd_context.hash(password)
|
||||||
result.roles=roles
|
result.roles=roles
|
||||||
|
@ -35,6 +35,9 @@ def authenticate_user(username: str, password: str):
|
|||||||
return False
|
return False
|
||||||
if not verify_password(password, user.password):
|
if not verify_password(password, user.password):
|
||||||
return False
|
return False
|
||||||
|
user.connected_at = datetime.today()
|
||||||
|
user_repository = users.UserRepository(database=database.database)
|
||||||
|
user_repository.save(user)
|
||||||
return user
|
return user
|
||||||
|
|
||||||
def create_access_token(data: dict, expires_delta: timedelta | None = None):
|
def create_access_token(data: dict, expires_delta: timedelta | None = None):
|
||||||
|
@ -1,32 +1,39 @@
|
|||||||
from pydantic import BaseModel, EmailStr
|
from pydantic import BaseModel, EmailStr
|
||||||
from pydantic_mongo import AbstractRepository, ObjectIdField
|
from pydantic_mongo import AbstractRepository, ObjectIdField
|
||||||
from datetime import datetime
|
from datetime import datetime, date
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
id: ObjectIdField = None
|
id: ObjectIdField = None
|
||||||
username: str
|
username: str
|
||||||
password: str
|
password: str
|
||||||
|
firstName: str = ""
|
||||||
|
name: str = ""
|
||||||
roles: str = "User"
|
roles: str = "User"
|
||||||
status: int = 0
|
status: int = 0
|
||||||
email: EmailStr
|
email: EmailStr
|
||||||
birth: datetime = None
|
birth: str | None = None
|
||||||
created_at: datetime = datetime.today()
|
created_at: datetime = datetime.today()
|
||||||
connected_at: datetime = None
|
connected_at: datetime | None = None
|
||||||
updated_at: datetime = datetime.today()
|
updated_at: datetime = datetime.today()
|
||||||
deleted_at: datetime = None
|
deleted_at: datetime | None = None
|
||||||
|
|
||||||
class UserOut(BaseModel):
|
class UserOut(BaseModel):
|
||||||
id: ObjectIdField = None
|
id: ObjectIdField = None
|
||||||
username: str
|
username: str
|
||||||
roles: str
|
roles: str
|
||||||
|
firstName: str
|
||||||
|
name: str
|
||||||
status: int = 0
|
status: int = 0
|
||||||
email: EmailStr
|
email: EmailStr
|
||||||
|
|
||||||
|
|
||||||
class UserIn(BaseModel):
|
class UserIn(BaseModel):
|
||||||
username: str
|
username: str
|
||||||
|
name: str
|
||||||
|
firstName: str
|
||||||
roles: str
|
roles: str
|
||||||
password: str
|
password: str
|
||||||
|
birth: str
|
||||||
email: EmailStr
|
email: EmailStr
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
|
from datetime import datetime
|
||||||
from ..dependencies import users_token, permissions_checker, database
|
from ..dependencies import users_token, permissions_checker, database
|
||||||
from ..models import users
|
from ..models import users
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
@ -19,7 +20,7 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm
|
|||||||
listUsers = []
|
listUsers = []
|
||||||
user_repository = users.UserRepository(database=database.database)
|
user_repository = users.UserRepository(database=database.database)
|
||||||
for user_index in user_repository.find_by({}, limit=limit, skip=skip):
|
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)
|
listUsers.append(user)
|
||||||
return listUsers
|
return listUsers
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P
|
|||||||
listUsers = []
|
listUsers = []
|
||||||
user_repository = users.UserRepository(database=database.database)
|
user_repository = users.UserRepository(database=database.database)
|
||||||
for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip):
|
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)
|
listUsers.append(user)
|
||||||
return listUsers
|
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
|
response.status_code = status.HTTP_201_CREATED
|
||||||
user = users.User()
|
user = users.User()
|
||||||
user.username = userSingle.username
|
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.roles = userSingle.roles
|
||||||
user.email = userSingle.email
|
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)
|
user_repository.save(user)
|
||||||
return user
|
return user
|
Loading…
x
Reference in New Issue
Block a user