Skip to content

Commit 533a554

Browse files
committed
Изменения в get_comment и get_comments
1 parent 0388ac0 commit 533a554

4 files changed

Lines changed: 34 additions & 74 deletions

File tree

rating_api/routes/comment.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
CommentGet,
2323
CommentGetAll,
2424
CommentGetAllWithAllInfo,
25-
CommentGetAllWithLike,
2625
CommentGetAllWithStatus,
2726
CommentGetWithAllInfo,
28-
CommentGetWithLike,
2927
CommentGetWithStatus,
3028
CommentImportAll,
3129
CommentPost,
@@ -164,25 +162,22 @@ async def import_comments(
164162
return result
165163

166164

167-
@comment.get("/{uuid}", response_model=CommentGetWithLike)
168-
async def get_comment(uuid: UUID, user=Depends(UnionAuth())) -> CommentGetWithLike:
165+
@comment.get("/{uuid}", response_model=CommentGet)
166+
async def get_comment(uuid: UUID, user=Depends(UnionAuth(auto_error=False, allow_none=False))) -> CommentGet:
169167
"""
170168
Возвращает комментарий по его UUID в базе данных RatingAPI
171169
"""
172170
comment: Comment = Comment.query(session=db.session).filter(Comment.uuid == uuid).one_or_none()
173171
if comment is None:
174172
raise ObjectNotFound(Comment, uuid)
175173
base_data = CommentGet.model_validate(comment)
176-
return CommentGetWithLike(
177-
**base_data.model_dump(),
178-
is_liked=comment.has_reaction(user.get("id"), Reaction.LIKE),
179-
is_disliked=comment.has_reaction(user.get("id"), Reaction.DISLIKE),
180-
)
174+
if user:
175+
base_data.is_liked=comment.has_reaction(user.get("id"), Reaction.LIKE)
176+
base_data.is_disliked=comment.has_reaction(user.get("id"), Reaction.DISLIKE)
177+
return base_data
181178

182179

183-
@comment.get(
184-
"", response_model=Union[CommentGetAll, CommentGetAllWithLike, CommentGetAllWithAllInfo, CommentGetAllWithStatus]
185-
)
180+
@comment.get("", response_model=Union[CommentGetAll, CommentGetAllWithAllInfo, CommentGetAllWithStatus])
186181
async def get_comments(
187182
limit: int = 10,
188183
offset: int = 0,
@@ -196,7 +191,7 @@ async def get_comments(
196191
unreviewed: bool = False,
197192
asc_order: bool = False,
198193
user=Depends(UnionAuth(scopes=["rating.comment.review"], auto_error=False, allow_none=False)),
199-
) -> Union[CommentGetAll, CommentGetAllWithLike, CommentGetAllWithAllInfo, CommentGetAllWithStatus]:
194+
) -> Union[CommentGetAll, CommentGetAllWithAllInfo, CommentGetAllWithStatus]:
200195
"""
201196
Scopes: `["rating.comment.review"]`
202197
@@ -219,7 +214,6 @@ async def get_comments(
219214
220215
`asc_order` -Если передано true, сортировать в порядке возрастания. Иначе - в порядке убывания
221216
"""
222-
user = {"id": 101, "session_scopes": []} # тестовый user_id # пустые скоупы = обычный пользователь
223217
comments_query = (
224218
Comment.query(session=db.session)
225219
.filter(Comment.search_by_lectorer_id(lecturer_id))
@@ -245,11 +239,7 @@ async def get_comments(
245239
result = CommentGetAllWithStatus(limit=limit, offset=offset, total=len(comments))
246240
comment_validator = CommentGetWithStatus
247241
else:
248-
result = (
249-
CommentGetAllWithLike(limit=limit, offset=offset, total=len(comments))
250-
if user
251-
else CommentGetAll(limit=limit, offset=offset, total=len(comments))
252-
)
242+
result = CommentGetAll(limit=limit, offset=offset, total=len(comments))
253243
comment_validator = CommentGet
254244

255245
result.comments = comments
@@ -267,22 +257,19 @@ async def get_comments(
267257
result.total = len(result.comments)
268258
comments_with_like = []
269259
current_user_id = user.get("id") if user else None
260+
270261
if current_user_id and result.comments:
271262
user_reactions = Comment.reactions_for_comments(current_user_id, db.session, result.comments)
272263
else:
273264
user_reactions = {}
274265

275266
for comment in result.comments:
276267
base_data = comment_validator.model_validate(comment)
277-
278268
if current_user_id:
279269
reaction = user_reactions.get(comment.uuid)
280-
comment_with_reactions = CommentGetWithLike(
281-
**base_data.model_dump(), is_liked=reaction == Reaction.LIKE, is_disliked=reaction == Reaction.DISLIKE
282-
)
283-
comments_with_like.append(comment_with_reactions)
284-
else:
285-
comments_with_like.append(base_data)
270+
base_data.is_liked = (reaction == Reaction.LIKE)
271+
base_data.is_disliked = (reaction == Reaction.DISLIKE)
272+
comments_with_like.append(base_data)
286273

287274
result.comments = comments_with_like
288275
return result

rating_api/schemas/models.py

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,19 @@ class CommentGet(Base):
2626
like_count: int
2727
dislike_count: int
2828
user_fullname: str | None = None
29-
30-
31-
class CommentGetWithLike(CommentGet):
32-
is_liked: bool
33-
is_disliked: bool
29+
is_liked: bool = False
30+
is_disliked: bool = False
3431

3532

3633
class CommentGetWithStatus(CommentGet):
3734
review_status: ReviewStatus
3835

3936

40-
"""
41-
class CommentGetWithLikeAndStatus(CommentGetWithLike):
42-
review_status: ReviewStatus
43-
"""
44-
45-
4637
class CommentGetWithAllInfo(CommentGet):
4738
review_status: ReviewStatus
4839
approved_by: int | None = None
4940

5041

51-
"""
52-
class CommentGetWithAllInfoAndLike(CommentGetWithLike):
53-
review_status: ReviewStatus
54-
approved_by: int | None = None
55-
"""
56-
57-
5842
class CommentUpdate(Base):
5943
subject: str = None
6044
text: str = None
@@ -93,45 +77,20 @@ class CommentGetAll(Base):
9377
total: int
9478

9579

96-
class CommentGetAllWithLike(Base):
97-
comments: list[CommentGetWithLike] = []
98-
limit: int
99-
offset: int
100-
total: int
101-
102-
10380
class CommentGetAllWithStatus(Base):
10481
comments: list[CommentGetWithStatus] = []
10582
limit: int
10683
offset: int
10784
total: int
10885

10986

110-
"""
111-
class CommentGetAllWithStatusAndLike(Base):
112-
comments: list[CommentGetWithLikeAndStatus] = []
113-
limit: int
114-
offset: int
115-
total: int
116-
"""
117-
118-
11987
class CommentGetAllWithAllInfo(Base):
12088
comments: list[CommentGetWithAllInfo] = []
12189
limit: int
12290
offset: int
12391
total: int
12492

12593

126-
"""
127-
class CommentGetAllWithAllInfoAndLike(Base):
128-
comments: list[CommentGetWithAllInfoAndLike] = []
129-
limit: int
130-
offset: int
131-
total: int
132-
"""
133-
134-
13594
class LecturerUserCommentPost(Base):
13695
lecturer_id: int
13796
user_id: int

tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ def comment(dbsession, lecturer):
139139
dbsession.commit()
140140

141141

142+
@pytest.fixture
143+
def comment_reaction(dbsession, comment):
144+
created_reactions = []
145+
146+
def _create_reaction(user_id: int, react: Reaction):
147+
reaction = CommentReaction(user_id=user_id, comment_uuid=comment.uuid, reaction=react)
148+
dbsession.add(reaction)
149+
dbsession.commit()
150+
created_reactions.append(reaction)
151+
152+
yield _create_reaction
153+
154+
for reaction in created_reactions:
155+
dbsession.delete(reaction)
156+
dbsession.commit()
157+
158+
142159
@pytest.fixture
143160
def unreviewed_comment(dbsession, lecturer):
144161
_comment = Comment(

tests/test_routes/test_comment.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,12 @@ def test_create_comment(client, dbsession, lecturers, body, lecturer_n, response
210210
(None, None, None), # anonymous
211211
],
212212
)
213-
def test_get_comment_with_reaction(client, dbsession, comment, reaction_data, expected_reaction, comment_user_id):
213+
def test_get_comment_with_reaction(client, comment, reaction_data, expected_reaction, comment_user_id, comment_reaction):
214214
comment.user_id = comment_user_id
215215

216216
if reaction_data:
217217
user_id, reaction_type = reaction_data
218-
reaction = CommentReaction(user_id=user_id, comment_uuid=comment.uuid, reaction=reaction_type)
219-
dbsession.add(reaction)
220-
221-
dbsession.commit()
218+
comment_reaction(user_id, reaction_type)
222219

223220
response_comment = client.get(f'{url}/{comment.uuid}')
224221

0 commit comments

Comments
 (0)