Skip to content

Commit ddc3bcb

Browse files
committed
Исправления
1 parent 6bcb83d commit ddc3bcb

3 files changed

Lines changed: 22 additions & 36 deletions

File tree

migrations/versions/1c001709fc55_advanced_sort.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,20 @@
55
Create Date: 2025-04-26 17:01:57.140143
66
77
"""
8-
from alembic import op
8+
99
import sqlalchemy as sa
10+
from alembic import op
1011

1112

12-
# revision identifiers, used by Alembic.
1313
revision = '1c001709fc55'
1414
down_revision = 'dd44854aa12a'
1515
branch_labels = None
1616
depends_on = None
1717

1818

1919
def upgrade():
20-
# ### commands auto generated by Alembic - please adjust! ###
21-
op.alter_column('comment', 'approved_by',
22-
existing_type=sa.INTEGER(),
23-
nullable=True)
24-
# ### end Alembic commands ###
20+
op.alter_column('comment', 'approved_by', existing_type=sa.INTEGER(), nullable=True)
2521

2622

2723
def downgrade():
28-
# ### commands auto generated by Alembic - please adjust! ###
29-
op.alter_column('comment', 'approved_by',
30-
existing_type=sa.INTEGER(),
31-
nullable=False)
32-
# ### end Alembic commands ###
24+
op.alter_column('comment', 'approved_by', existing_type=sa.INTEGER(), nullable=False)

rating_api/models/db.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,24 @@ def mark_general(self):
126126
@hybrid_method
127127
def order_by_create_ts(
128128
self, query: str, asc_order: bool
129-
) -> tuple[UnaryExpression[datetime.datetime] | InstrumentedAttribute, InstrumentedAttribute]:
130-
return getattr(Comment, query) if asc_order else desc(getattr(Comment, query)), Comment.user_id
129+
) -> UnaryExpression[datetime.datetime] | InstrumentedAttribute:
130+
return getattr(Comment, query) if asc_order else desc(getattr(Comment, query))
131131

132132
@hybrid_method
133-
def order_by_mark(
134-
self, query: str, asc_order: bool
135-
) -> tuple[UnaryExpression[float] | InstrumentedAttribute, InstrumentedAttribute]:
136-
return getattr(Comment, query) if asc_order else desc(getattr(Comment, query)), Comment.user_id
133+
def order_by_mark(self, query: str, asc_order: bool) -> UnaryExpression[float] | InstrumentedAttribute:
134+
return getattr(Comment, query) if asc_order else desc(getattr(Comment, query))
137135

138136
@hybrid_method
139137
def search_by_lectorer_id(self, query: int) -> bool:
140-
response = true
141-
if query:
142-
response = and_(Comment.review_status == ReviewStatus.APPROVED, Comment.lecturer_id == query)
143-
return response
138+
if not query:
139+
return true()
140+
return and_(Comment.review_status == ReviewStatus.APPROVED, Comment.lecturer_id == query)
144141

145142
@hybrid_method
146143
def search_by_user_id(self, query: int) -> bool:
147-
response = true
148-
if query:
149-
response = and_(Comment.user_id == query)
150-
return response
144+
if not query:
145+
return true()
146+
return Comment.user_id == query
151147

152148

153149
class LecturerUserComment(BaseDbModel):

rating_api/routes/comment.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,18 @@ async def get_comments(
207207
.filter(Comment.search_by_lectorer_id(lecturer_id))
208208
.filter(Comment.search_by_user_id(user_id))
209209
.order_by(
210-
*(
211-
Comment.order_by_create_ts(order_by, asc_order)
212-
if "mark" in order_by
213-
else Comment.order_by_mark(order_by, asc_order)
214-
)
210+
Comment.order_by_create_ts(order_by, asc_order)
211+
if "mark" in order_by
212+
else Comment.order_by_mark(order_by, asc_order)
215213
)
216214
)
217-
215+
216+
print(comments_query.statement.compile())
218217
comments = comments_query.limit(limit).offset(offset).all()
219-
218+
220219
if not comments:
221220
raise ObjectNotFound(Comment, 'all')
222-
if user and "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
221+
if "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
223222
result = CommentGetAllWithAllInfo(limit=limit, offset=offset, total=len(comments))
224223
comment_validator = CommentGetWithAllInfo
225224
elif user.get('id') == user_id:
@@ -230,7 +229,7 @@ async def get_comments(
230229
comment_validator = CommentGet
231230

232231
result.comments = comments
233-
232+
234233
if unreviewed:
235234
if not user:
236235
raise ForbiddenAction(Comment)
@@ -244,7 +243,6 @@ async def get_comments(
244243
raise ForbiddenAction(Comment)
245244
else:
246245
result.comments = [comment for comment in result.comments if comment.review_status is ReviewStatus.APPROVED]
247-
result.comments = result.comments[offset : limit + offset]
248246

249247
result.total = len(result.comments)
250248
result.comments = [comment_validator.model_validate(comment) for comment in result.comments]

0 commit comments

Comments
 (0)