Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…python_fastapi_web into feat/post-user-faq-inquiries-form
  • Loading branch information
JoshuaOloton committed Aug 23, 2024
2 parents 4f284fa + 2a994ba commit 32391e0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 27 deletions.
37 changes: 33 additions & 4 deletions api/v1/routes/auth.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
from datetime import timedelta
from fastapi import BackgroundTasks, Depends, status, APIRouter, Response, Request
from fastapi import (BackgroundTasks, Depends,
status, APIRouter,
Response, Request)
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session
from typing import Annotated

from api.core.dependencies.email_sender import send_email
from api.utils.success_response import auth_response, success_response
from api.utils.send_mail import send_magic_link
from api.v1.models import User
from api.v1.schemas.user import Token
from api.v1.schemas.user import LoginRequest, UserCreate, EmailRequest
from api.v1.schemas.user import (LoginRequest, UserCreate, EmailRequest,
ProfileData, UserData2)
from api.v1.schemas.token import TokenRequest
from api.v1.schemas.user import UserCreate, MagicLinkRequest, ChangePasswordSchema
from api.v1.schemas.user import (UserCreate,
MagicLinkRequest,
ChangePasswordSchema,
AuthMeResponse)
from api.v1.services.organisation import organisation_service
from api.v1.schemas.organisation import CreateUpdateOrganisation
from api.db.database import get_db
from api.v1.services.user import user_service
from api.v1.services.auth import AuthService
from api.v1.services.profile import profile_service

auth = APIRouter(prefix="/auth", tags=["Authentication"])

Expand Down Expand Up @@ -357,3 +364,25 @@ async def change_password(
old_password=schema.old_password)

return success_response(status_code=200, message="Password changed successfully")


@auth.get("/@me",
status_code=status.HTTP_200_OK,
response_model=AuthMeResponse)
def get_current_user_details(
db: Annotated[Session, Depends(get_db)],
current_user: Annotated[User, Depends(user_service.get_current_user)],
):
"""Endpoint to get current user details.
"""
profile = profile_service.fetch_by_user_id(db, current_user.id)
organisation = organisation_service.retrieve_user_organizations(current_user, db)
return AuthMeResponse(
message='User details retrieved successfully',
status_code=200,
data={
'user': UserData2.model_validate(current_user, from_attributes=True),
'organisations': organisation,
'profile': ProfileData.model_validate(profile, from_attributes=True)
}
)
22 changes: 0 additions & 22 deletions api/v1/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,6 @@
user_router = APIRouter(prefix="/users", tags=["Users"])


@user_router.get("/me", status_code=status.HTTP_200_OK, response_model=success_response)
def get_current_user_details(
db: Session = Depends(get_db),
current_user: User = Depends(user_service.get_current_user),
):
"""Endpoint to get current user details"""

return success_response(
status_code=200,
message="User details retrieved successfully",
data=jsonable_encoder(
current_user,
exclude=[
"password",
"is_deleted",
"is_verified",
"updated_at",
],
),
)


@user_router.get('/delete', status_code=200)
async def delete_account(request: Request, db: Session = Depends(get_db), current_user: User = Depends(user_service.get_current_user)):
'''Endpoint to delete a user account'''
Expand Down
64 changes: 63 additions & 1 deletion api/v1/schemas/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from email_validator import validate_email, EmailNotValidError
import dns.resolver
from datetime import datetime
from typing import Optional, Union, List, Annotated
from typing import (Optional, Union,
List, Annotated, Dict,
Literal)

from pydantic import (BaseModel, EmailStr,
field_validator, ConfigDict,
Expand Down Expand Up @@ -115,6 +117,66 @@ class UserData(BaseModel):

model_config = ConfigDict(from_attributes=True)

class UserData2(BaseModel):
"""
Schema for users to be returned to superadmin
"""
id: str
email: EmailStr
first_name: str
last_name: str
is_active: bool
is_deleted: bool
is_verified: bool
created_at: datetime
updated_at: datetime

model_config = ConfigDict(from_attributes=True)

class ProfileData(BaseModel):
"""
Pydantic model for a profile.
"""
id: str
created_at: datetime
pronouns: Optional[str] = None
job_title: Optional[str] = None
department: Optional[str] = None
social: Optional[str] = None
bio: Optional[str] = None
phone_number: Optional[str] = None
avatar_url: Optional[str] = None
recovery_email: Optional[EmailStr]

model_config = ConfigDict(from_attributes=True)

class OrganisationData(BaseModel):
"""Base organisation schema"""
id: str
created_at: datetime
updated_at: datetime
name: str
email: Optional[EmailStr] = None
industry: Optional[str] = None
user_role: List[str]
type: Optional[str] = None
country: Optional[str] = None
state: Optional[str] = None
address: Optional[str] = None
description: Optional[str] = None

model_config = ConfigDict(from_attributes=True)

class AuthMeResponse(BaseModel):
"""
Auth response
"""
message: str
status_code: int
data: Dict[Literal["user", "organisations", "profile"],
Union[UserData2, List[OrganisationData], ProfileData]]


class AllUsersResponse(BaseModel):
"""
Schema for all users
Expand Down

0 comments on commit 32391e0

Please sign in to comment.