|
| 1 | +mod templatetags; |
| 2 | + |
| 3 | +pub use templatetags::get_templatetags; |
| 4 | +pub use templatetags::TemplateTags; |
| 5 | + |
| 6 | +use crate::db::Db as ProjectDb; |
| 7 | +use crate::inspector::inspector_run; |
| 8 | +use crate::inspector::queries::Query; |
| 9 | +use crate::python::python_environment; |
| 10 | +use crate::Project; |
| 11 | + |
| 12 | +/// Check if Django is available for the current project. |
| 13 | +/// |
| 14 | +/// This determines if Django is installed and configured in the Python environment. |
| 15 | +/// First consults the inspector, then falls back to environment detection. |
| 16 | +#[salsa::tracked] |
| 17 | +pub fn django_available(db: &dyn ProjectDb, project: Project) -> bool { |
| 18 | + // First try to get Django availability from inspector |
| 19 | + if let Some(json_data) = inspector_run(db, Query::DjangoInit) { |
| 20 | + // Parse the JSON response - expect a boolean |
| 21 | + if let Ok(available) = serde_json::from_str::<bool>(&json_data) { |
| 22 | + return available; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + // Fallback to environment detection |
| 27 | + python_environment(db, project).is_some() |
| 28 | +} |
| 29 | + |
| 30 | +/// Get the Django settings module name for the current project. |
| 31 | +/// |
| 32 | +/// Returns the settings_module_override from project, or inspector result, |
| 33 | +/// or DJANGO_SETTINGS_MODULE env var, or attempts to detect it. |
| 34 | +#[salsa::tracked] |
| 35 | +pub fn django_settings_module(db: &dyn ProjectDb, project: Project) -> Option<String> { |
| 36 | + // Check project override first |
| 37 | + if let Some(settings) = project.settings_module(db) { |
| 38 | + return Some(settings.clone()); |
| 39 | + } |
| 40 | + |
| 41 | + // Try to get settings module from inspector |
| 42 | + if let Some(json_data) = inspector_run(db, Query::DjangoInit) { |
| 43 | + // Parse the JSON response - expect a string |
| 44 | + if let Ok(settings) = serde_json::from_str::<String>(&json_data) { |
| 45 | + return Some(settings); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + let project_path = project.root(db); |
| 50 | + |
| 51 | + // Try to detect settings module |
| 52 | + if project_path.join("manage.py").exists() { |
| 53 | + // Look for common settings modules |
| 54 | + for candidate in &["settings", "config.settings", "project.settings"] { |
| 55 | + let parts: Vec<&str> = candidate.split('.').collect(); |
| 56 | + let mut path = project_path.clone(); |
| 57 | + for part in &parts[..parts.len() - 1] { |
| 58 | + path = path.join(part); |
| 59 | + } |
| 60 | + if let Some(last) = parts.last() { |
| 61 | + path = path.join(format!("{last}.py")); |
| 62 | + } |
| 63 | + |
| 64 | + if path.exists() { |
| 65 | + return Some((*candidate).to_string()); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + None |
| 71 | +} |
0 commit comments