From f0a6fd5e1741e4a48425ffad565c90cf893d07c2 Mon Sep 17 00:00:00 2001 From: Timur Enikeev Date: Sat, 9 Nov 2024 19:30:07 -0500 Subject: [PATCH 1/5] Fix lecturer total --- rating_api/models/db.py | 12 ++++++++++-- rating_api/routes/lecturer.py | 13 +++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 7851235..eeaaa47 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -7,7 +7,7 @@ from sqlalchemy import UUID, DateTime from sqlalchemy import Enum as DbEnum -from sqlalchemy import ForeignKey, Integer, String, and_, or_, true +from sqlalchemy import ForeignKey, Integer, String, and_, func, or_, true from sqlalchemy.ext.hybrid import hybrid_method from sqlalchemy.orm import Mapped, mapped_column, relationship @@ -36,7 +36,7 @@ class Lecturer(BaseDbModel): comments: Mapped[list[Comment]] = relationship("Comment", back_populates="lecturer") @hybrid_method - def search(self, query: str) -> bool: + def search_by_name(self, query: str) -> bool: response = true query = query.split(' ') for q in query: @@ -45,6 +45,14 @@ def search(self, query: str) -> bool: ) return response + @hybrid_method + def search_by_subject(self, query: str) -> bool: + query = query.lower() + response = true + if query != "": + response = and_(Comment.review_status == ReviewStatus.APPROVED, func.lower(Comment.subject) == query) + return response + class Comment(BaseDbModel): uuid: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4) diff --git a/rating_api/routes/lecturer.py b/rating_api/routes/lecturer.py index a5e7f79..3be74fb 100644 --- a/rating_api/routes/lecturer.py +++ b/rating_api/routes/lecturer.py @@ -101,7 +101,13 @@ async def get_lecturers( `name` Поле для ФИО. Если передано `name` - возвращает всех преподователей, для которых нашлись совпадения с переданной строкой """ - lecturers = Lecturer.query(session=db.session).filter(Lecturer.search(name)).all() + lecturers = ( + Lecturer.query(session=db.session) + .join(Lecturer.comments) + .filter(Lecturer.search_by_name(name)) + .filter(Lecturer.search_by_subject(subject)) + .all() + ) if not lecturers: raise ObjectNotFound(Lecturer, 'all') result = LecturerGetAll(limit=limit, offset=offset, total=len(lecturers)) @@ -137,11 +143,6 @@ async def get_lecturers( result.lecturers.append(lecturer_to_result) 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.total = len(result.lecturers) return result From ec4816d7ea5bdd6f161848eb07af289eb75d166f Mon Sep 17 00:00:00 2001 From: Morozov Artem <126605382+Temmmmmo@users.noreply.github.com> Date: Sun, 10 Nov 2024 03:35:11 +0300 Subject: [PATCH 2/5] Update fee34ac4fcab_delete_user_id_from_lectureruser.py --- .../versions/fee34ac4fcab_delete_user_id_from_lectureruser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/migrations/versions/fee34ac4fcab_delete_user_id_from_lectureruser.py b/migrations/versions/fee34ac4fcab_delete_user_id_from_lectureruser.py index 07960ec..5026086 100644 --- a/migrations/versions/fee34ac4fcab_delete_user_id_from_lectureruser.py +++ b/migrations/versions/fee34ac4fcab_delete_user_id_from_lectureruser.py @@ -6,8 +6,9 @@ """ -from alembic import op import sqlalchemy as sa +from alembic import op + revision = 'fee34ac4fcab' From 08cb0364ab0afd47e9e984ecb5b4c7fbee5f152b Mon Sep 17 00:00:00 2001 From: Timur Enikeev Date: Sat, 9 Nov 2024 19:40:01 -0500 Subject: [PATCH 3/5] Style fix --- .../versions/fee34ac4fcab_delete_user_id_from_lectureruser.py | 1 - 1 file changed, 1 deletion(-) diff --git a/migrations/versions/fee34ac4fcab_delete_user_id_from_lectureruser.py b/migrations/versions/fee34ac4fcab_delete_user_id_from_lectureruser.py index 5026086..5619568 100644 --- a/migrations/versions/fee34ac4fcab_delete_user_id_from_lectureruser.py +++ b/migrations/versions/fee34ac4fcab_delete_user_id_from_lectureruser.py @@ -10,7 +10,6 @@ from alembic import op - revision = 'fee34ac4fcab' down_revision = '0fbda260a023' branch_labels = None From 0de27d6072bea8f0e5cc6347162ffb299d8b82e8 Mon Sep 17 00:00:00 2001 From: Timur Enikeev Date: Sat, 9 Nov 2024 19:44:43 -0500 Subject: [PATCH 4/5] subject name fix --- rating_api/models/db.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 52a7ff2..58e3a5f 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -49,8 +49,8 @@ def search_by_name(self, query: str) -> bool: def search_by_subject(self, query: str) -> bool: query = query.lower() response = true - if query != "": - response = and_(Comment.review_status == ReviewStatus.APPROVED, func.lower(Comment.subject) == query) + if query: + response = and_(Comment.review_status == ReviewStatus.APPROVED, func.lower(Comment.subject).contains(query)) return response From 7d51b6b0fd5ab61baca4406ad3925d6f8ea5fcd4 Mon Sep 17 00:00:00 2001 From: Timur Enikeev Date: Sat, 9 Nov 2024 19:59:18 -0500 Subject: [PATCH 5/5] Fix subject search --- rating_api/routes/lecturer.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/rating_api/routes/lecturer.py b/rating_api/routes/lecturer.py index 3be74fb..99c0cab 100644 --- a/rating_api/routes/lecturer.py +++ b/rating_api/routes/lecturer.py @@ -101,13 +101,11 @@ async def get_lecturers( `name` Поле для ФИО. Если передано `name` - возвращает всех преподователей, для которых нашлись совпадения с переданной строкой """ - lecturers = ( - Lecturer.query(session=db.session) - .join(Lecturer.comments) - .filter(Lecturer.search_by_name(name)) - .filter(Lecturer.search_by_subject(subject)) - .all() - ) + lecturers_query = Lecturer.query(session=db.session) + if subject: + lecturers_query = lecturers_query.join(Lecturer.comments).filter(Lecturer.search_by_subject(subject)) + lecturers_query = lecturers_query.filter(Lecturer.search_by_name(name)) + lecturers = lecturers_query.all() if not lecturers: raise ObjectNotFound(Lecturer, 'all') result = LecturerGetAll(limit=limit, offset=offset, total=len(lecturers))