diff --git a/migrations/versions/0fbda260a023_add_user_id.py b/migrations/versions/0fbda260a023_add_user_id.py new file mode 100644 index 0000000..fda0582 --- /dev/null +++ b/migrations/versions/0fbda260a023_add_user_id.py @@ -0,0 +1,25 @@ +"""add user_id + +Revision ID: 0fbda260a023 +Revises: 5659e13277b6 +Create Date: 2024-11-08 12:49:18.796942 + +""" + +import sqlalchemy as sa +from alembic import op + + +# revision identifiers, used by Alembic. +revision = '0fbda260a023' +down_revision = '5659e13277b6' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('comment', sa.Column('user_id', sa.Integer(), nullable=True)) + + +def downgrade(): + op.drop_column('comment', 'user_id') diff --git a/rating_api/models/base.py b/rating_api/models/base.py index 678078a..8ffc3b1 100644 --- a/rating_api/models/base.py +++ b/rating_api/models/base.py @@ -2,9 +2,9 @@ import re -from sqlalchemy import Integer, not_ +from sqlalchemy import not_ from sqlalchemy.exc import NoResultFound -from sqlalchemy.orm import Mapped, Query, Session, as_declarative, declared_attr, mapped_column +from sqlalchemy.orm import Query, Session, as_declarative, declared_attr from rating_api.exceptions import ObjectNotFound diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 21ad8d5..773a3d0 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -5,7 +5,7 @@ import uuid from enum import Enum -from sqlalchemy import UUID, Boolean, DateTime +from sqlalchemy import UUID, DateTime from sqlalchemy import Enum as DbEnum from sqlalchemy import ForeignKey, Integer, String from sqlalchemy.orm import Mapped, mapped_column, relationship @@ -37,6 +37,7 @@ class Lecturer(BaseDbModel): class Comment(BaseDbModel): uuid: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4) + user_id: Mapped[int] = mapped_column(Integer, nullable=True) create_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False) update_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False) subject: Mapped[str] = mapped_column(String, nullable=False) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 678a691..c0cbcf4 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -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) diff --git a/rating_api/routes/lecturer.py b/rating_api/routes/lecturer.py index eb3b770..26422d6 100644 --- a/rating_api/routes/lecturer.py +++ b/rating_api/routes/lecturer.py @@ -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 diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index 35d5ae2..8e7408b 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -6,6 +6,7 @@ class CommentGet(Base): uuid: UUID + user_id: int create_ts: datetime.datetime update_ts: datetime.datetime subject: str