Skip to content

Commit

Permalink
added create_ts and update_ts to create_comment
Browse files Browse the repository at this point in the history
  • Loading branch information
zipperman1 committed Nov 15, 2024
1 parent c81b676 commit 8c1a8ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
33 changes: 24 additions & 9 deletions rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,40 @@
@comment.post("", response_model=CommentGet)
async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depends(UnionAuth())) -> CommentGet:
"""
Scopes: `["rating.comment.review"]`
Создает комментарий к преподавателю в базе данных RatingAPI
Для создания комментария нужно быть авторизованным
"""
lecturer = Lecturer.get(session=db.session, id=lecturer_id)
if not lecturer:
raise ObjectNotFound(Lecturer, lecturer_id)

if comment_info.create_ts or comment_info.update_ts:
if not "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
raise ForbiddenAction(Comment)
else:
if not comment_info.create_ts:
comment_info.create_ts = datetime.datetime.utcnow()
if not comment_info.update_ts:
comment_info.update_ts = datetime.datetime.utcnow()
else:
comment_info.create_ts = datetime.datetime.utcnow()
comment_info.update_ts = datetime.datetime.utcnow()

user_comments: list[LecturerUserComment] = (
LecturerUserComment.query(session=db.session).filter(LecturerUserComment.user_id == user.get("id")).all()
)
for user_comment in user_comments:
if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta(
minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES
):
raise TooManyCommentRequests(
dtime=user_comment.update_ts
+ datetime.timedelta(minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES)
- datetime.datetime.utcnow()
)

if not "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
for user_comment in user_comments:
if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta(
minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES
):
raise TooManyCommentRequests(
dtime=user_comment.update_ts
+ datetime.timedelta(minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES)
- datetime.datetime.utcnow()
)

LecturerUserComment.create(session=db.session, lecturer_id=lecturer_id, user_id=user.get('id'))
new_comment = Comment.create(
Expand Down
2 changes: 2 additions & 0 deletions rating_api/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class CommentGet(Base):
class CommentPost(Base):
subject: str
text: str
create_ts: datetime.datetime | None = None
update_ts: datetime.datetime | None = None
mark_kindness: int
mark_freebie: int
mark_clarity: int
Expand Down

0 comments on commit 8c1a8ba

Please sign in to comment.