123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import imp
- from typing import Any, List
- from fastapi import APIRouter, Body, Depends, HTTPException
- from fastapi.encoders import jsonable_encoder
- from pydantic.networks import EmailStr
- from sqlalchemy.orm import Session
- from app import crud, models, schemas
- from app.api import deps
- from app.core.config import settings
- from app.api.api_v1.endpoints.line import LineRouter
- router = APIRouter(route_class=LineRouter)
- @router.get("/")
- def read_users(
- db: Session = Depends(deps.get_db),
- skip: int = 0,
- limit: int = 100,
- current_user: models.users = Depends(deps.get_current_active_superuser),
- ) -> Any:
- """
- Retrieve users.
- """
- users = crud.user.get_multi(db, skip=skip, limit=limit)
- return users
- @router.post("/")
- def create_user(
- *,
- db: Session = Depends(deps.get_db),
- user_in: schemas.UserCreate,
- current_user: models.users = Depends(deps.get_current_active_superuser),
- ) -> Any:
- """
- Create new user.
- """
- user = crud.user.get_by_email(db, email=user_in.email)
- if user:
- raise HTTPException(
- status_code=400,
- detail="The user with this username already exists in the system.",
- )
- user = crud.user.create(db, obj_in=user_in)
- return user
- @router.put("/me")
- def update_user_me(
- *,
- db: Session = Depends(deps.get_db),
- password: str = Body(None),
- full_name: str = Body(None),
- email: EmailStr = Body(None),
- current_user: models.users = Depends(deps.get_current_active_user),
- ) -> Any:
- """
- Update own user.
- """
- current_user_data = jsonable_encoder(current_user)
- user_in = schemas.UserUpdate(**current_user_data)
- if password is not None:
- user_in.hashed_password = password
- if email is not None:
- user_in.email = email
- user = crud.user.update(db, db_obj=current_user, obj_in=user_in)
- return user
- @router.get("/me", response_model=schemas.UserBase)
- def read_user_me(
- db: Session = Depends(deps.get_db),
- current_user: models.users = Depends(deps.get_current_active_user),
- ) -> Any:
- """
- Get current user.
- """
- return current_user.__dict__
- @router.post("/open")
- def create_user_open(
- *,
- db: Session = Depends(deps.get_db),
- password: str = Body(...),
- email: EmailStr = Body(...),
- account: str = Body(...),
- ) -> Any:
- """
- Create new user without the need to be logged in.
- """
- if not settings.USERS_OPEN_REGISTRATION:
- raise HTTPException(
- status_code=403,
- detail="Open user registration is forbidden on this server",
- )
- user = crud.user.get_by_email(db, email=email)
- if user:
- raise HTTPException(
- status_code=400,
- detail="The user with this email already exists in the system",
- )
- user = crud.user.get_by_account(db, account=account)
- if user:
- raise HTTPException(
- status_code=400,
- detail="The user with this account already exists in the system",
- )
- user_in = schemas.UserCreate(hashed_password=password, email=email, account=account)
- user = crud.user.create(db, obj_in=user_in)
- return user
- @router.get("/{user_id}", response_model=schemas.UserBase)
- def read_user_by_id(
- user_id: int,
- current_user: models.users = Depends(deps.get_current_active_user),
- db: Session = Depends(deps.get_db),
- ) -> Any:
- """
- Get a specific user by id.
- """
- user = crud.user.get(db, id=user_id)
- if user == current_user:
- return user.__dict__
- if not crud.user.is_superuser(current_user):
- raise HTTPException(
- status_code=400, detail="The user doesn't have enough privileges"
- )
- return user.__dict__
- @router.put("/{user_id}")
- def update_user(
- *,
- db: Session = Depends(deps.get_db),
- user_id: int,
- user_in: schemas.UserUpdate,
- current_user: models.users = Depends(deps.get_current_active_superuser),
- ) -> Any:
- """
- Update a user.
- """
- user = crud.user.get(db, id=user_id)
- if not user:
- raise HTTPException(
- status_code=404,
- detail="The user with this username does not exist in the system",
- )
- user = crud.user.update(db, db_obj=user, obj_in=user_in)
- return user
|