From 452588dded75f87cc2d758e01dd404119e96fb4a Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 25 Sep 2025 18:32:22 +0800 Subject: [PATCH] refactor(api): fix pyright check - Replaced `is_editor` checks with `has_edit_permission` in `workflow_trigger.py` and `workflow.py` to enhance clarity and consistency in permission handling. - Updated the rate limiter to use `datetime.now(UTC)` instead of `datetime.utcnow()` for accurate time handling. - Added `__all__` declaration in `trigger/__init__.py` for better module export management. - Initialized `debug_dispatched` variable in `trigger_processing_tasks.py` to ensure proper tracking during workflow dispatching. These changes improve code readability and maintainability while ensuring correct permission checks and time management. --- api/controllers/console/app/workflow.py | 4 ++-- api/controllers/console/app/workflow_trigger.py | 2 +- api/controllers/trigger/__init__.py | 5 +++++ api/services/workflow/rate_limiter.py | 4 ++-- api/tasks/trigger_processing_tasks.py | 1 + 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/api/controllers/console/app/workflow.py b/api/controllers/console/app/workflow.py index a16e7db664..5bca33f498 100644 --- a/api/controllers/console/app/workflow.py +++ b/api/controllers/console/app/workflow.py @@ -1041,7 +1041,7 @@ class DraftWorkflowTriggerNodeApi(Resource): """ Poll for trigger events and execute single node when event arrives """ - if not isinstance(current_user, Account) or not current_user.is_editor: + if not isinstance(current_user, Account) or not current_user.has_edit_permission: raise Forbidden() parser = reqparse.RequestParser() @@ -1119,7 +1119,7 @@ class DraftWorkflowTriggerRunApi(Resource): """ Poll for trigger events and execute full workflow when event arrives """ - if not isinstance(current_user, Account) or not current_user.is_editor: + if not isinstance(current_user, Account) or not current_user.has_edit_permission: raise Forbidden() parser = reqparse.RequestParser() diff --git a/api/controllers/console/app/workflow_trigger.py b/api/controllers/console/app/workflow_trigger.py index 04292f406a..5fe7d02bf4 100644 --- a/api/controllers/console/app/workflow_trigger.py +++ b/api/controllers/console/app/workflow_trigger.py @@ -110,7 +110,7 @@ class AppTriggerEnableApi(Resource): assert isinstance(current_user, Account) assert current_user.current_tenant_id is not None - if not current_user.is_editor: + if not current_user.has_edit_permission: raise Forbidden() trigger_id = args["trigger_id"] diff --git a/api/controllers/trigger/__init__.py b/api/controllers/trigger/__init__.py index 972f28649c..4f584dc4f6 100644 --- a/api/controllers/trigger/__init__.py +++ b/api/controllers/trigger/__init__.py @@ -5,3 +5,8 @@ bp = Blueprint("trigger", __name__, url_prefix="/triggers") # Import routes after blueprint creation to avoid circular imports from . import trigger, webhook + +__all__ = [ + "trigger", + "webhook", +] diff --git a/api/services/workflow/rate_limiter.py b/api/services/workflow/rate_limiter.py index dff284538a..1e26f40a6a 100644 --- a/api/services/workflow/rate_limiter.py +++ b/api/services/workflow/rate_limiter.py @@ -4,7 +4,7 @@ Day-based rate limiter for workflow executions. Implements UTC-based daily quotas that reset at midnight UTC for consistent rate limiting. """ -from datetime import datetime, time, timedelta +from datetime import UTC, datetime, time, timedelta from typing import Union import pytz @@ -173,7 +173,7 @@ class TenantDailyRateLimiter: Datetime when quota resets (next UTC midnight in tenant's timezone) """ tz = pytz.timezone(timezone_str) - utc_now = datetime.utcnow() + utc_now = datetime.now(UTC) # Get next midnight in UTC, then convert to tenant's timezone next_utc_midnight = datetime.combine(utc_now.date() + timedelta(days=1), time.min) diff --git a/api/tasks/trigger_processing_tasks.py b/api/tasks/trigger_processing_tasks.py index 7548db37bd..07ee86ac76 100644 --- a/api/tasks/trigger_processing_tasks.py +++ b/api/tasks/trigger_processing_tasks.py @@ -112,6 +112,7 @@ def dispatch_triggered_workflows_async( continue # Dispatch to debug sessions after processing all triggers + debug_dispatched = 0 try: debug_dispatched = TriggerDebugService.dispatch_debug_event( tenant_id=subscription.tenant_id,