users.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from typing import Any, List
  2. from fastapi import APIRouter, Body, Depends, HTTPException
  3. from fastapi.encoders import jsonable_encoder
  4. from pydantic.networks import EmailStr
  5. from sqlalchemy.orm import Session
  6. from app import crud, models, schemas
  7. from app.api import deps
  8. from app.core.config import settings
  9. router = APIRouter()
  10. @router.get("/")
  11. def read_users(
  12. db: Session = Depends(deps.get_db),
  13. skip: int = 0,
  14. limit: int = 100,
  15. current_user: models.users = Depends(deps.get_current_active_superuser),
  16. ) -> Any:
  17. """
  18. Retrieve users.
  19. """
  20. users = crud.user.get_multi(db, skip=skip, limit=limit)
  21. return users
  22. @router.post("/")
  23. def create_user(
  24. *,
  25. db: Session = Depends(deps.get_db),
  26. user_in: schemas.UserCreate,
  27. current_user: models.users = Depends(deps.get_current_active_superuser),
  28. ) -> Any:
  29. """
  30. Create new user.
  31. """
  32. user = crud.user.get_by_email(db, email=user_in.email)
  33. if user:
  34. raise HTTPException(
  35. status_code=400,
  36. detail="The user with this username already exists in the system.",
  37. )
  38. user = crud.user.create(db, obj_in=user_in)
  39. return user
  40. @router.put("/me")
  41. def update_user_me(
  42. *,
  43. db: Session = Depends(deps.get_db),
  44. password: str = Body(None),
  45. full_name: str = Body(None),
  46. email: EmailStr = Body(None),
  47. current_user: models.users = Depends(deps.get_current_active_user),
  48. ) -> Any:
  49. """
  50. Update own user.
  51. """
  52. current_user_data = jsonable_encoder(current_user)
  53. user_in = schemas.UserUpdate(**current_user_data)
  54. if password is not None:
  55. user_in.hashed_password = password
  56. if email is not None:
  57. user_in.email = email
  58. user = crud.user.update(db, db_obj=current_user, obj_in=user_in)
  59. return user
  60. @router.get("/me", response_model=schemas.UserBase)
  61. def read_user_me(
  62. db: Session = Depends(deps.get_db),
  63. current_user: models.users = Depends(deps.get_current_active_user),
  64. ) -> Any:
  65. """
  66. Get current user.
  67. """
  68. return current_user.__dict__
  69. @router.post("/open")
  70. def create_user_open(
  71. *,
  72. db: Session = Depends(deps.get_db),
  73. password: str = Body(...),
  74. email: EmailStr = Body(...),
  75. account: str = Body(...),
  76. ) -> Any:
  77. """
  78. Create new user without the need to be logged in.
  79. """
  80. if not settings.USERS_OPEN_REGISTRATION:
  81. raise HTTPException(
  82. status_code=403,
  83. detail="Open user registration is forbidden on this server",
  84. )
  85. user = crud.user.get_by_email(db, email=email)
  86. if user:
  87. raise HTTPException(
  88. status_code=400,
  89. detail="The user with this email already exists in the system",
  90. )
  91. user = crud.user.get_by_account(db, account=account)
  92. if user:
  93. raise HTTPException(
  94. status_code=400,
  95. detail="The user with this account already exists in the system",
  96. )
  97. user_in = schemas.UserCreate(hashed_password=password, email=email, account=account)
  98. user = crud.user.create(db, obj_in=user_in)
  99. return user
  100. @router.get("/{user_id}", response_model=schemas.UserBase)
  101. def read_user_by_id(
  102. user_id: int,
  103. current_user: models.users = Depends(deps.get_current_active_user),
  104. db: Session = Depends(deps.get_db),
  105. ) -> Any:
  106. """
  107. Get a specific user by id.
  108. """
  109. user = crud.user.get(db, id=user_id)
  110. if user == current_user:
  111. return user.__dict__
  112. if not crud.user.is_superuser(current_user):
  113. raise HTTPException(
  114. status_code=400, detail="The user doesn't have enough privileges"
  115. )
  116. return user.__dict__
  117. @router.put("/{user_id}")
  118. def update_user(
  119. *,
  120. db: Session = Depends(deps.get_db),
  121. user_id: int,
  122. user_in: schemas.UserUpdate,
  123. current_user: models.users = Depends(deps.get_current_active_superuser),
  124. ) -> Any:
  125. """
  126. Update a user.
  127. """
  128. user = crud.user.get(db, id=user_id)
  129. if not user:
  130. raise HTTPException(
  131. status_code=404,
  132. detail="The user with this username does not exist in the system",
  133. )
  134. user = crud.user.update(db, db_obj=user, obj_in=user_in)
  135. return user