|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import os |
3 | 4 | import sys |
4 | 5 | from dataclasses import dataclass |
5 | 6 | from enum import Enum |
@@ -42,6 +43,23 @@ def get_python_environment_info(): |
42 | 43 | ) |
43 | 44 |
|
44 | 45 |
|
| 46 | +def initialize_django() -> tuple[bool, str | None]: |
| 47 | + import django |
| 48 | + from django.apps import apps |
| 49 | + |
| 50 | + try: |
| 51 | + if not os.environ.get("DJANGO_SETTINGS_MODULE"): |
| 52 | + return False, None |
| 53 | + |
| 54 | + if not apps.ready: |
| 55 | + django.setup() |
| 56 | + |
| 57 | + return True, None |
| 58 | + |
| 59 | + except Exception as e: |
| 60 | + return False, str(e) |
| 61 | + |
| 62 | + |
45 | 63 | @dataclass |
46 | 64 | class TemplateTagQueryData: |
47 | 65 | templatetags: list[TemplateTag] |
@@ -90,78 +108,4 @@ def get_installed_templatetags() -> TemplateTagQueryData: |
90 | 108 | return TemplateTagQueryData(templatetags=templatetags) |
91 | 109 |
|
92 | 110 |
|
93 | | -def initialize_django() -> tuple[bool, str | None]: |
94 | | - """Initialize Django and return (success, error_message).""" |
95 | | - import os |
96 | | - import django |
97 | | - from django.apps import apps |
98 | | - |
99 | | - try: |
100 | | - # Check if Django settings are configured |
101 | | - if not os.environ.get("DJANGO_SETTINGS_MODULE"): |
102 | | - # Try to find and set settings module |
103 | | - import sys |
104 | | - from pathlib import Path |
105 | | - |
106 | | - # Look for manage.py to determine project structure |
107 | | - current_path = Path.cwd() |
108 | | - manage_py = None |
109 | | - |
110 | | - # Search up to 3 levels for manage.py |
111 | | - for _ in range(3): |
112 | | - if (current_path / "manage.py").exists(): |
113 | | - manage_py = current_path / "manage.py" |
114 | | - break |
115 | | - if current_path.parent == current_path: |
116 | | - break |
117 | | - current_path = current_path.parent |
118 | | - |
119 | | - if not manage_py: |
120 | | - return ( |
121 | | - False, |
122 | | - "Could not find manage.py or DJANGO_SETTINGS_MODULE not set", |
123 | | - ) |
124 | | - |
125 | | - # Add project directory to sys.path |
126 | | - project_dir = manage_py.parent |
127 | | - if str(project_dir) not in sys.path: |
128 | | - sys.path.insert(0, str(project_dir)) |
129 | | - |
130 | | - # Try to find settings module - look for common patterns |
131 | | - # First check if there's a directory with the same name as the parent |
132 | | - project_name = project_dir.name |
133 | | - settings_candidates = [ |
134 | | - f"{project_name}.settings", # e.g., myproject.settings |
135 | | - "settings", # Just settings.py in root |
136 | | - "config.settings", # Common pattern |
137 | | - "project.settings", # Another common pattern |
138 | | - ] |
139 | | - |
140 | | - # Also check for any directory containing settings.py |
141 | | - for item in project_dir.iterdir(): |
142 | | - if item.is_dir() and (item / "settings.py").exists(): |
143 | | - candidate = f"{item.name}.settings" |
144 | | - if candidate not in settings_candidates: |
145 | | - settings_candidates.insert( |
146 | | - 0, candidate |
147 | | - ) # Prioritize found settings |
148 | | - |
149 | | - for settings_candidate in settings_candidates: |
150 | | - try: |
151 | | - __import__(settings_candidate) |
152 | | - os.environ["DJANGO_SETTINGS_MODULE"] = settings_candidate |
153 | | - break |
154 | | - except ImportError: |
155 | | - continue |
156 | | - |
157 | | - # Set up Django |
158 | | - if not apps.ready: |
159 | | - django.setup() |
160 | | - |
161 | | - return True, None |
162 | | - |
163 | | - except Exception as e: |
164 | | - return False, str(e) |
165 | | - |
166 | | - |
167 | 111 | QueryData = PythonEnvironmentQueryData | TemplateTagQueryData |
0 commit comments