Skip to content

Commit

Permalink
update CRUD methods for desk
Browse files Browse the repository at this point in the history
  • Loading branch information
Puzanovim committed Apr 29, 2021
1 parent 673fa2b commit 0cdb9c5
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 82 deletions.
13 changes: 4 additions & 9 deletions src/accounts/client_account/services.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from fastapi import HTTPException, status, Request
from fastapi import HTTPException, status
from fastapi.responses import RedirectResponse
from fastapi_users.router import ErrorCode
from pydantic.types import UUID4
Expand All @@ -12,14 +12,9 @@
from ...errors import Errors
from ...reference_book.models import licences, softwares
from ...users.logic import all_users, get_or_404
from ...users.models import users, user_db
from ...users.models import users
from ...users.schemas import UserCreate, OwnerCreate, UserUpdate


async def check_and_convert_to_dict(result):
if result:
return dict(result)
return []
from ...service import check_dict


async def get_clients():
Expand All @@ -33,7 +28,7 @@ async def get_client(id: int):
if result:
result = dict(result)
owner_data = await database.fetch_one(query=users.select().where(users.c.id == result["owner_id"]))
owner = await check_and_convert_to_dict(owner_data)
owner = await check_dict(owner_data)
employee_data = await database.fetch_all(query=users.select().where(users.c.client_id == id))
employees = [dict(employee) for employee in employee_data]
licence_data = await database.fetch_all(query=licences.select().where(licences.c.client_id == id))
Expand Down
4 changes: 2 additions & 2 deletions src/accounts/developer_account/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from fastapi import APIRouter, Depends, status, Response
from pydantic.types import UUID4

from .services import get_developers, get_developer, add_developer, update_developer, delete_developer
from .services import get_developer, add_developer, update_developer, delete_developer
from src.users.models import UserTable
from src.users.logic import developer_user
from src.users.logic import developer_user, get_developers
from src.users.schemas import UserDB, UserCreate, UserUpdate

developer_router = APIRouter()
Expand Down
7 changes: 1 addition & 6 deletions src/accounts/developer_account/services.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from fastapi import Request, HTTPException, status
from fastapi import HTTPException, status
from fastapi_users.router import ErrorCode
from pydantic.types import UUID4

Expand All @@ -10,11 +10,6 @@
from src.users.schemas import UserCreate, DeveloperCreate, UserUpdate


async def get_developers():
developers_data = await database.fetch_all(users.select().where(users.c.is_superuser is True))
return [dict(developer) for developer in developers_data]


async def get_developer(id: UUID4):
developer = await database.fetch_one(users.select().where((users.c.is_superuser is True) & (users.c.id == id)))
if developer:
Expand Down
7 changes: 4 additions & 3 deletions src/desk/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import enum

from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, sql, Enum

from ..db.db import Base


class StatusTasks(enum.Enum):
new = "New"
register = "Registered"
registered = "Registered"
in_work = "In work"
closed = "Closed"
canceled = "Canceled"
Expand All @@ -21,10 +22,10 @@ class Appeal(Base):
text = Column(String(500), nullable=False)
client_id = Column(Integer, ForeignKey('client.id'))
author_id = Column(String, ForeignKey('user.id'))
responsible_id = Column(String, ForeignKey('user.id'), nullable=True)
status = Column(Enum(StatusTasks), default=StatusTasks.new)
date_create = Column(DateTime(timezone=True), server_default=sql.func.now())
date_processing = Column(DateTime, default=None)
status = Column(Enum(StatusTasks), default=StatusTasks.new)
responsible_id = Column(String, ForeignKey('user.id'), nullable=True)
software_id = Column(Integer, ForeignKey('software.id'), nullable=False)
module_id = Column(Integer, ForeignKey('module.id'), nullable=False)
# attachments
Expand Down
53 changes: 35 additions & 18 deletions src/desk/routes.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,74 @@
from fastapi import APIRouter, status, Depends
from fastapi import APIRouter, status, Depends, Response
from typing import List

from .services import get_appeals, get_appeal, get_comments, get_comment, \
add_appeal, add_comment, update_appeal, update_comment, delete_comment
from pydantic.types import UUID4

from .services import get_all_appeals, get_appeals, get_appeal, get_comments, get_comment, \
add_appeal, add_comment, update_appeal, update_comment, delete_comment, update_attachments
from .schemas import Appeal, CommentShort, Comment, AppealCreate, CommentCreate, CommentDB, \
AppealUpdate, AppealDB, AppealShort, CommentUpdate
AppealUpdate, AppealDB, AppealShort, CommentUpdate, DevAppeal
from ..users.models import UserTable
from ..users.logic import employee
from ..users.logic import employee, any_user, developer_user

router = APIRouter()


@router.get("/", response_model=List[AppealShort], status_code=status.HTTP_200_OK)
async def appeals_list(user: UserTable = Depends(employee)):
async def appeals_list(user: UserTable = Depends(any_user)):
if user.is_superuser:
return await get_all_appeals()
return await get_appeals(user)


@router.get("/{id}", response_model=Appeal, status_code=status.HTTP_200_OK)
@router.get("/{id}", status_code=status.HTTP_200_OK)
async def appeal(id: int, user: UserTable = Depends(employee)):
return await get_appeal(id, user)


# @router.get("/{id}", response_model=DevAppeal, status_code=status.HTTP_200_OK)
# async def appeal(id: int, user: UserTable = Depends(developer_user)):
# print("WINDOW OF DEVELOPER")
# print(user.is_superuser)
# return await get_appeal(id, user)


@router.post("/", response_model=AppealDB, status_code=status.HTTP_201_CREATED)
async def create_appeal(item: AppealCreate, user: UserTable = Depends(employee)):
return await add_appeal(item, user)


@router.put("/{id}", response_model=AppealDB, status_code=status.HTTP_201_CREATED)
async def update_appeal_by_id(id: int, item: AppealUpdate, user: UserTable = Depends(employee)):
# @router.patch("/{id}", status_code=status.HTTP_201_CREATED)
# async def update_attachments_on_appeal(id: int, item: AppealUpdate, user: UserTable = Depends(employee)):
# pass
# return await update_attachments(id, item, user)


@router.patch("/{id}", response_model=AppealDB, status_code=status.HTTP_201_CREATED)
async def update_appeal_by_id(id: int, item: AppealUpdate, user: UserTable = Depends(developer_user)):
return await update_appeal(id, item, user)


@router.get("/{id}/comments", response_model=List[CommentShort], status_code=status.HTTP_200_OK)
async def comments_list(id: int, user: UserTable = Depends(employee)):
async def comments_list(id: int, user: UserTable = Depends(any_user)):
return await get_comments(id, user)


@router.get("/{id}/comments/{pk}", response_model=Comment, status_code=status.HTTP_200_OK)
async def comment(id: int, pk: int, user: UserTable = Depends(employee)):
async def comment(id: int, pk: int, user: UserTable = Depends(any_user)):
return await get_comment(id, pk, user)


@router.post("/{id}/comments/", response_model=CommentDB, status_code=status.HTTP_201_CREATED)
async def create_comment(id: int, item: CommentCreate, user: UserTable = Depends(employee)):
async def create_comment(id: int, item: CommentCreate, user: UserTable = Depends(any_user)):
return await add_comment(id, item, user)


@router.put("/{id}/comments/{pk}", response_model=CommentDB, status_code=status.HTTP_201_CREATED)
async def update_comment_by_id(id: int, item: CommentUpdate, user: UserTable = Depends(employee)):
return await update_comment(id, item, user)
@router.patch("/{id}/comments/{pk}", response_model=CommentDB, status_code=status.HTTP_201_CREATED)
async def update_comment_by_id(id: int, pk: int, item: CommentUpdate, user: UserTable = Depends(any_user)):
return await update_comment(id, pk, item, user)


@router.delete("/{id}/comments/{pk}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_comment_by_id(id: int, user: UserTable = Depends(employee)):
return await delete_comment(id, user)
@router.delete("/{id}/comments/{pk}", response_class=Response, status_code=status.HTTP_204_NO_CONTENT)
async def delete_comment_by_id(id: int, pk: int, user: UserTable = Depends(employee)):
await delete_comment(id, pk, user)

25 changes: 18 additions & 7 deletions src/desk/schemas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Optional, List
from datetime import datetime

from .models import StatusTasks
from pydantic import BaseModel

Expand All @@ -18,23 +19,28 @@ class AppealCreate(AppealBase):
module_id: int


class AppealUpdate(AppealCreate):
pass
class AppealUpdate(AppealBase):
topic: Optional[str]
text: Optional[str]
status: Optional[StatusTasks]
software_id: Optional[int]
module_id: Optional[int]
responsible_id: Optional[str]


class AppealShort(AppealBase):
id: int
status: StatusTasks


class AppealDB(AppealCreate):
class AppealDB(AppealBase):
id: int
client_id: int
author_id: str
status: StatusTasks
date_create: datetime
date_processing: Optional[datetime]
responsible_id: Optional[int]
responsible_id: Optional[str]
software_id: int
module_id: int

Expand Down Expand Up @@ -73,12 +79,17 @@ class CommentShort(CommentBase):

class Appeal(AppealBase):
id: int
status: StatusTasks
date_create: datetime
date_processing: Optional[datetime] = None
client: ClientDB
author: UserDB
responsible: Optional[UserDB]
status: StatusTasks
date_create: datetime
date_processing: Optional[datetime]
software: SoftwareDB
module: ModuleShort
comment: Optional[List[CommentShort]]


class DevAppeal(Appeal):
developers: List[UserDB]
allowed_statuses: List[StatusTasks]
Loading

0 comments on commit 0cdb9c5

Please sign in to comment.