From 2ee37f48ba78c992c6ada1064d1558e952e67a1d Mon Sep 17 00:00:00 2001 From: m-o-d-e-r Date: Sun, 30 Jul 2023 00:23:10 +0300 Subject: [PATCH] added the ability to filter tickets by 'scope' and 'queues' --- burrito/apps/admin/views.py | 4 +++- burrito/apps/anon/views.py | 4 +++- burrito/apps/tickets/views.py | 21 +++++++++++++-------- burrito/schemas/filters_schema.py | 3 ++- burrito/utils/query_util.py | 11 +++++++++-- tests/admin_tests.py | 8 +++++--- 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/burrito/apps/admin/views.py b/burrito/apps/admin/views.py index 79f1be5f..4f0c6dee 100644 --- a/burrito/apps/admin/views.py +++ b/burrito/apps/admin/views.py @@ -20,6 +20,7 @@ q_is_valid_faculty, q_is_valid_queue, q_is_valid_status_list, + q_scope_is, q_is_hidden, ) from burrito.utils.users_util import get_user_by_id @@ -157,8 +158,9 @@ async def admin__get_ticket_list_by_filter( "hidden": q_is_hidden(filters.hidden), "anonymous": q_is_anonymous(filters.anonymous), "faculty": q_is_valid_faculty(filters.faculty) if filters.faculty else None, + "status": q_is_valid_status_list(filters.status), + "scope": q_scope_is(filters.scope) if filters.scope else None, "queue": q_is_valid_queue(filters.queue) if filters.queue else None, - "status": q_is_valid_status_list(filters.status) } final_filters = select_filters(available_filters, filters) diff --git a/burrito/apps/anon/views.py b/burrito/apps/anon/views.py index ca35afcd..5601588d 100644 --- a/burrito/apps/anon/views.py +++ b/burrito/apps/anon/views.py @@ -19,6 +19,7 @@ q_is_anonymous, q_is_valid_faculty, q_is_valid_queue, + q_scope_is, q_is_valid_status_list, q_not_hidden, q_protected_statuses @@ -35,8 +36,9 @@ async def anon__get_ticket_list_by_filter(filters: AnonTicketListRequestSchema): available_filters = { "anonymous": q_is_anonymous(filters.anonymous), "faculty": q_is_valid_faculty(filters.faculty) if filters.faculty else None, + "status": q_is_valid_status_list(filters.status), + "scope": q_scope_is(filters.scope) if filters.scope else None, "queue": q_is_valid_queue(filters.queue) if filters.queue else None, - "status": q_is_valid_status_list(filters.status) } final_filters = select_filters(available_filters, filters) + [ q_not_hidden(), diff --git a/burrito/apps/tickets/views.py b/burrito/apps/tickets/views.py index 06ebb964..ce259abd 100644 --- a/burrito/apps/tickets/views.py +++ b/burrito/apps/tickets/views.py @@ -28,6 +28,7 @@ q_is_anonymous, q_is_valid_faculty, q_is_valid_queue, + q_scope_is, q_is_valid_status_list, q_protected_statuses, q_not_hidden, @@ -349,8 +350,9 @@ async def tickets__show_tickets_list_by_filter( "hidden": q_is_hidden(filters.hidden), "anonymous": q_is_anonymous(filters.anonymous), "faculty": q_is_valid_faculty(filters.faculty) if filters.faculty else None, - "queue": q_is_valid_queue(filters.queue) if filters.queue else None, - "status": q_is_valid_status_list(filters.status) + "status": q_is_valid_status_list(filters.status), + "scope": q_scope_is(filters.scope) if filters.scope else None, + "queue": q_is_valid_queue(filters.queue) if filters.queue else None } final_filters = select_filters(available_filters, filters) + ( [ @@ -528,8 +530,9 @@ async def tickets__get_liked_tickets( "hidden": q_is_hidden(_filters.hidden), "anonymous": q_is_anonymous(_filters.anonymous), "faculty": q_is_valid_faculty(_filters.faculty) if _filters.faculty else None, - "queue": q_is_valid_queue(_filters.queue) if _filters.queue else None, - "status": q_is_valid_status_list(_filters.status) + "status": q_is_valid_status_list(_filters.status), + "scope": q_scope_is(_filters.scope) if _filters.scope else None, + "queue": q_is_valid_queue(_filters.queue) if _filters.queue else None } final_filters = select_filters(available_filters, _filters) + [ q_not_deleted(token_payload.user_id), @@ -595,9 +598,10 @@ async def tickets__get_bookmarked_tickets( "hidden": q_is_hidden(_filters.hidden), "anonymous": q_is_anonymous(_filters.anonymous), "faculty": q_is_valid_faculty(_filters.faculty) if _filters.faculty else None, - "queue": q_is_valid_queue(_filters.queue) if _filters.queue else None, "status": q_is_valid_status_list(_filters.status), - "bookmarks_type": q_bookmarked(token_payload.user_id, _filters.bookmarks_type) + "bookmarks_type": q_bookmarked(token_payload.user_id, _filters.bookmarks_type), + "scope": q_scope_is(_filters.scope) if _filters.scope else None, + "queue": q_is_valid_queue(_filters.queue) if _filters.queue else None } final_filters = select_filters(available_filters, _filters) + [ q_not_deleted(token_payload.user_id) @@ -659,8 +663,9 @@ async def tickets__get_deleted_tickets( "hidden": q_is_hidden(_filters.hidden), "anonymous": q_is_anonymous(_filters.anonymous), "faculty": q_is_valid_faculty(_filters.faculty) if _filters.faculty else None, - "queue": q_is_valid_queue(_filters.queue) if _filters.queue else None, - "status": q_is_valid_status_list(_filters.status) + "status": q_is_valid_status_list(_filters.status), + "scope": q_scope_is(_filters.scope) if _filters.scope else None, + "queue": q_is_valid_queue(_filters.queue) if _filters.queue else None } final_filters = select_filters(available_filters, _filters) + [ q_deleted(token_payload.user_id) diff --git a/burrito/schemas/filters_schema.py b/burrito/schemas/filters_schema.py index cf3df4ac..52ef7220 100644 --- a/burrito/schemas/filters_schema.py +++ b/burrito/schemas/filters_schema.py @@ -4,5 +4,6 @@ class BaseFilterSchema(BurritoPagination): anonymous: bool | None faculty: int | None - queue: int | None status: list[int] | None + scope: str | None + queue: list[int] | None diff --git a/burrito/utils/query_util.py b/burrito/utils/query_util.py index 30ed0a32..4ae5ff99 100644 --- a/burrito/utils/query_util.py +++ b/burrito/utils/query_util.py @@ -5,6 +5,7 @@ from burrito.models.statuses_model import Statuses from burrito.models.tickets_model import Tickets from burrito.models.deleted_model import Deleted +from burrito.models.queues_model import Queues from burrito.models.liked_model import Liked from burrito.utils.converter import ( @@ -58,8 +59,14 @@ def q_is_valid_faculty(value: int) -> Expression: return Tickets.faculty == FacultyConverter.convert(value) -def q_is_valid_queue(queue: int) -> Expression: - return Tickets.queue == QueueConverter.convert(queue) +def q_scope_is(scope: str) -> Expression: + return Tickets.queue.in_( + Queues.select(Queues.queue_id).where(Queues.scope == scope) + ) + + +def q_is_valid_queue(queues: list[int]) -> Expression: + return Tickets.queue.in_(queues) def q_is_valid_status(value: int) -> Expression: diff --git a/tests/admin_tests.py b/tests/admin_tests.py index a6445369..d00a2b92 100644 --- a/tests/admin_tests.py +++ b/tests/admin_tests.py @@ -44,9 +44,11 @@ def test_002_admin_ticket_list_view(self): "Authorization": f"Bearer {AuthTestCase.access_token}" }, json={ - "status": [1], -# "hidden": True, -# "anonymous": True + "scope": 'Reports', + "queue": [2], + "status": [6], + "hidden": True, + "anonymous": True }, timeout=TIMEOUT )