From 0b02f6de2a135056f26aca1ecb03cc5560898d16 Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Fri, 8 Nov 2024 13:34:48 +0000 Subject: [PATCH 1/4] Added nullable user_id --- .../versions/0fbda260a023_add_user_id.py | 27 +++++++++++++++++++ rating_api/models/db.py | 3 ++- rating_api/schemas/models.py | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/0fbda260a023_add_user_id.py diff --git a/migrations/versions/0fbda260a023_add_user_id.py b/migrations/versions/0fbda260a023_add_user_id.py new file mode 100644 index 0000000..10fac57 --- /dev/null +++ b/migrations/versions/0fbda260a023_add_user_id.py @@ -0,0 +1,27 @@ +"""add user_id + +Revision ID: 0fbda260a023 +Revises: 5659e13277b6 +Create Date: 2024-11-08 12:49:18.796942 + +""" + +from alembic import op +import sqlalchemy as sa + + +# 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)) + op.alter_column('lecturer_user_comment', 'user_id', existing_type=sa.INTEGER(), nullable=True) + + +def downgrade(): + op.alter_column('lecturer_user_comment', 'user_id', existing_type=sa.INTEGER(), nullable=False) + op.drop_column('comment', 'user_id') diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 21ad8d5..9da0ff9 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -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) @@ -52,6 +53,6 @@ class Comment(BaseDbModel): class LecturerUserComment(BaseDbModel): id: Mapped[int] = mapped_column(Integer, primary_key=True) lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) - user_id: Mapped[int] = mapped_column(Integer, nullable=False) + 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) 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 From 4f99b7c698d0d8efc0f16df7a13e61ad54d53fb0 Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Fri, 8 Nov 2024 13:45:05 +0000 Subject: [PATCH 2/4] linting --- rating_api/models/base.py | 4 ++-- rating_api/models/db.py | 2 +- rating_api/routes/comment.py | 4 ++-- rating_api/routes/lecturer.py | 4 +++- 4 files changed, 8 insertions(+), 6 deletions(-) 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 9da0ff9..fd7084a 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 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 From f84ed25774ed71252e54355bb1e26f7b2c609d32 Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Fri, 8 Nov 2024 14:13:06 +0000 Subject: [PATCH 3/4] linting part two --- migrations/versions/0fbda260a023_add_user_id.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/versions/0fbda260a023_add_user_id.py b/migrations/versions/0fbda260a023_add_user_id.py index 10fac57..e862a48 100644 --- a/migrations/versions/0fbda260a023_add_user_id.py +++ b/migrations/versions/0fbda260a023_add_user_id.py @@ -6,8 +6,8 @@ """ -from alembic import op import sqlalchemy as sa +from alembic import op # revision identifiers, used by Alembic. From 0f84b635ac413a47824e85b9305aa2086e32cd46 Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Fri, 8 Nov 2024 14:40:14 +0000 Subject: [PATCH 4/4] reverted nullable to False --- migrations/versions/0fbda260a023_add_user_id.py | 2 -- rating_api/models/db.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/migrations/versions/0fbda260a023_add_user_id.py b/migrations/versions/0fbda260a023_add_user_id.py index e862a48..fda0582 100644 --- a/migrations/versions/0fbda260a023_add_user_id.py +++ b/migrations/versions/0fbda260a023_add_user_id.py @@ -19,9 +19,7 @@ def upgrade(): op.add_column('comment', sa.Column('user_id', sa.Integer(), nullable=True)) - op.alter_column('lecturer_user_comment', 'user_id', existing_type=sa.INTEGER(), nullable=True) def downgrade(): - op.alter_column('lecturer_user_comment', 'user_id', existing_type=sa.INTEGER(), nullable=False) op.drop_column('comment', 'user_id') diff --git a/rating_api/models/db.py b/rating_api/models/db.py index fd7084a..773a3d0 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -53,6 +53,6 @@ class Comment(BaseDbModel): class LecturerUserComment(BaseDbModel): id: Mapped[int] = mapped_column(Integer, primary_key=True) lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) - user_id: Mapped[int] = mapped_column(Integer, nullable=True) + user_id: Mapped[int] = mapped_column(Integer, nullable=False) 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)