users.py 4.2 KB

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