Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rating_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ def __init__(self, dtime: datetime.timedelta):
class ForbiddenAction(RatingAPIError):
def __init__(self, type: Type):
super().__init__(f"Forbidden action with {type.__name__}", f"Запрещенное действие с объектом {type.__name__}")


class WrongMark(RatingAPIError):
def __init__(self):
super().__init__(
f"Ratings can only take values: -2, -1, 0, 1, 2", f"Оценки могут принимать только значения: -2, -1, 0, 1, 2"
)
4 changes: 2 additions & 2 deletions rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ async def get_comments(
raise ForbiddenAction(Comment)
else:
result.comments = [comment for comment in result.comments if comment.review_status is ReviewStatus.APPROVED]

result.comments = result.comments[offset : limit + offset]

if "create_ts" in order_by:
result.comments.sort(key=lambda comment: comment.create_ts)
result.total = len(result.comments)
Expand Down
9 changes: 8 additions & 1 deletion rating_api/routes/exc_handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import starlette.requests
from starlette.responses import JSONResponse

from rating_api.exceptions import AlreadyExists, ForbiddenAction, ObjectNotFound, TooManyCommentRequests
from rating_api.exceptions import AlreadyExists, ForbiddenAction, ObjectNotFound, TooManyCommentRequests, WrongMark
from rating_api.schemas.base import StatusResponseModel

from .base import app
Expand Down Expand Up @@ -33,3 +33,10 @@ async def forbidden_action_handler(req: starlette.requests.Request, exc: Already
return JSONResponse(
content=StatusResponseModel(status="Error", message=exc.eng, ru=exc.ru).model_dump(), status_code=403
)


@app.exception_handler(WrongMark)
async def wrong_mark_handler(req: starlette.requests.Request, exc: WrongMark):
return JSONResponse(
content=StatusResponseModel(status="Error", message=exc.eng, ru=exc.ru).model_dump(), status_code=400
)
4 changes: 3 additions & 1 deletion rating_api/routes/lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ async def get_lecturers(
if "general" in order_by:
result.lecturers.sort(key=lambda item: (item.mark_general is None, item.mark_general))
if subject:
result.lecturers = [lecturer for lecturer in result.lecturers if lecturer.subjects and subject in lecturer.subjects]
result.lecturers = [
lecturer for lecturer in result.lecturers if lecturer.subjects and subject in lecturer.subjects
]
result.total = len(result.lecturers)
return result

Expand Down
10 changes: 10 additions & 0 deletions rating_api/schemas/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
from uuid import UUID

from pydantic import field_validator

from rating_api.exceptions import WrongMark
from rating_api.schemas.base import Base


Expand All @@ -23,6 +26,13 @@ class CommentPost(Base):
mark_freebie: int
mark_clarity: int

@field_validator('mark_kindness', 'mark_freebie', 'mark_clarity')
@classmethod
def validate_mark(cls, value):
if value not in [-2, -1, 0, 1, 2]:
raise WrongMark()
return value


class CommentGetAll(Base):
comments: list[CommentGet] = []
Expand Down
20 changes: 20 additions & 0 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ def test_create_comment(client, dbsession):
assert post_response.status_code == status.HTTP_404_NOT_FOUND


def test_post_bad_mark(client, dbsession):
body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0}
lecturer: Lecturer = Lecturer(**body)
dbsession.add(lecturer)
dbsession.commit()

body = {
"subject": "Физика",
"text": "Хороший препод",
"mark_kindness": 4,
"mark_freebie": -2,
"mark_clarity": 0,
}
params = {"lecturer_id": lecturer.id}
post_response = client.post(url, json=body, params=params)
assert post_response.status_code == status.HTTP_400_BAD_REQUEST
dbsession.delete(lecturer)
dbsession.commit()


def test_get_comment(client, dbsession):
body = {
"first_name": 'Иван',
Expand Down