Skip to content

Commit

Permalink
привел api системы личных кабинетов в соответствие с прототипом
Browse files Browse the repository at this point in the history
  • Loading branch information
Puzanovim committed May 30, 2021
1 parent 74ac0e5 commit 3585892
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 161 deletions.
4 changes: 2 additions & 2 deletions src/accounts/client_account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Client(Base):
__tablename__ = 'client'

id = Column(Integer, primary_key=True, index=True, unique=True)
name = Column(String, unique=True, nullable=False) # TODO can be unique?
name = Column(String, nullable=False)
avatar = Column(String, nullable=True)
is_active = Column(Boolean, default=False, nullable=False)
date_create = Column(DateTime(timezone=True), server_default=sql.func.now())
date_block = Column(DateTime, default=None, nullable=True)
owner_id = Column(String, ForeignKey('user.id'), nullable=False)
# avatar


clients = Client.__table__
55 changes: 27 additions & 28 deletions src/accounts/client_account/routers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
from typing import List
from typing import List, Union

from fastapi import APIRouter, Depends, status, Request
from .schemas import ClientDB, Client, ClientCreate, ClientUpdate, ClientAndOwnerCreate
from .services import get_clients, get_client, add_client, update_client, add_owner, get_client_owner, update_owner
from fastapi import APIRouter, Depends, status, Request, HTTPException
from .schemas import ClientDB, Client, ClientCreate, ClientUpdate, ClientAndOwnerCreate, ClientsPage, ClientPage, \
DevClientPage
from .services import get_clients, get_client, add_client, update_client, add_owner, get_client_owner, update_client_owner, \
get_clients_page, get_client_page, block_client, get_dev_client_page, get_client_info
from src.users.models import UserTable
from src.users.logic import developer_user, any_user, get_client_users_with_superuser, get_owner_with_superuser
from src.reference_book.api.licence import client_licences_router
from src.users.logic import developer_user, any_user, get_client_users_with_superuser, get_owner_with_superuser, \
get_client_users
from ..employee_account.routers import employee_router
from ...users.schemas import UserCreate

client_router = APIRouter()


@client_router.get("/", response_model=List[ClientDB], status_code=status.HTTP_200_OK)
@client_router.get("/", response_model=ClientsPage, status_code=status.HTTP_200_OK)
async def clients_list(user: UserTable = Depends(developer_user)):
return await get_clients()
return await get_clients_page()


@client_router.post("/", response_model=ClientDB, status_code=status.HTTP_201_CREATED)
Expand All @@ -23,35 +25,32 @@ async def create_client(item: ClientAndOwnerCreate, user: UserTable = Depends(de
return new_client


@client_router.get("/{id}", response_model=Client, status_code=status.HTTP_200_OK)
@client_router.get("/{id}", response_model=Union[ClientPage, DevClientPage], status_code=status.HTTP_200_OK)
async def client(id: int, user: UserTable = Depends(any_user)):
user = await get_client_users_with_superuser(id, user)
return await get_client(id)
if user.is_superuser:
return await get_dev_client_page(id)
elif await get_client_users(id, user):
return await get_client_page(id)
else:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)


@client_router.patch("/{id}", response_model=ClientDB, status_code=status.HTTP_201_CREATED)
@client_router.get("/{id}/info", response_model=Client, status_code=status.HTTP_200_OK)
async def client_info(id: int, user: UserTable = Depends(any_user)):
user = await get_owner_with_superuser(id, user)
return await get_client_info(id)


@client_router.patch("/{id}/info", response_model=ClientDB, status_code=status.HTTP_201_CREATED)
async def update_client_by_id(id: int, item: ClientUpdate, user: UserTable = Depends(any_user)):
# TODO разделить изменение аватарки владельцем и изменение владельца владельцем или разработчиком
user = await get_owner_with_superuser(id, user)
return await update_client(id, item)


@client_router.get("/{id}/owner", status_code=status.HTTP_200_OK)
async def owner(id: int, user: UserTable = Depends(any_user)):
user = await get_client_users_with_superuser(id, user)
return await get_client_owner(id)


@client_router.post("/{id}/owner", status_code=status.HTTP_201_CREATED)
async def create_owner(id: int, item: UserCreate, user: UserTable = Depends(developer_user)):
return await add_owner(id, item)


@client_router.patch("/{id}/owner", status_code=status.HTTP_201_CREATED)
async def change_owner(id: int, item: ClientUpdate, user: UserTable = Depends(developer_user)):
user = await get_owner_with_superuser(id, user)
return await update_owner(id, item.owner_id)
@client_router.patch("/{id}/block", response_model=ClientDB, status_code=status.HTTP_201_CREATED)
async def block_client_by_id(id: int, user: UserTable = Depends(developer_user)):
return await block_client(id)


client_router.include_router(client_licences_router)
client_router.include_router(employee_router)
55 changes: 43 additions & 12 deletions src/accounts/client_account/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
from typing import Optional, List

from pydantic import BaseModel, EmailStr, validator
from pydantic.types import UUID4

from ...reference_book.schemas import LicenceDB, SoftwareDB
from ...users.schemas import UserDB, generate_pwd
from ...reference_book.schemas import LicenceDB, SoftwareDB, Licence
from ...users.schemas import UserDB, generate_pwd, Employee, EmployeeList


class ClientBase(BaseModel):
name: str
avatar: Optional[str]


class ClientCreate(ClientBase):
name: Optional[str]
owner_id: Optional[str]
avatar: Optional[str]


class ClientAndOwnerCreate(ClientCreate):
Expand All @@ -23,9 +24,13 @@ class ClientAndOwnerCreate(ClientCreate):
patronymic: Optional[str]
email: EmailStr
password: str = generate_pwd()
avatar: Optional[str]
owner_avatar: Optional[str]
is_active: Optional[bool] = True
is_superuser: Optional[bool] = False
is_verified: Optional[bool] = False
owner_licence: int
licences_list: List[int]

@validator('password')
def valid_password(cls, v: str):
Expand All @@ -36,24 +41,50 @@ def valid_password(cls, v: str):

class ClientUpdate(ClientBase): # TODO доработать изменение заказчика
name: Optional[str]
# avatar
pass
owner_id: Optional[str]


class Client(ClientBase):
class ClientDB(ClientBase):
id: int
is_active: bool
date_create: datetime
date_block: Optional[datetime]
owner_id: str


class ClientShort(ClientDB):
is_active: bool
owner: UserDB
employees: List[UserDB]
licences: List[LicenceDB]
software: List[SoftwareDB]
count_employees: int = 0


class ClientDB(ClientBase):
class Client(ClientBase):
id: int
is_active: bool
date_create: datetime
date_block: Optional[datetime]
owner_id: str
owner: Employee
licences: List[Licence]


class ClientPage(ClientBase):
client: Client
employees_list: List[EmployeeList]
licences_list: List[Licence]


class DevClientPage(ClientBase):
client: Client
employees_list: List[Employee] = []
software_list: List[SoftwareDB]


class ClientsPage(ClientBase):
clients_list: List[ClientShort]
licences_list: List[LicenceDB]


class EmployeePage(BaseModel):
employees: Employee
client: Client
licences: List[Licence]

Loading

0 comments on commit 3585892

Please sign in to comment.