Skip to content

Commit

Permalink
Merge pull request #1 from Crocodily-3-0/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Puzanovim authored Jun 2, 2021
2 parents 07dd83f + 90894ae commit f2d50ab
Show file tree
Hide file tree
Showing 70 changed files with 3,216 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ __pycache__/
*.pyc
*.pyo
*.pyd
*.local
*.local

# database
test.db
pre-production.db
production.db
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ win_format:


up:
uvicorn $(CODE).app:app --reload
uvicorn $(CODE).app:app --host=0.0.0.0 --reload


ci: lint test
Expand Down
Empty file added src/__init__.py
Empty file.
Empty file added src/accounts/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions src/accounts/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from fastapi import APIRouter
from .client_account.routers import client_router as client_router
from .developer_account.routers import developer_router

accounts_router = APIRouter()

accounts_router.include_router(client_router, prefix='/clients', tags=['Client'])
accounts_router.include_router(developer_router, prefix='/developers', tags=['Developer'])
Empty file.
18 changes: 18 additions & 0 deletions src/accounts/client_account/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, sql

from ...db.db import Base


class Client(Base):
__tablename__ = 'client'

id = Column(Integer, primary_key=True, index=True, unique=True)
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)


clients = Client.__table__
52 changes: 52 additions & 0 deletions src/accounts/client_account/routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from fastapi import APIRouter, Depends, status, HTTPException
from pydantic.types import UUID4

from .schemas import ClientDB, Client, ClientUpdate, ClientAndOwnerCreate, ClientsPage
from .services import add_client, update_client, 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_owner_with_superuser, get_client_users, default_uuid
from ..employee_account.routers import employee_router

client_router = APIRouter()


@client_router.get("/", response_model=ClientsPage, status_code=status.HTTP_200_OK)
async def clients_list(user: UserTable = Depends(developer_user), last_id: int = 0, limit: int = 9):
return await get_clients_page(last_id, limit)


@client_router.post("/", response_model=ClientDB, status_code=status.HTTP_201_CREATED)
async def create_client(item: ClientAndOwnerCreate, user: UserTable = Depends(developer_user)):
return await add_client(item)


@client_router.get("/{id}", status_code=status.HTTP_200_OK)
async def client(id: int, user: UserTable = Depends(any_user), last_id: UUID4 = default_uuid, limit: int = 9):
if user.is_superuser:
return await get_dev_client_page(id, last_id, limit)
elif await get_client_users(id, user):
return await get_client_page(id, last_id, limit)
else:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)


@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.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(employee_router)
91 changes: 91 additions & 0 deletions src/accounts/client_account/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from datetime import datetime
from typing import Optional, List

from pydantic import BaseModel, EmailStr, validator

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


class ClientBase(BaseModel):
name: str
avatar: str


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


class ClientAndOwnerCreate(ClientCreate):
owner_name: str
surname: str
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):
if len(v) < 6:
raise ValueError('Password should be at least 6 characters')
return v


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


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


class ClientShort(ClientDB):
is_active: bool
owner: UserDB
count_employees: int = 0


class Client(ClientBase):
id: int
is_active: bool
date_create: datetime
owner: Employee
licences: List[Licence]


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


class DevClientPage(BaseModel):
client: Client
employees_list: List[EmployeeList] = []
software_list: List[SoftwareDB]


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


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

Loading

0 comments on commit f2d50ab

Please sign in to comment.