From 24b95e5a6f86ed54731351558cf01823f2908802 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 18 Nov 2025 22:46:17 +0000 Subject: [PATCH] Improve code quality per PR #11 and #12 review feedback - Improve CORS config readability by breaking nested os.getenv() into explicit fallback steps (_default_cors_origin -> _cors_origins_str) - Remove duplicate import os statement - Consolidate all Celery task imports at module level - Remove inline imports from function bodies --- surfsense_backend/app/config/__init__.py | 3 ++- surfsense_backend/app/routes/documents_routes.py | 16 ++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/surfsense_backend/app/config/__init__.py b/surfsense_backend/app/config/__init__.py index 1fa9d3f33..e5dd223d4 100644 --- a/surfsense_backend/app/config/__init__.py +++ b/surfsense_backend/app/config/__init__.py @@ -138,7 +138,8 @@ class Config: # CORS Configuration # Comma-separated list of allowed origins, defaults to frontend URL # Note: Wildcard "*" is not allowed when allow_credentials=True - _cors_origins_str = os.getenv("CORS_ORIGINS", os.getenv("NEXT_FRONTEND_URL", "http://localhost:3000")) + _default_cors_origin = os.getenv("NEXT_FRONTEND_URL", "http://localhost:3000") + _cors_origins_str = os.getenv("CORS_ORIGINS", _default_cors_origin) CORS_ORIGINS = [origin.strip() for origin in _cors_origins_str.split(",") if origin.strip()] # Google OAuth diff --git a/surfsense_backend/app/routes/documents_routes.py b/surfsense_backend/app/routes/documents_routes.py index 8f5d59945..9a41621b9 100644 --- a/surfsense_backend/app/routes/documents_routes.py +++ b/surfsense_backend/app/routes/documents_routes.py @@ -26,7 +26,12 @@ from app.schemas import ( ) from app.users import current_active_user from app.utils.check_ownership import check_ownership -from app.tasks.celery_tasks.document_tasks import process_file_upload_task +from app.tasks.celery_tasks.document_tasks import ( + process_crawled_url_task, + process_extension_document_task, + process_file_upload_task, + process_youtube_video_task, +) try: asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy()) @@ -34,8 +39,6 @@ except RuntimeError as e: print("Error setting event loop policy", e) pass -import os - os.environ["UNSTRUCTURED_HAS_PATCHED_LOOP"] = "1" @@ -53,10 +56,6 @@ async def create_documents( await check_ownership(session, SearchSpace, request.search_space_id, user) if request.document_type == DocumentType.EXTENSION: - from app.tasks.celery_tasks.document_tasks import ( - process_extension_document_task, - ) - for individual_document in request.content: # Convert document to dict for Celery serialization document_dict = { @@ -70,14 +69,11 @@ async def create_documents( document_dict, request.search_space_id, str(user.id) ) elif request.document_type == DocumentType.CRAWLED_URL: - from app.tasks.celery_tasks.document_tasks import process_crawled_url_task - for url in request.content: process_crawled_url_task.delay( url, request.search_space_id, str(user.id) ) elif request.document_type == DocumentType.YOUTUBE_VIDEO: - from app.tasks.celery_tasks.document_tasks import process_youtube_video_task for url in request.content: process_youtube_video_task.delay(