From b01780bbe2a67d31b9670890d11ca27e84e5fc5c Mon Sep 17 00:00:00 2001 From: Kevin Glisson Date: Tue, 7 Mar 2023 11:31:25 -0800 Subject: [PATCH 1/4] Allow for casting for signal instance id --- .../dispatch_slack/case/interactive.py | 8 ++++--- src/dispatch/signal/service.py | 21 +++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/dispatch/plugins/dispatch_slack/case/interactive.py b/src/dispatch/plugins/dispatch_slack/case/interactive.py index 6913fd40f32a..1544a4b53e44 100644 --- a/src/dispatch/plugins/dispatch_slack/case/interactive.py +++ b/src/dispatch/plugins/dispatch_slack/case/interactive.py @@ -16,8 +16,8 @@ ) from slack_bolt import Ack, BoltContext, Respond from slack_sdk.web.client import WebClient -from sqlalchemy.orm import Session from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import Session from dispatch.auth.models import DispatchUser from dispatch.case import flows as case_flows @@ -71,7 +71,7 @@ from dispatch.project import service as project_service from dispatch.search.utils import create_filter_expression from dispatch.signal import service as signal_service -from dispatch.signal.models import SignalUpdate, SignalFilterCreate, SignalFilterRead +from dispatch.signal.models import SignalFilterCreate, SignalFilterRead, SignalUpdate def configure(config: SlackConversationConfiguration): @@ -1315,7 +1315,9 @@ def signal_button_click( ack: Ack, body: dict, db_session: Session, context: BoltContext, client: WebClient ): ack() - signal = signal_service.get(db_session=db_session, signal_id=context["subject"].id) + signal = signal_service.get_signal_instance( + db_session=db_session, signal_id=context["subject"].id + ) blocks = [Section(text=f"```{json.dumps(signal.raw, indent=2)}```")] diff --git a/src/dispatch/signal/service.py b/src/dispatch/signal/service.py index c890731561d1..c47ce1303893 100644 --- a/src/dispatch/signal/service.py +++ b/src/dispatch/signal/service.py @@ -1,10 +1,14 @@ import json +from sqlalchemy_utils import cast_if + from datetime import datetime, timedelta, timezone from typing import Optional, Literal from pydantic.error_wrappers import ErrorWrapper, ValidationError -from sqlalchemy import asc +from sqlalchemy import asc, String from sqlalchemy.orm import Session +from sqlalchemy.dialects.postgresql import UUID + from dispatch.auth.models import DispatchUser from dispatch.case.priority import service as case_priority_service @@ -115,7 +119,20 @@ def get_signal_filter_by_name(*, db_session, project_id: int, name: str) -> Opti def get_signal_filter(*, db_session: Session, signal_filter_id: int) -> SignalFilter: """Gets a single signal filter.""" - return db_session.query(SignalFilter).filter(SignalFilter.id == signal_filter_id).one_or_none() + return ( + db_session.query(SignalFilter) + .filter(SignalFilter.id == cast_if(signal_filter_id, UUID)) + .one_or_none() + ) + + +def get_signal_instance(*, db_session: Session, signal_instance_id: int | str): + """Gets a signal instance by it's UUID.""" + return ( + db_session.query(SignalInstance) + .filter(SignalInstance.id == cast_if(signal_instance_id, String)) + .one_or_none() + ) def get(*, db_session: Session, signal_id: int) -> Optional[Signal]: From e7257b9255b69f199ad11e18206df9627ab66392 Mon Sep 17 00:00:00 2001 From: Kevin Glisson Date: Tue, 7 Mar 2023 11:32:23 -0800 Subject: [PATCH 2/4] kwargs typo --- src/dispatch/plugins/dispatch_slack/case/interactive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dispatch/plugins/dispatch_slack/case/interactive.py b/src/dispatch/plugins/dispatch_slack/case/interactive.py index 1544a4b53e44..17206b6ad1a8 100644 --- a/src/dispatch/plugins/dispatch_slack/case/interactive.py +++ b/src/dispatch/plugins/dispatch_slack/case/interactive.py @@ -1316,7 +1316,7 @@ def signal_button_click( ): ack() signal = signal_service.get_signal_instance( - db_session=db_session, signal_id=context["subject"].id + db_session=db_session, signal_instance_id=context["subject"].id ) blocks = [Section(text=f"```{json.dumps(signal.raw, indent=2)}```")] From bc0d1d28f747596722012291476f53b365eb7f73 Mon Sep 17 00:00:00 2001 From: Kevin Glisson Date: Tue, 7 Mar 2023 11:33:31 -0800 Subject: [PATCH 3/4] Wrong cast --- src/dispatch/signal/service.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/dispatch/signal/service.py b/src/dispatch/signal/service.py index c47ce1303893..d699f2b7e126 100644 --- a/src/dispatch/signal/service.py +++ b/src/dispatch/signal/service.py @@ -119,11 +119,7 @@ def get_signal_filter_by_name(*, db_session, project_id: int, name: str) -> Opti def get_signal_filter(*, db_session: Session, signal_filter_id: int) -> SignalFilter: """Gets a single signal filter.""" - return ( - db_session.query(SignalFilter) - .filter(SignalFilter.id == cast_if(signal_filter_id, UUID)) - .one_or_none() - ) + return db_session.query(SignalFilter).filter(SignalFilter.id == signal_filter_id).one_or_none() def get_signal_instance(*, db_session: Session, signal_instance_id: int | str): From ad11dba3693d701a44776fb8670d9daeb46068ed Mon Sep 17 00:00:00 2001 From: Kevin Glisson Date: Tue, 7 Mar 2023 11:40:24 -0800 Subject: [PATCH 4/4] Fixes ruff --- src/dispatch/signal/service.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dispatch/signal/service.py b/src/dispatch/signal/service.py index d699f2b7e126..7088796b1469 100644 --- a/src/dispatch/signal/service.py +++ b/src/dispatch/signal/service.py @@ -7,7 +7,6 @@ from pydantic.error_wrappers import ErrorWrapper, ValidationError from sqlalchemy import asc, String from sqlalchemy.orm import Session -from sqlalchemy.dialects.postgresql import UUID from dispatch.auth.models import DispatchUser