11use crate :: apps:: Apps ;
22use crate :: gis:: { check_gis_setup, GISError } ;
3- use crate :: scripts;
43use crate :: templates:: TemplateTags ;
5- use djls_python:: { ImportCheck , Python , RunnerError , ScriptRunner } ;
4+ use djls_ipc:: { parse_json_response, JsonResponse , PythonProcess , TransportError } ;
5+ use djls_python:: { ImportCheck , Python } ;
66use serde:: Deserialize ;
77use std:: fmt;
88
99#[ derive( Debug ) ]
1010pub struct DjangoProject {
1111 py : Python ,
12+ python : PythonProcess ,
1213 settings_module : String ,
1314 installed_apps : Apps ,
1415 templatetags : TemplateTags ,
@@ -20,54 +21,67 @@ struct DjangoSetup {
2021 templatetags : TemplateTags ,
2122}
2223
23- impl ScriptRunner for DjangoSetup {
24- const SCRIPT : & ' static str = scripts:: DJANGO_SETUP ;
24+ impl DjangoSetup {
25+ pub fn setup ( python : & mut PythonProcess ) -> Result < JsonResponse , ProjectError > {
26+ let response = python. send ( "django_setup" , None ) ?;
27+ let response = parse_json_response ( response) ?;
28+ Ok ( response)
29+ }
2530}
2631
2732impl DjangoProject {
2833 fn new (
2934 py : Python ,
35+ python : PythonProcess ,
3036 settings_module : String ,
3137 installed_apps : Apps ,
3238 templatetags : TemplateTags ,
3339 ) -> Self {
3440 Self {
3541 py,
42+ python,
3643 settings_module,
3744 installed_apps,
3845 templatetags,
3946 }
4047 }
4148
42- pub fn setup ( ) -> Result < Self , ProjectError > {
49+ pub fn setup ( mut python : PythonProcess ) -> Result < Self , ProjectError > {
4350 let settings_module =
4451 std:: env:: var ( "DJANGO_SETTINGS_MODULE" ) . expect ( "DJANGO_SETTINGS_MODULE must be set" ) ;
4552
46- let py = Python :: initialize ( ) ?;
53+ let py = Python :: setup ( & mut python ) ?;
4754
48- let has_django = ImportCheck :: check ( & py , "django" ) ?;
55+ let has_django = ImportCheck :: check ( & mut python , Some ( vec ! [ "django" . to_string ( ) ] ) ) ?;
4956
5057 if !has_django {
5158 return Err ( ProjectError :: DjangoNotFound ) ;
5259 }
5360
54- if !check_gis_setup ( & py ) ? {
61+ if !check_gis_setup ( & mut python ) ? {
5562 eprintln ! ( "Warning: GeoDjango detected but GDAL is not available." ) ;
5663 eprintln ! ( "Django initialization will be skipped. Some features may be limited." ) ;
5764 eprintln ! ( "To enable full functionality, please install GDAL and other GeoDjango prerequisites." ) ;
5865
5966 return Ok ( Self {
6067 py,
68+ python,
6169 settings_module,
6270 installed_apps : Apps :: default ( ) ,
6371 templatetags : TemplateTags :: default ( ) ,
6472 } ) ;
6573 }
6674
67- let setup = DjangoSetup :: run_with_py ( & py) ?;
75+ let response = DjangoSetup :: setup ( & mut python) ?;
76+ let setup: DjangoSetup = response
77+ . data ( )
78+ . clone ( )
79+ . ok_or_else ( || TransportError :: Process ( "No data in response" . to_string ( ) ) )
80+ . and_then ( |data| serde_json:: from_value ( data) . map_err ( TransportError :: Json ) ) ?;
6881
6982 Ok ( Self :: new (
7083 py,
84+ python,
7185 settings_module,
7286 Apps :: from_strings ( setup. installed_apps . to_vec ( ) ) ,
7387 setup. templatetags ,
@@ -110,8 +124,11 @@ pub enum ProjectError {
110124 Json ( #[ from] serde_json:: Error ) ,
111125
112126 #[ error( transparent) ]
113- Python ( #[ from] djls_python:: PythonError ) ,
127+ Packaging ( #[ from] djls_python:: PackagingError ) ,
114128
115129 #[ error( transparent) ]
116- Runner ( #[ from] RunnerError ) ,
130+ Python ( #[ from] djls_python:: PythonError ) ,
131+
132+ #[ error( "Transport error: {0}" ) ]
133+ Transport ( #[ from] TransportError ) ,
117134}
0 commit comments