Skip to content

Commit

Permalink
Добавил вложения к обращениям
Browse files Browse the repository at this point in the history
  • Loading branch information
Puzanovim committed Jun 1, 2021
1 parent e9a3f34 commit b95b545
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 36 deletions.
12 changes: 11 additions & 1 deletion src/desk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Appeal(Base):
software_id = Column(Integer, ForeignKey('software.id'), nullable=False)
module_id = Column(Integer, ForeignKey('module.id'), nullable=False)
importance = Column(Integer, default=1)
# attachments


class Comment(Base):
Expand All @@ -43,5 +42,16 @@ class Comment(Base):
date_create = Column(DateTime(timezone=True), server_default=sql.func.now())


class Attachment(Base):
__tablename__ = 'attachment'

id = Column(Integer, primary_key=True, index=True, unique=True)
filename = Column(String(300), nullable=False)
appeal_id = Column(Integer, ForeignKey('appeal.id'), nullable=False)
author_id = Column(String, ForeignKey('user.id'), nullable=False)
date_create = Column(DateTime(timezone=True), server_default=sql.func.now())


appeals = Appeal.__table__
comments = Comment.__table__
attachments = Attachment.__table__
45 changes: 36 additions & 9 deletions src/desk/routes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from fastapi import APIRouter, status, Depends, Response, HTTPException
from fastapi import APIRouter, status, Depends, Response, HTTPException, UploadFile
from typing import List, Union

from .services import get_all_appeals, get_appeal, get_comments, get_comment, \
add_appeal, add_comment, update_appeal, update_comment, delete_comment, get_appeals_page
add_appeal, add_comment, update_appeal, update_comment, delete_comment, get_appeals_page, upload_attachment, \
delete_attachment, get_attachment, update_dev_appeal, check_access
from .schemas import Appeal, CommentShort, Comment, AppealCreate, CommentCreate, CommentDB, \
AppealUpdate, AppealDB, AppealShort, CommentUpdate, DevAppeal
AppealUpdate, AppealDB, AppealShort, CommentUpdate, DevAppeal, AttachmentDB
from ..users.models import UserTable
from ..users.logic import employee, any_user, developer_user
from ..users.logic import employee, any_user

router = APIRouter()

Expand All @@ -19,8 +20,9 @@ async def appeals_list(user: UserTable = Depends(any_user)):


@router.get("/{id}", response_model=Union[Appeal, DevAppeal], status_code=status.HTTP_200_OK)
async def appeal(appeal_id: int, user: UserTable = Depends(any_user)):
return await get_appeal(appeal_id, user)
async def appeal(id: int, user: UserTable = Depends(any_user)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
return await get_appeal(id, user)


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


@router.patch("/{id}", response_model=AppealDB, status_code=status.HTTP_201_CREATED) # TODO сделать изменение обращения пользователем
async def update_appeal_by_id(appeal_id: int, item: AppealUpdate, user: UserTable = Depends(developer_user)):
@router.patch("/{id}", response_model=AppealDB, status_code=status.HTTP_201_CREATED)
async def update_appeal_by_id(appeal_id: int, item: AppealUpdate, user: UserTable = Depends(any_user)):
if user.is_superuser:
return await update_dev_appeal(appeal_id, item, user)
return await update_appeal(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(any_user)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
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(any_user)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
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(any_user)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
return await add_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)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
return await update_comment(id, pk, item, 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)):
async def delete_comment_by_id(id: int, pk: int, user: UserTable = Depends(any_user)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
await delete_comment(id, pk, user)


@router.get("/{id}/attachments/{pk}", status_code=status.HTTP_200_OK)
async def get_attachment_by_id(id: int, pk: int, user: UserTable = Depends(any_user)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
return await get_attachment(pk)


@router.post("/{id}/attachments/", response_model=AttachmentDB, status_code=status.HTTP_201_CREATED)
async def upload_attachments(id: int, file: UploadFile, user: UserTable = Depends(any_user)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
return await upload_attachment(id, file, user)


@router.delete("/{id}/comments/{pk}", response_class=Response, status_code=status.HTTP_204_NO_CONTENT)
async def delete_attachment_by_id(id: int, pk: int, user: UserTable = Depends(any_user)):
await check_access(id, user, status.HTTP_403_FORBIDDEN)
await delete_attachment(pk)

17 changes: 17 additions & 0 deletions src/desk/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ class CommentShort(CommentBase):
date_create: datetime


class AttachmentBase(BaseModel):
filename: str


class AttachmentCreate(AttachmentBase):
appeal_id: int
author_id: str
date_create: datetime = datetime.utcnow()


class AttachmentDB(AttachmentBase):
id: int
appeal_id: int
author_id: str
date_create: datetime


class AppealBase(BaseModel):
topic: str
importance: int
Expand Down
Loading

0 comments on commit b95b545

Please sign in to comment.