From dbf4ae86da26c2e68ea24b8320964af9830b0df5 Mon Sep 17 00:00:00 2001 From: aloftus23 Date: Tue, 10 Dec 2024 09:18:05 -0500 Subject: [PATCH] Add django.setup to scheduler lambda --- backend/src/xfd_django/xfd_api/api_methods/scan.py | 5 ++++- backend/src/xfd_django/xfd_api/api_methods/search.py | 2 +- backend/src/xfd_django/xfd_api/tasks/scheduler.py | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/src/xfd_django/xfd_api/api_methods/scan.py b/backend/src/xfd_django/xfd_api/api_methods/scan.py index 08e86b43..63e9cbc6 100644 --- a/backend/src/xfd_django/xfd_api/api_methods/scan.py +++ b/backend/src/xfd_django/xfd_api/api_methods/scan.py @@ -295,7 +295,10 @@ def run_scan(scan_id: str, current_user): scan.manualRunPending = True scan.save() - return {"status": "success", "message": f"Scan {scan_id} set to manualRunPending."} + return { + "status": "success", + "message": f"Scan {scan_id} set to manualRunPending.", + } except HTTPException as http_exc: raise http_exc diff --git a/backend/src/xfd_django/xfd_api/api_methods/search.py b/backend/src/xfd_django/xfd_api/api_methods/search.py index 882b26c3..b86faee5 100644 --- a/backend/src/xfd_django/xfd_api/api_methods/search.py +++ b/backend/src/xfd_django/xfd_api/api_methods/search.py @@ -83,7 +83,7 @@ def search(search_body: SearchBody, event) -> Dict[str, Any]: def search_post(request_input): """Handle Elastic Search request""" - + es_query = build_elasticsearch_query(request_input) # Perform search in Elasticsearch TODO: Confirm index name and format diff --git a/backend/src/xfd_django/xfd_api/tasks/scheduler.py b/backend/src/xfd_django/xfd_api/tasks/scheduler.py index 96130224..a12577dd 100644 --- a/backend/src/xfd_django/xfd_api/tasks/scheduler.py +++ b/backend/src/xfd_django/xfd_api/tasks/scheduler.py @@ -6,6 +6,7 @@ import os # Third-Party Libraries +import django from django.utils import timezone from ..helpers.getScanOrganizations import get_scan_organizations @@ -13,6 +14,13 @@ from ..schema_models.scan import SCAN_SCHEMA from .ecs_client import ECSClient +# Set the Django settings module +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xfd_django.settings") +os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" + +# Initialize Django +django.setup() + def chunk(iterable, size): """Chunk a list into a nested list."""